Enterprise Library链接数据库

来源:互联网 发布:商品标题的优化方法 编辑:程序博客网 时间:2024/04/20 12:57
Enterprise Library装上后写了个读取数据库的小例子.共享一下:
首先我们创建一个application的项目,在项目中引用
打开工具Enterprise Library Configuration
选择File->open Application->选择我们创建好的application程序的App.config文件.
如图:
我这里有个问题就是Connection Strings下的节点失踪是无法将自己定义好的链接字符串
写入config文件中,所有我删除了以前默认的创建了个新的Connection String节点.
选择,按照下图填写connectionstring字符串
ok了.其他的什么都不用修改.
然后我打开form1.cs在后台添加如下代码:
增加引用:
using Microsoft.Practices.EnterpriseLibrary.Data;
 
      public Form1()
        {
            InitializeComponent();
            Database db = DatabaseFactory.CreateDatabase("Connection String");
            System.Data.Common.DbCommand d = db.GetSqlStringCommand("select * from info");
            DataSet ds = db.ExecuteDataSet(d);
            label1.Text = ds.Tables[0].Rows[0][0].ToString();
        }
运行.看结果?简单吧.
后面我将继续写关于Enterprise Library的文章.请大家多多关注于评论.谢谢.