.net(C#数据库访问) Mysql,Sql server,Sqlite,Access四种数据库的连接方式

便签记录Mysql,Sql server,Sqlite,Access四种数据库的简单连接方式

//using MySql.Data.MySqlClient; 
#region  执行简单SQL语句,使用MySQL查询
        static string  strConn = "server=.;database=Data20180608;uid=sa;pwd=123456;integrated Security=SSPI;persist Security info=false;";
        /// 
        /// 执行SQL语句,返回影响的记录数
        /// 
        /// SQL语句
        /// 影响的记录数
        public static int MyExecuteSql(string SQLString)
        {
            using (MySqlConnection connection = new MySqlConnection(strConn))
            {
                using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        connection.Close();
                        throw e;
                    }
                }
            }
        }
        /// 
        /// 执行查询语句,返回DataTable
        /// 
        /// 查询语句
        /// DataSet
        public static DataTable MyQuery(string SQLString)
        {
            using (MySqlConnection connection = new MySqlConnection(strConn))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds.Tables[0];
            }
        }
        #endregion
Mysql

 

 #region  执行简单SQL语句,使用SQL SERVER查询
        static string strConn = "Data Source=.;database=Data20180608;User id=root;Password=123456;pooling=false;CharSet=utf8;port=3306;";

        /// 
        /// 执行SQL语句,返回影响的记录数
        /// 
        /// SQL语句
        /// 影响的记录数
        public static int ExecuteSql(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(strConn))
            {
                using (SqlCommand cmd = new SqlCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        connection.Close();
                        throw e;
                    }
                }
            }
        }
        /// 
        /// 执行查询语句,返回DataTable
        /// 
        /// 查询语句
        /// DataSet
        public static DataTable Query(string SQLString)
        {
            using (SqlConnection connection = new SqlConnection(strConn))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SqlDataAdapter command = new SqlDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (System.Data.SqlClient.SqlException ex)
                {
                    throw new Exception(ex.Message);
                }
                return ds.Tables[0];
            }
        }
        #endregion
Sql server

 

//using System.Data.OleDb;
 public static string OtherPAth = "";
        public static string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=";
       public static string Sql = String.Empty;
        #region  执行简单SQL语句,使用OleDb查询

        /// 
        /// 执行SQL语句,返回影响的记录数
        /// 
        /// SQL语句
        /// 影响的记录数
        public static int ExecuteSql(string SQLString)
        {
            using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth ))
            {
                using (OleDbCommand cmd = new OleDbCommand(SQLString, connection))
                {
                    try
                    {
                        connection.Open();
                        int rows = cmd.ExecuteNonQuery();
                        return rows;
                    }
                    catch (System.Data.SqlClient.SqlException e)
                    {
                        connection.Close();
                        throw e;
                    }
                }
            }
        }

      


        /// 
        /// 执行查询语句,返回DataTable
        /// 
        /// 查询语句
        /// DataSet
        public static DataTable Query(string SQLString)
        {
            using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    OleDbDataAdapter command = new OleDbDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                return ds.Tables[0];
            }
        }

        /// 
        /// 执行查询语句,返回DataTable
        /// 
        /// 查询语句
        /// DataSet
        public static DataTable QueryDataName()
        {
            using (OleDbConnection connection = new OleDbConnection(strConn + OtherPAth))
            {
                DataTable ds = new DataTable();
                try
                {
                    connection.Open();
                    ds = connection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show(ex.Message);
                }
                return ds;
            }
        }

        #endregion
Access

 

 public static string strConn = @"Data Source=" + @"F:\资料文档\20190227\CAD\0625AnPin\CadDLLcl\Data\Cad_try0626.db";
        public static string Sql = String.Empty;

        /// 
        /// 执行SQL语句,返回影响的记录数
        /// 
        /// SQL语句
        /// 影响的记录数
        public static int ExecuteSql(string SQLString)
        {
            using (SQLiteConnection connection = new SQLiteConnection(strConn))
            {
                //事务
                using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    using (SQLiteCommand cmd = new SQLiteCommand(SQLString, connection))
                    {
                        try
                        {
                            connection.Open();
                            cmd.Transaction = singleTrans;
                            int rows = cmd.ExecuteNonQuery();
                            singleTrans.Commit();
                            return rows;
                        }
                        catch (System.Data.SqlClient.SqlException e)
                        {
                            connection.Close();
                            singleTrans.Rollback();
                            throw e;
                        }
                    }
                }
            }
        }

        /// 
        /// 执行查询语句,返回DataTable
        /// 
        ///  查询语句 
        /// < returns > DataSet 
        public static DataTable Query(string SQLString)
        {
            using (SQLiteConnection connection = new SQLiteConnection(strConn))
            {
                DataSet ds = new DataSet();
                try
                {
                    connection.Open();
                    SQLiteDataAdapter command = new SQLiteDataAdapter(SQLString, connection);
                    command.Fill(ds, "ds");
                    return ds.Tables[0];
                }
                catch (Exception ex)
                {
                    connection.Close();
                    throw ex;
                }
            }
        }


        /// 
        /// 执行存储过程,返回影响的记录数
        /// 
        /// SQL语句
        /// 影响的记录数
        public static int ExecuteProc(string procName, SQLiteParameter[] coll)
        {
            using (SQLiteConnection connection = new SQLiteConnection(strConn))
            {
                //事务
                using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    using (SQLiteCommand cmd = new SQLiteCommand(connection))
                    {
                        try
                        {
                            connection.Open();
                            for (int i = 0; i < coll.Length; i++)
                            {
                                cmd.Parameters.Add(coll[i]);
                            }
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = procName;
                            cmd.Transaction = singleTrans;
                            int rows = cmd.ExecuteNonQuery();
                            singleTrans.Commit();
                            return rows;
                        }
                        catch (System.Data.SqlClient.SqlException e)
                        {
                            connection.Close();
                            singleTrans.Rollback();
                            throw e;
                        }
                    }
                }
            }
        }


        /// 
        /// 执行带参数的SQL语句,返回影响的记录数
        /// 
        /// SQL语句
        /// 影响的记录数
        public static int ExecuteSqlPar(string sqlPar, SQLiteParameter[] coll)
        {
            using (SQLiteConnection connection = new SQLiteConnection(strConn))
            {
                //事务
                using (SQLiteTransaction singleTrans = connection.BeginTransaction(IsolationLevel.ReadCommitted))
                {
                    using (SQLiteCommand cmd = new SQLiteCommand(connection))
                    {
                        try
                        {
                            connection.Open();
                            for (int i = 0; i < coll.Length; i++)
                            {
                                cmd.Parameters.Add(coll[i]);
                            }
                            cmd.CommandType = CommandType.Text;
                            cmd.CommandText = sqlPar;
                            cmd.Transaction = singleTrans;
                            int rows = cmd.ExecuteNonQuery();
                            singleTrans.Commit();
                            return rows;
                        }
                        catch (System.Data.SqlClient.SqlException e)
                        {
                            connection.Close();
                            singleTrans.Rollback();
                            throw e;
                        }
                    }
                }
            }
        }
Sqlite

 

你可能感兴趣的:(.net(C#数据库访问) Mysql,Sql server,Sqlite,Access四种数据库的连接方式)