[英]Data Access Application Block 5.0 fluent configuration
I am using following code to setup connection string:我正在使用以下代码来设置连接字符串:
ConfigurationSourceBuilder builder = new ConfigurationSourceBuilder ();
builder.ConfigureData ()
.ForDatabaseNamed ( "TestDatabase" )
.ThatIs.ASqlDatabase ()
.WithConnectionString ( "Data Source=127.0.0.1;User Id=sa;Password=123;Initial Catalog=DataAccessExamples;" )
.AsDefault ();
var configSource = new DictionaryConfigurationSource ();
builder.UpdateConfigurationWithReplace ( configSource );
EnterpriseLibraryContainer.Current
= EnterpriseLibraryContainer.CreateDefaultContainer ( configSource );
After executing above code I expect following code to work but it says connection not initialized and command.Connection property always keeps null.执行上述代码后,我希望以下代码可以工作,但它说连接未初始化并且 command.Connection 属性始终保持 null。
Database database = DatabaseFactory.CreateDatabase ();
DbCommand command = database.GetSqlStringCommand ( "Select * from TT" );
DbDataReader dbReader = command.ExecuteReader ( System.Data.CommandBehavior.CloseConnection );
while (dbReader.Read ())
{
System.Diagnostics.Debug.WriteLine ( dbReader[0].ToString () );
}
Please can anyone tell me why connection is not getting initialized?请谁能告诉我为什么连接没有被初始化?
I am using above code in a library project and want to configure DAAB at runtime.我在库项目中使用上述代码,并希望在运行时配置 DAAB。
Thanks谢谢
For one, why are you hard coding the connection string?一方面,为什么要对连接字符串进行硬编码? Wouldn't it make more sense to have the connection string in the config file for your application?在您的应用程序的配置文件中包含连接字符串不是更有意义吗?
Why do you need to use the Database factory?为什么需要使用数据库工厂?
SqlDatabase db = new SqlDatabase("some connection string from the config");
DbCommand cmd = db.GetSqlStringCommand(sqlStatement);
IDataReader reader = db.ExecuteReader(cmd);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.