开发者

Getting initial catalog from the web.config file

开发者 https://www.devze.com 2023-02-13 08:51 出处:网络
I have a connecti开发者_Python百科on string in the web.config file. I have to get the database name from it.

I have a connecti开发者_Python百科on string in the web.config file. I have to get the database name from it. Let say my connection sting is

<add name="LocalSqlServer" connectionString="Data Source=XYZ;Initial Catalog=MyDataBase;Integrated Security=true" providerName="System.Data.SqlClient"/>

I want to get the database name [i.e. Initial Catalog] from the connection string.

How can I get it?


You can use the SqlConnectionStringBuilder for this purpose:

string connectionString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder(connectionString);

string database = builder.InitialCatalog;
0

精彩评论

暂无评论...
验证码 换一张
取 消