Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get the add connection prompt on a button click event in a mvc application, that comes when we try to connect to the database.
Is there any way to achieve this?
Finally the user should be able to connect to multiple databases from the webpage and see the list of tables and stored procedures available.....

1 solution

Yes you can use multiple databases in a single application. This can be achieved by updating your connection string where in a same server or different server you can replace /add the database name or server name based on your requirement.

Suppose in your config file you have a connection string
<add name="DefectConnection">
			 connectionString="Data Source=SERVERNAME; Trusted_Connection=true;" providerName="System.Data.SqlClient"

At run time you want to add the database name. So add Initial Catalog within your code as below:

protected const string ConnectionStringKey = "DefaultConnection";
static string preConString = ConfigurationManager.ConnectionStrings[ConnectionStringKey].ConnectionString;
private static string conString = String.Format("{0};Initial Catalog={1}", preConString, "YOUR DATABASENAME");

Here you can use the configuration settings to get the connection name, you will be able to change the database name with the database name that you want to use.

You can use them within any place within your code.

Also you can use multiple connection strings as well.

Hope this will help.
Share this answer
goshan2011 12-Jun-15 1:17am    
Thank you for the info, but during run time i want the user to Add connection to new db server by himself and on test connection succeed, he sees the list of tables in the new db he got connected to... may be in this case i might make use of a view to fetch the list of tables available in the new to achieve this?
Mostafa Asaduzzaman 12-Jun-15 1:20am    
you can construct the connection string within your code and then connect to the database
goshan2011 12-Jun-15 1:36am    
No, i want to prompt a window to the user, will allow him to enter the server details along with the user id and password... like Add connection prompt window that comes while connecting to the new db/server. Any way to achive this? I have seen similar thing in the Tableau desktop tool, but dont know how to achive it. Tableau claims that it uses native connectors.
Mostafa Asaduzzaman 12-Jun-15 2:26am    
you can use a modal dialog for this purpose, take to modal inputs and send them to the backend

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