Sunday, March 30, 2014

Dynamically insert the database name in the connection string


I want to enable the usr to insert the name of the database. Than this name I want to use in the connection string in this format



string connString = @"
server = server1;
integrated security = false;
database = the inserted databaseName by the user //??
user id = user;
password = password;



Again, just build the string in steps as needed.



Assuming that you really *want* to keep the newlines and spaces that your example will

create because of the @" then you can do something like this:



Console.Write("Enter database name: ");
string dbn = Console.ReadLine();

string connString = @"
server = server1;
integrated security = false;
database = "
+ dbn + @";
user id = user;
password = password;";
Console.WriteLine(connString);

Sample input/output:



Enter database name: My database

server = server1;
integrated security = false;
database = My database;
user id = user;
password = password;

- Wayne



No comments:

Post a Comment