Click here to Skip to main content
14,447,974 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello everyone
I am writing a project which needs to connect to sql server ( with predefined tables) in run time. for example I want to get IP, port, username and password of sqlserver from user and then connect to it and fetch some data. my database table structure is same for all users but IP, port, username and password is different. so how can I connect dynamically in run time?

more information:
there are some Companies which they have a specified predefined database. my program will be installed on the companies client computers. so, I need to get IP, port, username and password of database servers and then create connection string and connect to each database with its own connection string.


thanks
Posted
Updated 11-Jan-14 19:23pm
v2
Comments
Sandeep Singh Shekhawat 11-Jan-14 6:07am
   
You can write configuration information in text file and then read connection string from text file dynamicaaly.
Karthik_Mahalingam 11-Jan-14 6:33am
   
windows or web ?
xLordProgrammer 11-Jan-14 7:04am
   
Windows (desktop application)
Karthik_Mahalingam 11-Jan-14 7:16am
   
still i am confused with the requirement..
can u pls add more info..
neymar1107 11-Jan-14 10:31am
   
i dont know
CHill60 11-Jan-14 11:31am
   
This is a little confusing ... is this application going to be run by the Users or by you? If by users, is that users databases on the same machine as the application? If by you, are you expecting the users to share their password with you? Can you give us some more detail
xLordProgrammer 12-Jan-14 0:22am
   
there are some Companies which they have a specified predefined database. my program will be installed on the companies client computers. so, I need to get IP, port, username and password of database servers and then create connection string and connect to each database with its own connection string.
Karthik_Mahalingam 12-Jan-14 0:33am
   
wheather the users will be knowing the ip,username,password ??
xLordProgrammer 12-Jan-14 1:21am
   
yes they know
Karthik_Mahalingam 12-Jan-14 1:32am
   
then you can provide a form windows for configuring the connection string.
and you can use that for dealing with DB ...
CHill60 13-Jan-14 11:21am
   
Virtual 5!
Rate this:
Please Sign up or sign in to vote.

Solution 1

There is a constructor for the connection object which takes a connection string, so just pass it through and it won't use the one in your config file.
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

You can configure the connection string at the backend. Almost all the DB dll have connectionstring as property.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.connectionstring(v=vs.110).aspx[^]

You can refer the following links to create the connection string dynamically

http://stackoverflow.com/questions/16511244/how-to-create-connection-string-dynamically-in-c-sharp[^]

If you think that the database would be same for all the clients then you can import only the required DLL eg: Sql DB needs only System.Data.SqlClient else you can add the needed dll and based on the DB that the client uses you can initiate the connection string

Reference for the DLL's required: http://stackoverflow.com/questions/16511244/how-to-create-connection-string-dynamically-in-c-sharp[^]
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100