Microsoft Enterprise Library Connection String
Hi all I've successfully got data out of th开发者_如何学Pythone database instance shown in the code. But how do I get the database name from the database instance. I can't find any properties regarding this. Please help.
private Database _db = EnterpriseLibraryContainer.Current.GetInstance<Database>("ConnString");
There is no explicit property for this, because Database is a database technology independent class, and the concept of "database name" is db specific. Heck, things like Sqlite or SqlCE don't even have "database names", just filenames.
You can use "_db.ConnectionString" to get the connection string back out and then parse through that if you know the type of database. Each ADO.NET provider includes a connection string builder class to do that parsing for you.
For example, if you have a MS Sql connection string, you can get the database name this way:
var connectionStringBuilder = new SqlConnectionStringBuilder(_db.ConnectionString);
string databaseName = connectionStringBuilder.InitialCatalog;
Different database providers will of course use different properties and terminology to give you this information.
精彩评论