Prompt for SQL Server to connect to in Visual Basic 2010 - sql-server

I have a project that current uses a hard coded SQL connection string. I want to make this more flexible so that when I move it to another environment the user can browse for the SQL server to connect to since it will have a different than my test server. I have tried countless ways to Google for a solution and I am just not getting any hit. I am looking for something similar to the open file dialog in VB, except for SQL servers, where it will list all the servers available on the network and let the users select one, then enter credentials.
Does such a thing exist?
Thanks!

It should be easy to develop a dialog box similar to one SQL Server Management Studio uses. Be aware that all instances on the network cannot be enumerated because broadcast packets are not typically routed and due to firewalls so you'll need to allow the user to enter a server name in the combobox. Here's a code snippet to return a DataTable of the found instances (http://msdn.microsoft.com/en-us/library/a6t1z9x2(v=vs.110).aspx):
Dim dt As DataTable = System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources
Below is a screenshot of the login dialog from SSMS. Just populate the server name combobox with the server and instance names from the data table, and add the other needed controls.

Related

SSRS - Unable to connect to datasource

Context: I have an installation of SQL Server 2019 installed on my local workstation and within an SSRS Report Project I'm trying to create a dataset that connects to this SQL Server datasource. I keep raising this error
"Unable to connect to datasource 'XXXX'"
EDIT: I realized clicking through the details there's a more descriptive message here: You have specified integrated security or credentials in the connection string for the data source, but the data source is configured to use a different credential type. To use the values in the connection string, you must configure the unattended report processing account for the report server.
I'm thought I was using Windows Authentication. This is a very strange error because prior to this step when I create the datasource and test the connection the test succeeds. How is it that the datasource connection properties dialog recognizes the server, database name and successfully authenticates my windows creds in the connection test but suddenly the dataset dialog cant connect...?
How have I tried to fix this: I thought this could be an issue with the ODBC driver I'm using so I've installed both Microsoft ODBC Driver 13.1 & 17 for SQL Server which is not resolving the issue.
Question: Does anyone know why this is happening and how to fix it?
Answer: Turns out under the "Shared Data Source Properties" dialog box, within the "Credentials" tab, the radio dial for "Use Windows Authentication (integrated security)" needed to be selected.
I assume the reason the connection test succeeded was because SSRS guessed at the correct authentication method, but it still needs to be explicitly selected for the dataset to connect properly.
Step 1:- Click on Data Source
Select Add Data Source
It show two options in Left corner of Data source property window called General and Credential.
Select Credential.
Note (if Use Windows Authentication(Integrated security) is already selected also DO not Press Ok Follow Below Procedure steps.
First choose Do not use Credentials(Radio Button) and Do not press Ok.
then select Use Windows Authentication(Integrated security) Press OK.
Now Data source1 is Created.
Step 2:-Click Right Button on Data Source which is New created -> Select (Data Source Property)
Click Embedded Connection Radio Button
Select Type as: Microsoft SQL Serve
For Connection String Click on BUILD/EDIT Button
In Connection Property Enter your Server Name (Sql Server Name)
Select or enter name select Database name
Click Test Connection Click Ok
Once this is completed.
Step 3:-Right click on Date Source Which you are Created
Click on ADD DATASET
In Data Set Properties Enter Dataset Name
Select Data Source which you created.
Enter Query or Select Query designer and Click Ok.
Note Please follow Step 1 Properly this work only for Windows Authentication.
If you Have any Doubts Mail Me My Mail ID:- Harshithwight83#gmail.com.
If you're using Visual Studio to design SSRS reports:
Double-Click the data source file on the right side of the screen in the Solution Explorer window.
Click "Credentials"
Select "Use Windows Authentication (integrated security)
Click "OK"
It should work now.

Labview - SSMS database communication... How to communicate between Labview and Microsoft SQL Server on separate devices?

I am attempting to set up a communication between Labview and Microsoft SQL Server, on two separate devices, in order to send and receive information about the database from both labview to SQL Server and SQL Server to labview. However, when I reach the "Data Link Properties" menu, I get the same "unable to log in" error upon attempting to log into the server. The server name comes up, however, an error occurs once I move on to select the database on that server. Is there any solution or tutorial to this problem that can allow me to successfully communicate back and forth from labview and smss on separate devices?
I've opened up various ports to allow a connection, even disabled the firewalls on both devices. The devices are connected via an Ethernet cable and I AM able to ping the devices to each other. However, in regards to being unable to log into the server in ssms, I have created new users, adjusted the login properties, tried changing permissions, but anything I try doesn't seem to solve my issue.
Can't really help much without seeing the error or some of the code of what you are trying to do.
That being said, if you go to the menu and select Help>Find Examples... and search for database, you should see a bunch of different things related to database connections. You may find the Database Connection.vi one helpful.
More info on the Database Connectivity Toolkit in LabVIEW can be found here
I see there can be one of the 2 issues
1) Inbound/Outbound port rules not set, Remote connection to server is not allowed.
2) If the server has multiple instances then you need to provide full host name of the instance you are trying to connect.
*Please refer to the below link to configure firewall rules.
https://learn.microsoft.com/en-us/sql/sql-server/install/configure-the-windows-firewall-to-allow-sql-server-access?view=sql-server-2017

