Problem
I’ve generated a database in SQL Server Management Studio and want to use it in my C# program now. I’m looking for the connection string.
Where do I look for the connection string, and where is my database kept?
Do I have to publish it or something, or is it somewhere in my documents?
using (var conn = new SqlConnection("your connection string to the database"))
What’s the best way to get the connection string? What is the location of the connection string that I need to copy and paste into the above section?
How can I make my database available for Visual Studio to use? Then I can just untangle the connection string?
Asked by Pomster
Solution #1
The simplest way to obtain the connection string is to open Visual Studio’s “Server Explorer” window (menu View, Server Explorer) and connect to the server from there.
The connection string can then be found in the properties of the connected server (right-click on the connection and press F4 or Alt+Enter, or choose Properties from the right-click menu).
Advanced connection string settings: When making the connection, select the “Advanced…” button at the bottom of the “Add connection” dialog to adjust any of the advanced connection string options, such as MARS, resilience, timeot, pooling configuration, and so on. By right-clicking the Data Connection and selecting “Modify connection…”, you can go to this dialog later. The advanced options provided differ depending on the server type.
If you create the database with SQL Server Management Studio, it will be created in a server instance, thus you’ll have to backup the database and deploy it in the deployment SQL Server to deploy your application. You may also utilize SQL Server Express (localDB in SQL Server 2012) to create a data file that can be simply deployed with your app.
If it’s an ASP.NET application, for example, there’s an App Datafolder. You can add a new element, such as a SQL Server Database, by right-clicking it. This file will be placed in that folder, will operate with SQL Express, and will be simple to install. This requires SQL Express / localDB to be installed on your machine.
Answered by JotaBe
Solution #2
To retrieve a connection string, create a text file with the extension.udl and change the extension from.txt to.udl.
The Data Link Properties wizard is launched by double-clicking the.udl file.
Configure and test your database server connection.
To utilize the connection string in your C# program, close the wizard and open the.udl file with your preferred text editor. Simply copy the connection string (without the Provider=driver> section).
content of a sample udl file
[oledb]
; Everything after this line is an OLE DB initstring
Provider=SQLNCLI11.1;Integrated Security=SSPI;Persist Security Info=False;User ID="";Initial Catalog=YOURDATABASENAME;Data Source=YOURSERVERNAME;Initial File Name="";Server SPN=""
what you’ll need to take notes from it
Integrated Security=SSPI;Initial Catalog=YOURDATABASENAME;Data Source=YOURSERVERNAME;
You can use alternative answers if you want to specify a username and password.
Tutorial: https://teusje.wordpress.com/2012/02/21/how-to-test-an-sql-server-connection/
Answered by Filburt
Solution #3
Go to Visual Studio if you’ve already installed and configured MS SQL Server and Management Studio (Visual Studio not SQL Server Management Studio).
Answered by Junaid Pathan
Solution #4
Every DB provider’s connection string may be found on connectionstrings.com. Certain attributes/properties and their values are used to create a connection string. It looks like this (standard, which is what you’ll need here) for SQL Server 2008:
Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;
Put the name of your installed instance on myServerAddress (by default, it’s.SQLEXPRESS for SQL Server Express edition). After connecting to SSMS, you’ll notice your database name on the left side of the screen. The rest is self-evident.
edit
For Windows authentication, you’ll need to remove the username and password and replace them with Integrated Security=SSPI.
Answered by MarioDS
Solution #5
Run the following query in SQL Server Management Studio. You will be given the following connection string:
select
'data source=' + @@servername +
';initial catalog=' + db_name() +
case type_desc
when 'WINDOWS_LOGIN'
then ';trusted_connection=true'
else
';user id=' + suser_name() + ';password=<<YourPassword>>'
end
as ConnectionString
from sys.server_principals
where name = suser_name()
Answered by Brijesh Kumar Tripathi
Post is based on https://stackoverflow.com/questions/10479763/how-to-get-the-connection-string-from-a-database