• C#--SQLserver、oracle、MySQL和Access的封装源码


    抽象类:

    using System;
    using System.Collections.Generic;
    using System.Data;  //DataSet引用集
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AdoNet
    {
        public abstract class Database
        {
            public abstract string Open(string link);                                   //打开
            public abstract string Insdelupd(string sql);                               //增删改
            public abstract string Insdelupd(string sql, string link);                  //增删改
            public abstract DataSet Select(string sql, out string record);              //
            public abstract DataSet Select(string sql, string link, out string record); //
            public abstract string Close();                                             //关闭
        }
    }

    SQLserver:

    /*----------------------------------------------------------------
     * 
    // author:HQ
     * 
    // describe:SQLserver
     * 
    // date:2019-10-08
     * 
    ------------------------------------------------------------------*/
    using System;
    using System.Collections.Generic;
    using System.Data;              //DataSet引用集
    using System.Data.SqlClient;    //sql引用集
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AdoNet
    {
        public class SQLserver : Database
        {
            private SqlConnection sql_con;  //声明对象
    
            /// <summary>
            /// SQLserver open
            /// </summary>
            /// <param name="link">link statement</param>
            /// <returns>Success:success; Fail:reason</returns>
            public override string Open(string link)
            {
                try
                {
                    sql_con = new SqlConnection(link);
                    sql_con.Open();
                    return "success";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
    
            /// <summary>
            /// SQLserver close
            /// </summary>
            /// <returns>Success:success Fail:reason</returns>
            public override string Close()
            {
                try
                {
                    if (sql_con == null)
                    {
                        return "No database connection";
                    }
                    if (sql_con.State == ConnectionState.Open || sql_con.State == ConnectionState.Connecting)
                    {
                        sql_con.Close();
                        sql_con.Dispose();
                    }
                    else
                    {
                        if (sql_con.State == ConnectionState.Closed)
                        {
                            return "success";
                        }
                        if (sql_con.State == ConnectionState.Broken)
                        {
                            return "ConnectionState:Broken";
                        }
                    }
                    return "success";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
    
            /// <summary>
            /// SQLserver insert,delete,update
            /// </summary>
            /// <param name="sql">insert,delete,update statement</param>
            /// <returns>Success:success + Number of affected rows; Fail:reason</returns>
            public override string Insdelupd(string sql)
            {
                try
                {
                    int num = 0;
                    if (sql_con == null)
                    {
                        return "Please open the database connection first";
                    }
                    if (sql_con.State == ConnectionState.Open)
                    {
                        SqlCommand sqlCommand = new SqlCommand(sql, sql_con);
                        num = sqlCommand.ExecuteNonQuery();
                    }
                    else
                    {
                        if (sql_con.State == ConnectionState.Closed)
                        {
                            return "Database connection closed";
                        }
                        if (sql_con.State == ConnectionState.Broken)
                        {
                            return "Database connection is destroyed";
                        }
                        if (sql_con.State == ConnectionState.Connecting)
                        {
                            return "The database is in connection";
                        }
                    }
                    return "success" + num;
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }
            }
    
            /// <summary>
            /// SQLserver insert,delete,update
            /// </summary>
            /// <param name="sql">insert,delete,update statement</param>
            /// <param name="link">link statement</param>
            /// <returns>Success:success + Number of affected rows; Fail:reason</returns>
            public override string Insdelupd(string sql, string link)
            {
                try
                {
                    int num = 0;
                    using (SqlConnection con = new SqlConnection(link))
                    {
                        con.Open();
                        //操作数据库的工具SqlCommand
                        SqlCommand cmd = new SqlCommand(sql, con);  //操作语句和链接工具
                        num = cmd.ExecuteNonQuery();            //执行操作返回影响行数
                        con.Close();
                        return "success" + num;
                    }
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }
            }
    
            /// <summary>
            /// SQLserver select
            /// </summary>
            /// <param name="sql">select statement</param>
            /// <param name="record">Success:success; Fail:reason</param>
            /// <returns>select result</returns>
            public override DataSet Select(string sql, out string record)
            {
                try
                {
                    //储存数据的工具初始化
                    DataSet dataSet = new DataSet();
                    if (sql_con == null)
                    {
                        record = "Please open the database connection first";
                        return dataSet;
                    }
                    if (sql_con.State == ConnectionState.Open)
                    {
                        SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sql, sql_con);
                        sqlDataAdapter.Fill(dataSet, "sample");
                        sqlDataAdapter.Dispose();
                        record = "success";
                        return dataSet;
                    }
                    if (sql_con.State == ConnectionState.Closed)
                    {
                        record = "Database connection closed";
                        return dataSet;
                    }
                    if (sql_con.State == ConnectionState.Broken)
                    {
                        record = "Database connection is destroyed";
                        return dataSet;
                    }
                    if (sql_con.State == ConnectionState.Connecting)
                    {
                        record = "The database is in connection";
                        return dataSet;
                    }
                    record = "ERROR";
                    return dataSet;
                }
                catch (Exception ex)
                {
                    DataSet dataSet = new DataSet();
                    record = ex.Message.ToString();
                    return dataSet;
                }
            }
    
            /// <summary>
            /// SQLserver select
            /// </summary>
            /// <param name="sql">select statement</param>
            /// <param name="link">link statement</param>
            /// <param name="record">Success:success; Fail:reason</param>
            /// <returns>select result</returns>
            public override DataSet Select(string sql, string link, out string record)
            {
                try
                {
                    //储存数据的工具初始化
                    DataSet ds = new DataSet();
                    //相当于链接数据库的一个工具类(连接字符串)
                    using (SqlConnection con = new SqlConnection(link))
                    {
                        con.Open();                                         //打开
                        //用SqlConnection工具链接数据库,在通过sql查询语句查询结果现存入sql适配器
                        SqlDataAdapter sda = new SqlDataAdapter(sql, con);  //(查询语句和连接工具)
                        sda.Fill(ds, "sample");                             //将适配器数据存入DataSet工具中
                        con.Close();                                        //用完关闭SqlConnection工具
                        sda.Dispose();                                      //手动释放SqlDataAdapter
                        record = "success";
                        return ds;
                    }
                }
                catch (Exception ex)
                {
                    DataSet dataSet = new DataSet();
                    record = ex.Message.ToString();
                    return dataSet;
                }
            }
        }
    }

    Oracle:

    /*----------------------------------------------------------------
     * 
    // author:HQ
     * 
    // describe:Oracle
     * 
    // date:2019-10-08
     * 
    ------------------------------------------------------------------*/
    using System;
    using System.Collections.Generic;
    using System.Data;              //DataSet引用集
    using System.Data.OracleClient; //oracle引用
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AdoNet
    {
        public class Oracle : Database
        {
            private OracleConnection oracle_con;
    
            /// <summary>
            /// Oracle open
            /// </summary>
            /// <param name="link">link statement</param>
            /// <returns>Success:success; Fail:reason</returns>
            public override string Open(string link)
            {
                try
                {
                    oracle_con = new OracleConnection(link);
                    oracle_con.Open();
                    return "success";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
    
            /// <summary>
            /// Oracle close
            /// </summary>
            /// <returns>Success:success Fail:reason</returns>
            public override string Close()
            {
                try
                {
                    if (oracle_con == null)
                    {
                        return "No database connection";
                    }
                    if (oracle_con.State == ConnectionState.Open)
                    {
                        oracle_con.Close();
                        oracle_con.Dispose();
                    }
                    else
                    {
                        if (oracle_con.State == ConnectionState.Closed)
                        {
                            return "success";
                        }
                        if (oracle_con.State == ConnectionState.Broken)
                        {
                            return "ConnectionState:Broken";
                        }
                    }
                    return "success";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
    
            /// <summary>
            /// Oracle insert,delete,update
            /// </summary>
            /// <param name="sql">insert,delete,update statement</param>
            /// <returns>Success:success + Number of affected rows; Fail:reason</returns>
            public override string Insdelupd(string sql)
            {
                try
                {
                    int num = 0;
                    if (oracle_con == null)
                    {
                        return "Please open the database connection first";
                    }
                    if (oracle_con.State == ConnectionState.Open)
                    {
                        OracleCommand oracleCommand = new OracleCommand(sql, oracle_con);
                        num = oracleCommand.ExecuteNonQuery();
                    }
                    else
                    {
                        if (oracle_con.State == ConnectionState.Closed)
                        {
                            return "Database connection closed";
                        }
                        if (oracle_con.State == ConnectionState.Broken)
                        {
                            return "Database connection is destroyed";
                        }
                    }
                    return "success" + num;
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }
            }
    
            /// <summary>
            /// Oracle insert,delete,update
            /// </summary>
            /// <param name="sql">insert,delete,update statement</param>
            /// <param name="link">link statement</param>
            /// <returns>Success:success + Number of affected rows; Fail:reason</returns>
            public override string Insdelupd(string sql, string link)
            {
                try
                {
                    int num = 0;
                    using (OracleConnection oracleConnection = new OracleConnection(link))
                    {
                        DataSet dataSet = new DataSet();
                        oracleConnection.Open();
                        OracleCommand oracleCommand = new OracleCommand(sql, oracleConnection);
                        num = oracleCommand.ExecuteNonQuery();
                        oracleConnection.Close();
                        return "success" + num;
                    }
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }
            }
    
            /// <summary>
            /// Oracle select
            /// </summary>
            /// <param name="sql">select statement</param>
            /// <param name="record">Success:success; Fail:reason</param>
            /// <returns>select result</returns>
            public override DataSet Select(string sql, out string record)
            {
                try
                {
                    DataSet dataSet = new DataSet();
                    if (oracle_con != null)
                    {
                        if (oracle_con.State == ConnectionState.Open)
                        {
                            OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(sql, oracle_con);
                            oracleDataAdapter.Fill(dataSet, "sample");
                            oracleDataAdapter.Dispose();
                            record = "OK";
                            return dataSet;
                        }
                        if (oracle_con.State == ConnectionState.Closed)
                        {
                            record = "Database connection closed";
                        }
                        else if (oracle_con.State == ConnectionState.Broken)
                        {
                            record = "Database connection is destroyed";
                        }
                    }
                    else
                    {
                        record = "Please open the database connection first";
                    }
                    record = "error";
                    return dataSet;
                }
                catch (Exception ex)
                {
                    DataSet dataSet = new DataSet();
                    record = ex.Message.ToString();
                    return dataSet;
                }
            }
    
            /// <summary>
            /// Oracle select
            /// </summary>
            /// <param name="sql">select statement</param>
            /// <param name="link">link statement</param>
            /// <param name="record">Success:success; Fail:reason</param>
            /// <returns>select result</returns>
            public override DataSet Select(string sql, string link, out string record)
            {
                try
                {
                    using (OracleConnection oracleConnection = new OracleConnection(link))
                    {
                        DataSet dataSet = new DataSet();
                        oracleConnection.Open();
                        OracleDataAdapter oracleDataAdapter = new OracleDataAdapter(sql, oracleConnection);
                        oracleDataAdapter.Fill(dataSet, "sample");
                        oracleDataAdapter.Dispose();
                        oracleConnection.Close();
                        record = "success";
                        return dataSet;
                    }
                }
                catch (Exception ex)
                {
                    DataSet dataSet = new DataSet();
                    record = ex.Message.ToString();
                    return dataSet;
                }
            }
        }
    }

    MySQL:

    /*----------------------------------------------------------------
     * 
    // author:HQ
     * 
    // describe:MySQL
     * 
    // date:2019-10-08
     * 
    ------------------------------------------------------------------*/
    using System;
    using System.Collections.Generic;
    using System.Data;              //DataSet引用集
    using MySql.Data.MySqlClient;   //MySQL引用
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AdoNet
    {
        public class MySQL : Database
        {
            private MySqlConnection mysql_con;
    
            /// <summary>
            /// MySQL open
            /// </summary>
            /// <param name="link">link statement</param>
            /// <returns>Success:success; Fail:reason</returns>
            public override string Open(string link)
            {
                try
                {
                    mysql_con = new MySqlConnection(link);
                    mysql_con.Open();
                    return "success";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
    
            /// <summary>
            /// MySQL close
            /// </summary>
            /// <returns>Success:success Fail:reason</returns>
            public override string Close()
            {
                try
                {
                    if (mysql_con == null)
                    {
                        return "No database connection";
                    }
                    if (mysql_con.State == ConnectionState.Open || mysql_con.State == ConnectionState.Connecting)
                    {
                        mysql_con.Close();
                        mysql_con.Dispose();
                    }
                    else
                    {
                        if (mysql_con.State == ConnectionState.Closed)
                        {
                            return "success";
                        }
                        if (mysql_con.State == ConnectionState.Broken)
                        {
                            return "ConnectionState:Broken";
                        }
                    }
                    return "success";
                }
                catch (Exception ex)
                {
                    return ex.Message;
                }
            }
    
            /// <summary>
            /// MySQL insert,delete,update
            /// </summary>
            /// <param name="sql">insert,delete,update statement</param>
            /// <returns>Success:success + Number of affected rows; Fail:reason</returns>
            public override string Insdelupd(string sql)
            {
                try
                {
                    int num = 0;
                    if (mysql_con == null)
                    {
                        return "Please open the database connection first";
                    }
                    if (mysql_con.State == ConnectionState.Open)
                    {
                        MySqlCommand sqlCommand = new MySqlCommand(sql, mysql_con);
                        num = sqlCommand.ExecuteNonQuery();
                    }
                    else
                    {
                        if (mysql_con.State == ConnectionState.Closed)
                        {
                            return "Database connection closed";
                        }
                        if (mysql_con.State == ConnectionState.Broken)
                        {
                            return "Database connection is destroyed";
                        }
                        if (mysql_con.State == ConnectionState.Connecting)
                        {
                            return "The database is in connection";
                        }
                    }
                    return "success" + num;
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }
            }
    
            /// <summary>
            /// MySQL insert,delete,update
            /// </summary>
            /// <param name="sql">insert,delete,update statement</param>
            /// <param name="link">link statement</param>
            /// <returns>Success:success + Number of affected rows; Fail:reason</returns>
            public override string Insdelupd(string sql, string link)
            {
                try
                {
                    int num = 0;
                    using (MySqlConnection con = new MySqlConnection(link))
                    {
                        con.Open();
                        //操作数据库的工具SqlCommand
                        MySqlCommand cmd = new MySqlCommand(sql, con);  //(操作语句和链接工具)
                        num = cmd.ExecuteNonQuery();            //执行操作返回影响行数
                        con.Close();
                        return "success" + num;
                    }
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }
            }
    
            /// <summary>
            /// MySQL select
            /// </summary>
            /// <param name="sql">select statement</param>
            /// <param name="record">Success:success; Fail:reason</param>
            /// <returns>select result</returns>
            public override DataSet Select(string sql, out string record)
            {
                try
                {
                    //储存数据的工具初始化
                    DataSet dataSet = new DataSet();
                    if (mysql_con == null)
                    {
                        record = "Please open the database connection first";
                        return dataSet;
                    }
                    if (mysql_con.State == ConnectionState.Open)
                    {
                        MySqlDataAdapter sqlDataAdapter = new MySqlDataAdapter(sql, mysql_con);
                        sqlDataAdapter.Fill(dataSet, "sample");
                        sqlDataAdapter.Dispose();
                        record = "success";
                        return dataSet;
                    }
                    if (mysql_con.State == ConnectionState.Closed)
                    {
                        record = "Database connection closed";
                        return dataSet;
                    }
                    if (mysql_con.State == ConnectionState.Broken)
                    {
                        record = "Database connection is destroyed";
                        return dataSet;
                    }
                    if (mysql_con.State == ConnectionState.Connecting)
                    {
                        record = "The database is in connection";
                        return dataSet;
                    }
                    record = "ERROR";
                    return dataSet;
                }
                catch (Exception ex)
                {
                    DataSet dataSet = new DataSet();
                    record = ex.Message.ToString();
                    return dataSet;
                }
            }
    
            /// <summary>
            /// MySQL select
            /// </summary>
            /// <param name="sql">select statement</param>
            /// <param name="link">link statement</param>
            /// <param name="record">Success:success; Fail:reason</param>
            /// <returns>select result</returns>
            public override DataSet Select(string sql, string link, out string record)
            {
                try
                {
                    //储存数据的工具初始化
                    DataSet ds = new DataSet();
                    //相当于链接数据库的一个工具类(连接字符串)
                    using (MySqlConnection con = new MySqlConnection(link))
                    {
                        con.Open();                                         //打开
                        //用SqlConnection工具链接数据库,在通过sql查询语句查询结果现存入sql适配器
                        MySqlDataAdapter sda = new MySqlDataAdapter(sql, con);  //(查询语句和连接工具)
                        sda.Fill(ds, "sample");                             //将适配器数据存入DataSet工具中
                        con.Close();                                        //用完关闭SqlConnection工具
                        sda.Dispose();                                      //手动释放SqlDataAdapter
                        record = "success";
                        return ds;
                    }
                }
                catch (Exception ex)
                {
                    DataSet dataSet = new DataSet();
                    record = ex.Message.ToString();
                    return dataSet;
                }
            }
        }
    }

    Access:

    /*----------------------------------------------------------------
     * 
    // author:HQ
     * 
    // describe:Access
     * 
    // date:2019-10-08
     * 
    ------------------------------------------------------------------*/
    using System;
    using System.Collections.Generic;
    using System.Data;          //DataSet引用
    using System.Data.OleDb;    //Access引用
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace AdoNet
    {
        public class Access
        {
            #region Access
            /// <summary>
            /// Access select
            /// </summary>
            /// <param name="sql">select statement</param>
            /// <param name="link">link statement</param>
            /// <param name="record">Success:success; Fail:reason</param>
            /// <returns>select result</returns>
            public DataSet Access_Mdb_Select(string sql, string link, out string record)
            {
                try
                {
                    DataSet dataSet = new DataSet();
                    using (OleDbConnection oleDbConnection = new OleDbConnection(link))
                    {
                        oleDbConnection.Open();
                        OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter(sql, oleDbConnection);
                        oleDbDataAdapter.Fill(dataSet, "sample");
                        oleDbDataAdapter.Dispose();
                        oleDbConnection.Close();
                        record = "success";
                        return dataSet;
                    }
                }
                catch (Exception ex)
                {
                    DataSet dataSet = new DataSet();
                    record = ex.Message.ToString();
                    return dataSet;
                }
            }
    
            /// <summary>
            /// Access insert,delete,update
            /// </summary>
            /// <param name="sql">insert,delete,update statement</param>
            /// <param name="link">link statement</param>
            /// <returns>Success:success + Number of affected rows; Fail:reason</returns>
            public string Access_Mdb_Insdelupd(string sql, string link)
            {
                try
                {
                    using (OleDbConnection oleDbConnection = new OleDbConnection(link))
                    {
                        DataSet dataSet = new DataSet();
                        oleDbConnection.Open();
                        OleDbCommand oleDbCommand = new OleDbCommand(sql, oleDbConnection);
                        int num = oleDbCommand.ExecuteNonQuery();
                        oleDbConnection.Close();
                        return "success" + num;
                    }
                }
                catch (Exception ex)
                {
                    return ex.Message.ToString();
                }
            }
            #endregion
        }
    }

    由于Access用的比较少就没做其他功能

  • 相关阅读:
    「Poetize10」封印一击
    「Poetize10」能量获取
    vijosP1499炸毁燃料库
    BZOJ3550: [ONTAK2010]Vacation
    总结#3--一类最小割问题
    BZOJ1976: [BeiJing2010组队]能量魔方 Cube
    BZOJ2132: 圈地计划
    BZOJ2127: happiness
    BZOJ1754: [Usaco2005 qua]Bull Math
    920. 会议室
  • 原文地址:https://www.cnblogs.com/mexihq/p/12467245.html
Copyright © 2020-2023  润新知