Can't access AWS RDS database even though CIDR/IP added in security group

I have created a db instance (sql server ex) in my AWS account and now trying to access it via MS SQL Server Management Studio or via Visual Studio Server Explorer, but i can't, i get the errors like below:
I have added my ip address in inbound under security group as well:
I Tried all the above steps but didn't work. Even though, i have selected 'Publicly Accessible' while using visual studio, it didnt reflect in the aws console. So, the below steps worked for me. Please try this.
Note:- this is only for my testing/learning purposes. So, i made it publicly accessible. You might not enable publicly accessible if there is a security issue. If you are testing/using for learning purposes, you can follow the below approach.
Make sure your port is publicly accessible .Else you will not be able to connect.
To enable the public accesss, please follow the below steps.
Select the db instance --> click Modify
Scroll to connectivity Section, expand 'Additional configuration' section --> select 'Publicly Accessible'--> Scroll to bottom --> select update. (as shown below).
Your db instance will be in modifying state and after 2-3 minutes it will be available. Now, give it a try to connect again and you will be able to connect now.

How to avoid prompting for user id and password in MSAccess 2003

I am a .Net / SQL Server programmer. I am trying to make an Access database created by someone else to work. It looks like is uses a procedure similar to Save password for ODBC connection to SQL Server from MS Access 2007 but I am using Access 2003 so I suspect something else needs to be done. I don't really know what I am doing with Access but I am stuck with it.
I do have an ODBC connection and it looks like the linked tables use it. The database has an AutoExec which populates some local tables from the linked tables that the reports use. It also calls a qryConnect with a dsn-less connection like the linked article and this seems to work. I have got it to stop asking for a user and password on start up. I can also open most reports without problems, 2 do not work. A form loads on Startup with buttons that basically show reports. This is where it gets odd.
The reports fail from the ODBC driver with the windows credentials. Then a logon box shows up with the correct user (a user with read only privileges) but the trusted connection box is checked. I do not have the trusted box checked in the DSN. Not do I have Trusted_Connection=Yes in the DSN-less connection in qryConnect.
This is the code in the form
stDocName = "rptNegativeLotQtys"
DoCmd.OpenReport stDocName, acPreview
I don't know how I buggered it up so much and before I try again copying everything to a new mdb and trying again I am hoping to get some guidance.
On a side note I can open everything fine on my machine. I am an administrator as far as Windows is concerned but not SQL Server.
EDIT:
I created a new DSN just for these reports. Then I deleted all linked tables and re-linked then using the new DSN. I looked at the linked tables in MsysObjects and the new DSN is listed there. Yet Access still prompts for the password. It appears that it is not caching the password.
Actually, you do NOT need to add nor store the UID/Pass in the connection strings used. However, what you MUST ensure is that all connection strings are the SAME. If they are different, then the cached UID/password will not work.
Also, make sure you do NOT connect nor open a table AFTER having connected using Windows Auth – the reason being then when you add the table links (especially via code), then the cached setup of using windows auth will occur, and thus prompts will occur when the links suggest to do otherwise. In other words linking and attempted to use SQL logons will NOT work if you already opened any table link via windows auth (so exit the database and do NOT open any table that could/can use windows auth).
So I would delete the links, and re-create them – but again, ensure that you never connected as a windows auth user to SQL server.
So the "cache" that access has is a GREAT friend/feature to eliminate the need to include UID/pass in the connection string - but the SAME feature will bite you VERY bad if you at any time connecte to the database in question via windows auth.
How to “cache” the user logon and ID is explained here:
Power Tip: Improve the security of database connections
http://blogs.office.com/b/microsoft-access/archive/2011/04/08/power-tip-improve-the-security-of-database-connections.aspx
So you “can” include the UID/LOGON in the linked tables, but above shows that a ONE time logon can also be used. So be VERY careful when setting up table links – due to the above “cache” of the user and HOW they logged into the database – code that ATTEMPTS to create table links based SQL logons will actually wind up using windows auth if you already connected as such (so exit the database if you going to re-link using SQL logons).
Keep in mind, Access does NOT use the DSN AFTER you linked the table. The information from the DSN is a ONE TIME copy to the connection string. You can verify this by going into my documents and deleting the DSN you used. Assuming the linked table was working correctly, you find that they CONTINUE to work EVEN if you delete the DSN. In effect this means that linked tables are DSN less except for when you create the linked table. This allows you to easy copy the application to different computers without having to copy the DSN.
As to ensure that all the linked tables use the same connection string, it is a simple matter to delete them all, and re-link.
You can also hit ctrl-g to bring up the debug window, and look at the connection string this way:
? currentdb.TableDefs("linked table name").Connect
the result in the debug window for a windows auth connection string will look like this:
ODBC;Description=test DSN;DRIVER=SQL Server;
SERVER=albertkallal-pc\SQLEXPRESS;
Trusted_Connection=Yes;
APP=Microsoft Office 2010;DATABASE=AxisMIS
Note how in above we see "trusted connection" (that means windows auth).
If I linked the table using SQL logon (and REMEMBER to check save password), then you see this:
ODBC;Description=TEST3;DRIVER=SQL Server;
SERVER=ALBERTKALLAL-PC\SQLEXPRESS;
UID=MySQLogon;PWD=MyPassword;
APP=Microsoft Office 2010;DATABASE=AxisMIS
Just remember during the table link process to "check" the save password.
eg this:

