1. 程式人生 > >C#通過讀取配置檔案方式連線mysql資料庫

C#通過讀取配置檔案方式連線mysql資料庫

連線資料庫最常用的方式就是通過字串連線,如下:

public static string connstr = "server = localhost; uid = root; pwd = 123456; database = db_sms;";

 MySqlConnection conn = new MySqlConnection(connstr);
 MySqlCommand comm = new MySqlCommand(sqlstr, conn);

但是這種方式不利於應用程式釋出後的後期維護。

所有便通過另外一種方式進行連線——通過讀取配置檔案App.config

(1)首先進行編寫App.config:

<configuration>
  <connectionStrings>
    <add name ="connstr" connectionString ="server = localhost; uid = root; pwd = 123456; database = db_sms;"/>
  </connectionStrings>
</configuration>

一定要在 configuration 節點中進行編輯,寫入連線資料庫的字串。

(2)新增引用 System.Configuration

(3)使用ConfigurationManger類:

 public static string connstr = ConfigurationManager.ConnectionStrings["connstr"].ToString();

接下來就是常規操作(增刪改查)

注意事項:

(1)以下程式碼必須嚴格編寫,不能有錯,即使一個字元的錯誤(多一個字元,少一個字元,大小寫)都會導致配置檔案讀取失敗。

<configuration>
  <connectionStrings>
    <add name ="" connectionString =""/>
  </connectionStrings>
</configuration>

(2)資料庫連線字串的名稱(這裡是“connstr”),在App.config中和程式碼中必須保持一致。

附上資料庫操作類的完整程式碼:

  1. using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Data;
    using MySql.Data;
    using MySql.Data.MySqlClient;
    using System.Configuration;
    
    namespace DAL
    {
        public class SQLHelper
        {
            //public static string connstr = "server = localhost; uid = root; pwd = 123456; database = db_sms;";
    
            /*
             1.在 SMS 下的 App.config 配置檔案中新增:
             <connectionStrings>
                <add name ="connstr" connectionString ="server = localhost; uid = root; pwd = 123456; database = db_sms;"/>
             </connectionStrings>
    
             2.在 DAL 下新增引用:System.Configuration
    
             3.新增名稱空間,使用 ConfigurationManager 類下的 ConnectionStrings
             */
    
            // 通過讀取配置檔案進行資料庫連線
            public static string connstr = ConfigurationManager.ConnectionStrings["connstr"].ToString();
    
            /// <summary>
            /// 執行增、刪、改
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <returns></returns>
            private static int UpDate(string sqlstr)
            {
                MySqlConnection conn = new MySqlConnection(connstr);
                MySqlCommand comm = new MySqlCommand(sqlstr, conn);
                try
                {
                    conn.Open();
                    int result = comm.ExecuteNonQuery();
                    return result;
                }
                catch (System.Exception ex)
                {
                    // 寫入系統日誌
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
    
            /// <summary>
            /// 查詢:獲取單一結果
            /// </summary>
            /// <param name="sql"></param>
            /// <returns></returns>
            public static object GetSingleResult(string sqlstr)
            {
                MySqlConnection conn = new MySqlConnection(connstr);
                MySqlCommand comm = new MySqlCommand(sqlstr, conn);
                try
                {
                    conn.Open();
                    object result = comm.ExecuteScalar();
                    return result;
                }
                catch (System.Exception ex)
                {
                    // 寫入系統日誌
                    throw ex;
                }
                conn.Close();
            }
    
            /// <summary>
            /// 查詢:獲取結果集合
            /// </summary>
            /// <param name="sqlstr"></param>
            /// <returns></returns>
            public static MySqlDataReader GetDataReader(string sqlstr)
            {
                MySqlConnection conn = new MySqlConnection(connstr);
                MySqlCommand comm = new MySqlCommand(sqlstr, conn);
                try
                {
                    conn.Open();
                    return comm.ExecuteReader(CommandBehavior.CloseConnection);
                }
                catch (System.Exception ex)
                {
                    // 寫入系統日誌
                    conn.Clone();
                    throw ex;
                }
                finally
                {
                    conn.Close();
                }
            }
        }
    }