C#利用微软企业库Enterprise Library操作mysql数据库

在C#项目中,很多时候到要用到Enterprise Library。这里只是用一个很简单的小例子来演示一下Enterprise Library在VS2010中操作mysql数据库的流程。

1,利用Enterprise Library操作mysql数据库。首先要具备一下天剑

      (1)项目中要引用MySql.Data和MysroSoft.Practices.EnterpriseLibrary.Data这两个动态库。如果该项目的目标框架为.NET Framework 4 Client Profile,在程序编译过程中会报错,此时要把项目的目标框架改为.NET Framework 4(具体做法为:选中项目,点击右键,选择属性,然后进入修改即可)

       (2)安装“mysql-connector-net-6.7.4.msi”。运行程序的环境中要安装“mysql-connector-net-6.7.4.msi”。他的版本要与(1)中MySql.Data的版本一致。

      (3)在该项目的应用程序配置文件(app.config或web.config)中进行如下配置(配置中的版本号与上面的版本号一直).



  
    
       

          (4)基本操作的演示函数如下(所用数据库中含有一个名为table1的表。表中只有id和name两列):


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data;
using System.Data.Common;

namespace EnterpriseLibrary
{
    class SqlHelper
    {
       
        public static string  Add(string id,string name)
        {
            try
            {
                //建立连接
                Database db = DatabaseFactory.CreateDatabase("mysql");
                //数据插入指令
                DbCommand cmdAddOrder = db.GetSqlStringCommand(
@"insert into table1(id,name)values('" + id + "','" + name+"')");
              
                db.ExecuteNonQuery(cmdAddOrder);
                return "success";
            }
            catch (Exception ex)
            {
                //若操作时发生异常,则将异常信息写入到错误日志
                //LogContext.WriteError(ex);
                //Console.WriteLine("异常结果:{0}", ex);
                return ex.ToString();
            }
        }

       
        public static string Select(string id)
        {
            try
            {
                //创建一个连接
                Database db = DatabaseFactory.CreateDatabase("mysql");
                //创建一个插入command
                DbCommand cmd = db.GetSqlStringCommand("select id,name from table1 where id='" + id + "'");
                DataTable result = db.ExecuteDataSet(cmd).Tables[0];
                if (result.Rows.Count > 0)
                {

                   string str= result.Rows[0]["id"].ToString() +result.Rows[0]["name"].ToString();
                   return str;
                }
                else
                {
                    return "query fail";
                }
            }
            catch (Exception ex)
            {
                //若操作时发生异常,则将异常信息写入到错误日志
                // LogContext.WriteError(ex);
                return ex.ToString();
            }
        }

       
        public static string Update(string id,string name)
        {
            try
            {
                //创建一个连接
                Database db = DatabaseFactory.CreateDatabase("mysql");
                //创建一个插入command
                StringBuilder mysqlcommand = new StringBuilder();
                //生成SQL---Start
                mysqlcommand.Append("update table1 set name='").Append(name+"'");
                mysqlcommand.Append(" where id='").Append("2'");
                //生成SQL---End
                DbCommand cmdUpdataOrderState = db.GetSqlStringCommand(mysqlcommand.ToString());
                //执行SQL
                db.ExecuteNonQuery(cmdUpdataOrderState);
                return "Update Success";
            }
            catch (Exception ex)
            {
                //若操作时发生异常,则将异常信息写入到错误日志
                // LogContext.WriteError(ex);
                return ex.ToString();
            };
        }

     }
}


结束!!!

                                                                                                                                                                                                                                           2015.2.5


你可能感兴趣的:(C#技术,数据库)