• C# MySQL 数据库操作类


    using System;
    using System.Configuration;
    using System.Collections;
    using System.Data;
    using MySql.Data;
    using MySql.Data.MySqlClient;
    namespace MyData
    {
    /// <summary>
    /// 通用数据库类MySQL 
    /// </summary>
    public class Class_mysql_conn
    {
    //public static string ConnStr = @"server=数据库;uid=帐号;pwd=密码;database=数据库;charset=utf8";
    public static string ConnStr = MyData.Properties.Settings.Default.my_soft_mysqlConn + "pwd=密码;charset=utf8;";
    
    //打开数据库链接
    public static MySqlConnection Open_Conn( string ConnStr)
    {
    MySqlConnection Conn = new MySqlConnection(ConnStr);
    Conn.Open(); 
    return Conn;
    }
    //关闭数据库链接
    public static void Close_Conn(MySqlConnection Conn)
    {
    if(Conn != null)
    {
    Conn.Close();
    Conn.Dispose();
    }
    GC.Collect();
    }
    //运行MySql语句
    public static int Run_SQL(string SQL, string ConnStr)
    {
    MySqlConnection Conn = Open_Conn(ConnStr);
    MySqlCommand Cmd = Create_Cmd(SQL, Conn);
    try
    {
    int result_count = Cmd.ExecuteNonQuery();
    Close_Conn(Conn);
    return result_count;
    }
    catch
    {
    Close_Conn(Conn);
    return 0;
    }
    } 
    // 生成Command对象 
    public static MySqlCommand Create_Cmd(string SQL, MySqlConnection Conn)
    {
    MySqlCommand Cmd = new MySqlCommand(SQL, Conn);
    return Cmd;
    }
    // 运行MySql语句返回 DataTable
    public static DataTable Get_DataTable(string SQL, string ConnStr, string Table_name)
    {
    MySqlDataAdapter Da = Get_Adapter(SQL, ConnStr);
    DataTable dt = new DataTable(Table_name);
    Da.Fill(dt);
    return dt;
    }
    // 运行MySql语句返回 MySqlDataReader对象
    public static MySqlDataReader Get_Reader(string SQL, string ConnStr) 
    {
    MySqlConnection Conn = Open_Conn(ConnStr);
    MySqlCommand Cmd = Create_Cmd(SQL, Conn);
    MySqlDataReader Dr;
    try
    {
    Dr = Cmd.ExecuteReader(CommandBehavior.Default);
    }
    catch
    {
    throw new Exception(SQL);
    }
    Close_Conn(Conn);
    return Dr;
    }
    // 运行MySql语句返回 MySqlDataAdapter对象 
    public static MySqlDataAdapter Get_Adapter(string SQL, string ConnStr) 
    {
    MySqlConnection Conn = Open_Conn(ConnStr);
    MySqlDataAdapter Da = new MySqlDataAdapter(SQL, Conn);
    return Da;
    }
    // 运行MySql语句,返回DataSet对象
    public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds) 
    {
    MySqlDataAdapter Da = Get_Adapter(SQL, ConnStr);
    try
    {
    Da.Fill(Ds);
    }
    catch(Exception Err)
    {
    throw Err;
    }
    return Ds;
    }
    // 运行MySql语句,返回DataSet对象
    public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds, string tablename) 
    {
    MySqlDataAdapter Da = Get_Adapter(SQL, ConnStr);
    try
    {
    Da.Fill(Ds,tablename);
    }
    catch(Exception Ex)
    {
    throw Ex;
    }
    return Ds;
    }
    // 运行MySql语句,返回DataSet对象,将数据进行了分页
    public static DataSet Get_DataSet(string SQL, string ConnStr, DataSet Ds, int StartIndex, int PageSize, string tablename)
    {
    MySqlConnection Conn = Open_Conn(ConnStr);
    MySqlDataAdapter Da = Get_Adapter(SQL, ConnStr);
    try
    {
    Da.Fill(Ds, StartIndex, PageSize, tablename);
    }
    catch(Exception Ex)
    {
    throw Ex;
    }
    Close_Conn(Conn);
    return Ds;
    }
    // 返回MySql语句执行结果的第一行第一列
    public static string Get_Row1_Col1_Value(string SQL, string ConnStr) 
    {
    MySqlConnection Conn = Open_Conn(ConnStr);
    string result;
    MySqlDataReader Dr ;
    try
    {
    Dr = Create_Cmd(SQL,Conn).ExecuteReader();
    if (Dr.Read())
    {
    result = Dr[0].ToString();
    Dr.Close(); 
    }
    else
    {
    result = "";
    Dr.Close(); 
    }
    }
    catch
    {
    throw new Exception(SQL);
    }
    Close_Conn(Conn);
    return result;
    } 
    }
    }

    ------------------------------------------------------------------
    app.config 里的数据库链接字符串
    ------------------------------------------------------------------
    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
    <configSections>
    </configSections>
    <connectionStrings>
    <add name="MyData.Properties.Settings.my_soft_mysqlConn" connectionString="server=localhost;database=my_soft;uid=root;"
    </connectionStrings>
    </configuration>

  • 相关阅读:
    平衡——职场小说《监控》推荐
    《java程序员全攻略:从小工到专家》连载一:外行人眼中的IT人
    《java程序员全攻略:从小工到专家》连载二:IT行情分布
    各路技术牛人都推荐的书
    程序员2009精华本 有哪些值得期待
    揭秘孙小小《PPT演示之道》
    In the beginning, the world was void and without form…
    大学计算机课程复习操作系统
    大学计算机课程复习汇编语言
    (转)Winform 创建桌面快捷方式并开机启动
  • 原文地址:https://www.cnblogs.com/future/p/3794093.html
Copyright © 2020-2023  润新知