.net(C#數據庫訪問) Mysql,Sql server,Sqlite,Access四種數據庫的鏈接方式

便籤記錄Mysql,Sql server,Sqlite,Access四種數據庫的簡單鏈接方式web

//using MySql.Data.MySqlClient; 
#region  執行簡單SQL語句,使用MySQL查詢
        static string  strConn = "server=.;database=Data20180608;uid=sa;pwd=123456;integrated Security=SSPI;persist Security info=false;";
        /// <summary>
        /// 執行SQL語句,返回影響的記錄數
        /// </summary>
        /// <param name="SQLString">SQL語句</param>
        /// <returns>影響的記錄數</returns>
        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;
                    }
                }
            }
        }
        /// <summary>
        /// 執行查詢語句,返回DataTable
        /// </summary>
        /// <param name="SQLString">查詢語句</param>
        /// <returns>DataSet</returns>
        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;";

        /// <summary>
        /// 執行SQL語句,返回影響的記錄數
        /// </summary>
        /// <param name="SQLString">SQL語句</param>
        /// <returns>影響的記錄數</returns>
        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;
                    }
                }
            }
        }
        /// <summary>
        /// 執行查詢語句,返回DataTable
        /// </summary>
        /// <param name="SQLString">查詢語句</param>
        /// <returns>DataSet</returns>
        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查詢

        /// <summary>
        /// 執行SQL語句,返回影響的記錄數
        /// </summary>
        /// <param name="SQLString">SQL語句</param>
        /// <returns>影響的記錄數</returns>
        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;
                    }
                }
            }
        }

      


        /// <summary>
        /// 執行查詢語句,返回DataTable
        /// </summary>
        /// <param name="SQLString">查詢語句</param>
        /// <returns>DataSet</returns>
        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];
            }
        }

        /// <summary>
        /// 執行查詢語句,返回DataTable
        /// </summary>
        /// <param name="SQLString">查詢語句</param>
        /// <returns>DataSet</returns>
        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;

        /// <summary>
        /// 執行SQL語句,返回影響的記錄數
        /// </summary>
        /// <param name="SQLString">SQL語句</param>
        /// <returns>影響的記錄數</returns>
        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;
                        }
                    }
                }
            }
        }

        /// <summary>
        /// 執行查詢語句,返回DataTable
        /// </summary>
        /// <param name = "SQLString" > 查詢語句 </ param >
        /// < returns > DataSet </ returns >
        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;
                }
            }
        }


        /// <summary>
        /// 執行存儲過程,返回影響的記錄數
        /// </summary>
        /// <param name="SQLString">SQL語句</param>
        /// <returns>影響的記錄數</returns>
        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;
                        }
                    }
                }
            }
        }


        /// <summary>
        /// 執行帶參數的SQL語句,返回影響的記錄數
        /// </summary>
        /// <param name="SQLString">SQL語句</param>
        /// <returns>影響的記錄數</returns>
        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
相關文章
相關標籤/搜索