How to get the connection string?

I'm developing an application and connecting to a database so I can get the connection string and know it during development pahase and therefore I can set it to the ConnectionString property of my connection object. However, when I deploy this application on a client machine. How can I get the connection string to set it to the ConnectionString property for the connection object to access the database. Which means I cannot make my connection string a fixed connection string I need the connection string to be created dynamically.

I hope you understand what I mean.

Edited 2 Years Ago by AmrMohammed

for Oledb:

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"\\DBData1.mdb");

for SqlServer Database File:

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\Db_data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

before using this above code in C#.net make sure your database file is present in same of EXE's folder.
ex:

bin\Debug\myApp23.exe

bin\Debug\Db_data.mdf
bin\Debug\Db_data_log.ldf

bin\Debug\DBData1.mdb

"Server={0};Database={1};User ID={2};Password={3};"
Where the {#} are where you enter your data, however, this is if you want to connect with SQL authentication, if you are using WIndows authentiation you can use the following string

Server={0};Database={1};Trusted_Connection=True;

These however are for SQL Server, which you plug the string into
DbConnetion connection = new SqlConnection("String here");

This allows for dynamic, just have to change up the string values you pass in (whether you use say a config file that's read in at run time or what not)

Edited 2 Years Ago by JOSheaIV

So I have the connection string for SQL Server, Oracle, and AS400 (and possibly IBMDB2). However they work around what I pasted above. If you want I can share them

Thanks to all of you guys

"AngelofD4rkness" First thanks for you for your interest in the subject. But I do not want the connection string like that way.

I wanna make the connection string dynamic as the programs you buy which having a database with it only what you need to do is installing the program and behind the senses the connection string is created

I already mentioned above

First confirm your database is in bin\Debug folder.
then use the below connection strings.

for Oledb:

OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Application.StartupPath+"\\DBData1.mdb");

for SqlServer Database File:

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=" + Application.StartupPath + "\\Db_data.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

ex:

bin\Debug\Db_data.mdf
bin\Debug\Db_data_log.ldf

bin\Debug\DBData1.mdb

you can simply create a text file on clients machine and rename it with text.udl and then open it with Ole DB Core Services(Option will be available in open with section when you right click on file).
Now a window will appear haing tabs like provider,Connection,Advanced etc. so go to Provider Tab select Provider and then go to second tab Select server name,Information to log on server and Database name on that server.Then Test this connection.
On Successful connection save it and close this window.
now open this UDL file in to Notepad and That's It here is your Connection String you can use this connection string in your application and it will work for that local system.

"Suliman_1", "Daemon_CC" and all of you guys thanks for help. "Sulaiman_1" I saw your first post and it is a good approach and I know it :) and I wanted to know other techniques and I'm asking what if I did not put the Database file within my bin\debug directory what if I install the SQL Server on the client machine then I attach the database. How can I make something like a database selection Windows Form that shows available database to select from and when select a connection string is constructed.

"Daemon_CC" Thanks for your idea it was something new for me to know :)

Thanks to all of you

Ahh I see what you mean.

Well let me know still if you are interested in any of those other connection strings (also, I assume you are not installed the database as well, if so, then you control the configuration)

This article has been dead for over six months. Start a new discussion instead.