我有连接字符串:
<add name="fullDataBase"
connectionString="Data Source=.;Initial Catalog=Equipme;Integrated Security=True"
providerName="System.Data.SqlClient"/>
然后,当我使用以下字符串时,我可以从中获取目录的名称:
string connectionString = ConfigurationManager.ConnectionStrings[_connectionString].ConnectionString;
在我的情况下,它应该是Equipme
答案 0 :(得分:11)
使用SqlConnectionStringBuilder
class:
string initialCatalog = new SqlConnectionStringBuilder(connectionString).InitialCatalog;
答案 1 :(得分:2)
这是您的解决方案
System.Data.SqlClient.SqlConnectionStringBuilder builder = New System.Data.SqlClient.SqlConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings("fullDataBase").ToString()))
string initialCatalog=builder.InitialCatalog()
SqlConnectionStringBuilder
将创建一个ConnectionString,否则会将连接字符串分解为单独的部分行数据库名称等。