Click here to Skip to main content
16,018,458 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I would like to create two object in connecting to the database, Does it work well?

cnnString = "Data Source =" + datasource + "; Initial Catalog =" + database1 + "; User ID =" + user_ID + "; Password =" + pwd
cnn1 = New SqlConnection (cnnString)

and
cnnString = "Data Source =" + datasource + "; Initial Catalog =" + database2 + "; User ID =" + user_ID + "; Password =" + pwd
cnn2 = New SqlConnection (cnnString)
Posted
Comments
Richard C Bishop 17-Sep-13 16:54pm    
That is not how you want to store connection strings. Ideally you want them in your .config file. If your connection strings are created on the fly, just just string.Format to enter the data needed for the connection string. See an example below as a solution.
Majed Jemmieh 17-Sep-13 17:12pm    
Thank you
I refer to the "cnn1" and the "cnn2".



If you're asking if you can create two connections to two different databases at the same time, then yes, you can.

I question how you're doing it though, as someone else has already pointed out.
 
Share this answer
 
Comments
Majed Jemmieh 23-Dec-15 13:20pm    
thank you
Here is how you can store your connection string in the config file and use it in your code:
<add key="ConString" value="Data Source={0};Initial Catalog={1};User ID={2};Password={3}" />


In your code behind:
string connection = (ConfigurationManager.ConnectionStrings["ConString"].ConnectionString).ToString();

SqlConnection con = new SqlConnection(string.Format(connection, datasource, database1, user_ID, pwd));
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900