How to connect a new query script with SSMS add-in?

I'm trying to create a SSMS add-in. One of the things I want to do is to create a new query window and programatically connect it to a server instance (in the context of a SQL Login). I can create the new query script window just fine but I can't find how to connect it without first manually connecting to something else (like the Object Explorer).
So in other words, if I connect Obect Explorer to a SQL instance manually and then execute the method of my add-in that creates the query window I can connect it using this code:
ServiceCache.ScriptFactory.CreateNewBlankScript(
Editors.ScriptType.Sql,
ServiceCache.ScriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo,
null);
But I don't want to rely on CurrentlyActiveWndConnectionInfo.UIConnectionInfo for the connection. I want to set a SQL Login username and password programatically.
Does anyone have any ideas?
EDIT:
I've managed to get the query window connected by setting the last parameter to an instance of System.Data.SqlClient.SqlConnection. However, the connection uses the context of the last login that was connected instead of what I'm trying to set programatically. That is, the user it connects as is the one selected in the Connection Dialog that you get when you click the New Query button and don't have an Object Explorer connected.
EDIT2:
I'm writing (or hoping to write) an add-in to automatically send a SQL statement and the execution results to our case-tracking system when run against our production servers. One thought I had was to remove write permissions and assign logins through this add-in which will also force the user to enter a case # canceling the statement if it's not there. Another thought I've just had is to inspect the server name in ServiceCache.ScriptFactory.CurrentlyActiveWndConnectionInfo.UIConnectionInfo and compare it to our list of production servers. If it matches and there's no case # then cancel the query.
Just in case anybody else is searching for a way to create and connect a query window programmaticaly - a short example for SSMS 2012:
UIConnectionInfo u =new UIConnectionInfo();
u.ServerName = "TestPC\\ServerInstance";
u.ServerType = new Guid("8c91a03d-f9b4-46c0-a305-b5dcc79ff907");
u.AuthenticationType = 0;//Use AuthenticationType = 1 for SQL Server Authentication
u.AdvancedOptions.Set("DATABASE", "AdventureWorks2012");
IScriptFactory scriptFactory = ServiceCache.ScriptFactory;
if(scriptFactory != null)
{
scriptFactory.CreateNewBlankScript(ScriptType.Sql, u, null);
}
This code snippet will open a new query window connected to specified server and database with windows auth.
I have not found a way to do this, since there appears to be no way to hook into the connection dialog window.
what are you working on?
EDIT:
If i understand correctly you want to intercept the query being run and if it matches the production server cancel it else send the text and results to a db?
hmm... while this would be possible but is a real major pain in the behind, and i wouldn't use an add-in for this. plus an add-in can be disabled, uninstalled etc.
you better try doing this with proper security setup on your production server.
I haven't tried this yet but didn't know if you have given it a shot.
Microsoft.SqlServer.Management.Smo.RegSvrEnum.UIConnectionInfo u =
new Microsoft.SqlServer.Management.Smo.RegSvrEnum.UIConnectionInfo();
Should allow you to make your own connection. But like I said, I haven't tested it yet.
You'll need a reference to Microsoft.SqlServer.RegSvrEnum.dll.
Let me know if it works.

Resources