常用封装链接数据库类

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
using System.Data.OleDb;
using System.IO;

/// <summary>
/*************************************************************************
 【名    称】: 数据库操作类
 【功    能】:
 【作    者】: jhabb  邮箱:jhabb@163.com//   qq :75420724      
 【项目名称】:

 【创建日期】:
  ========================================================================
 【日  志】:
 *************************************************************************/
/// </summary>
public class DbOperator
{
    public DbOperator()
    {
        //
        // TODO: 在此处添加构造函数逻辑
        //
    }

    /// <summary>
    /// 填充数据表
    /// </summary>
    /// <param name="strConnName">数据连接名称</param>
    /// <param name="strSQL">查询语句</param>
    /// <param name="DTable">要被填充的数据表</param>
    /// <returns>成功 -- true; 异常 -- false;</returns>
    public bool FillDataTable(string strConnName, string strSQL, DataTable DTable)
    {
        bool bRet = true;
       
        //MySqlConnection
        MySqlConnection OleDbConn = new MySqlConnection(ConfigurationManager.ConnectionStrings[strConnName].ConnectionString);
        try
        {
            // 检测连接字符串和查询语句是否为空
            if ((string.IsNullOrEmpty(strConnName)) || string.IsNullOrEmpty(strSQL))
                return false;
            DTable.Clear();
           

            OleDbConn.Open();

            MySqlCommand DbCommand = OleDbConn.CreateCommand();
            DbCommand.CommandText = strSQL;
           
            MySqlDataAdapter adapter = new MySqlDataAdapter(DbCommand);
            adapter.Fill(DTable);

            OleDbConn.Close();
        }
        catch (MySqlException ec)
        {
            if (OleDbConn.State == ConnectionState.Open)
                OleDbConn.Close();

            bRet = false;
        }

        return bRet;
    }

    /// <summary>
    /// 执行SQL语句
    /// </summary>
    /// <param name="strConnName">数据连接名称</param>
    /// <param name="strSQL">要被执行的SQL语句</param>
    /// <returns>成功 -- true; 异常 -- false;</returns>
    public bool ExecuteSQL(string strConnName, string strSQL)
    {
        bool bRet = true;

        try
        {
            // 检测连接字符串和SQL语句是否为空
            if ((string.IsNullOrEmpty(strConnName)) || string.IsNullOrEmpty(strSQL))
                return false;
           
            MySqlConnection OleDbConn = new MySqlConnection(ConfigurationManager.ConnectionStrings[strConnName].ConnectionString);
            OleDbConn.Open();

            MySqlCommand DbCommand = OleDbConn.CreateCommand();
            DbCommand.CommandText = strSQL;

            DbCommand.ExecuteNonQuery();
            OleDbConn.Close();
        }
        catch
        {
            bRet = false;
        }

        return bRet;
    }
}

 

posted @ 2010-11-19 09:50  落冰  阅读(312)  评论(0编辑  收藏  举报