初学Enterprise Library,不知道该如何使用?!!!我是这么做的:1,在微软的网站下了一个Enterprise Library 3.1的安装文件,安完后把bin文件夹下的Microsoft.Practices.EnterpriseLibrary.Data.dll 和 Microsoft.Practices.EnterpriseLibrary.Common.dll 拷到了我的项目中,并在项目中引用了这两个dll。2,配置了我的web.config文件,加了如下的内容:<configSections>
    <section name="dataConfiguration"            type="Microsoft.Practices.EnterpriseLibrary.Data.Configuration.DatabaseSettings, Microsoft.Practices.EnterpriseLibrary.Data, Version=3.1.0.0, Culture=neutral"/>
  </configSections>
  <dataConfiguration defaultDatabase="Connection String"/>
  <connectionStrings>
    <add name="Connection String" connectionString="Database=dbname;Server=(local);User ID=sa;PWD=;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
3,在数据库访问类中,用如下代码读取数据:    Database db = DatabaseFactory.CreateDatabase();
    string sqlCommand = "Select count(*) from tablename";
    DbCommand dbCommand = db.GetSqlStringCommand(sqlCommand);    return (int)dbCommand.ExecuteScalar();
可是,当执行到 return (int)dbCommand.ExecuteScalar() 时报错:Connection 属性尚未初始化。请问:
1,这是为什么?
2,我在网上看到了使用 Enterprise Library 的一些讲解,还需要在 Enterprise Library Configuration 中做一些配置,我没有做。以前,我工作中也用过企业库访问数据库,可都是套用现成的项目框架,用别人给我的相关dll,自己的机器上从来没装过什么企业库,更没有配置过,所以很迷惑!一定要做在 Enterprise Library Configuration 中做配置吗?以后把项目配置在服务器上,相应的服务器也要装 Enterprise Library 并做相关的配置?问题较着急,望帮忙,多谢!

解决方案 »

  1.   

    http://www.cnblogs.com/Ferry/archive/2009/07/17/1525745.html参考
      

  2.   

     你找找dll里 有个 显示调用 "Connection String"这个字符串 创建db的  
      

  3.   

    Connection 属性尚未初始化
    ----------------------------
    说明你的连接字符串没有使用到连接对象上.
      

  4.   

    问题已经解决,我犯了一个错误。
    在最后的执行语句中,我写的语句是:dbCommand.ExecuteScalar();
    这里执行dbCommand对象的ExecuteScalar()方法,显然是不对的。因未设置dbCommand对象的Connenction,所以执行的时候报错“Connection 属性未初始化”也就不奇怪了!
    该句代码应该修改为:db.ExecuteScalar(dbCommand);问题已解决,谢谢大家的帮忙!ths
      

  5.   

    CreateDatabase("Connection String")