可以切換資料庫的SqlHelper

來源:互聯網
上載者:User

完整代碼:/Files/greatverve/MyFiles.rar
一個項目我一般只用一個資料庫,現在有需求,需要使用多個資料庫,臨時改了一下,不知道好不好,
請高手指教,大家使用多資料庫的時候是如何切換的。
DBHelper.cs

private static string connectionString = "SQLCONNECTIONSTRING";
/// <summary>
/// 切換資料庫
/// </summary>
/// <param name="connStr"></param>
public static void SetSqlConnection(string connStr)
{
    connectionString = connStr;
}
/// <summary>
/// 返回資料庫連接字串
/// </summary>
/// <returns></returns>
public static String GetSqlConnection()
{
    String conn = ConfigurationManager.AppSettings[connectionString].ToString();
    return conn;
}
/// <summary>
/// 執行SQL語句
/// </summary>
/// <param name="Sqlstr">SQL語句</param>
/// <param name="param">參數對象數組</param>
/// <returns></returns>
public static int ExecuteSql(String Sqlstr, SqlParameter[] param)
{
    String ConnStr = GetSqlConnection();
    using (SqlConnection conn = new SqlConnection(ConnStr))
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = Sqlstr;
        cmd.Parameters.AddRange(param);
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        return 1;
    }
}

Test.cs(BLL)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Configuration;
using System.Web;
using HY.Common;
using System.Text;
using System.Data.SqlClient;

namespace HY.BLL
{
    public class Test
    {
        /// <summary>
        /// 在靜態類的靜態建構函式裡,切換預設資料庫
        /// </summary>
        static Test()
        {
            DBHelper.SetSqlConnection("SQLCONNECTIONSTRING");
        }
        /// <summary>
        /// 不需要切換資料庫的,直接寫
        /// </summary>
        /// <returns></returns>
        public static DataTable getAll()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from t_Users");
            return DBHelper.ExecuteDt(sb.ToString());
        }
        /// <summary>
        /// 需要切換的話,函數中切換
        /// </summary>
        /// <returns></returns>
        public static DataTable getTable()
        {
            DBHelper.SetSqlConnection("SQLCONNECTIONSTRING1");//BLL中這樣切換
            StringBuilder sb = new StringBuilder();
            sb.Append("select * from t_User");
            return DBHelper.ExecuteDt(sb.ToString());
        }
    }
}

web.config

<appSettings>
  <add key="SQLCONNECTIONSTRING" value="data source=(local);uid=sa;pwd=sa;database=db#test"/>
  <add key="SQLCONNECTIONSTRING1" value="data source=(local);uid=sa;pwd=sa;database=hyoa_SysInfo"/>
</appSettings>

SqlTest.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        Bind();
}
private void Bind()
{
    //切換資料庫的操作封裝在BLL中
    GridView1.DataSource = Test.getAll();
    GridView1.DataBind();
    //切換資料庫
    GridView2.DataSource = Test.getTable();
    GridView2.DataBind();
}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.