close

基本的連線類別屬性建立,好處是連線字串不會散佈在程式碼之中,以後如果有修改,不會改了這忘了那

進階一點可連字串裡的內容再拆出來管理,這樣你的程式碼就不會因為換了別的資料庫而無法使用

public class Connection
{  

    const string db63_GFERP= "Data Source=127.0.0.1\\SQL2005;Initial Catalog=GF_ERP;

                                         Persist Security Info=True;User ID=sa;Password=12345";

    const string db63_PTERP = "Data Source=127.0.0.1\\SQL2005;Initial Catalog=PT_ERP;

                                          Persist Security Info=True;User ID=sa;Password=12345";


   public string DB63_GFERP
   {
     get { return db63_GFERP; }
   }
   public string DB63_PTERP
   {
    get { return db63_PTERP; }
   }

}

 

使用方式:

Connection conn = new Connection();
dataConnection.ConnectionString = conn.DB63_PTERP;

或是

Connection conn = new Connection();

using (SqlConnection dataConn = new SqlConnection( conn.DB63_PTERP))

{

   xxx

}

arrow
arrow

    YCH 發表在 痞客邦 留言(0) 人氣()