2013-01-10 55 views
-1

我已經創建了數據庫連接類來打開,關閉並創建連接字符串。我把它命名爲db_connections。我創建了另一個名爲db_operations的類來執行所有CRUD數據庫事務。我只想聲明一次連接字符串(爲此,假設我有一個表單輸入任何數據庫連接屬性,例如:server_name,db_name等)。需要建議:使用C#連接到mysql的數據庫連接類

我所知道的所有C#都有全局變量cmiiw,我的搜索許多人都建議使用靜態變量來存儲數據。但有人告訴我,使用靜態變量是不安全的。

所有代碼都使用C#4.0。

這裏是我的連接類的代碼:

class db_connections : databases_abstract 
{ 

    private static string dbname; 
    private static string dbuser; 
    private static string dbpass; 
    private static string dbserver; 

    public MySqlConnection mysqlConn; 

    public static string DB_NAME 
    { 
     get 
     { 
      return dbname; 
     } 
     set 
     { 
      dbname = value; 
     } 
    } 

    public static string DB_USER 
    { 
     get 
     { 
      return dbuser; 
     } 
     set 
     { 
      dbuser = value; 
     } 
    } 

    public static string DB_PASSWORD 
    { 
     get 
     { 
      return dbpass; 
     } 
     set 
     { 
      dbpass = value; 
     } 
    } 

    public static string DB_SERVER 
    { 
     get 
     { 
      return dbserver; 
     } 
     set 
     { 
      dbserver = value; 
     } 
    } 


    protected override string db_make_connstring(string dbserver, string dbuser, string dbpass, string dbname) 
    { 
     //## Our connection string 
     string connString = String.Format("server={0};user id={1}; password={2}; database={3}; pooling=false", 
     dbserver, dbuser, dbpass, dbname); 

     return connString; 
    } 

    public override Boolean db_open_connection() 
    { 
     try 
     { 
      //## Initialise the connection 
      mysqlConn = new MySqlConnection(
       this.db_make_connstring(db_connections.dbserver, db_connections.dbuser, 
        db_connections.dbpass, db_connections.dbname) 
       ); 
      if (mysqlConn != null) 
      { 
       mysqlConn.Close(); 
      } 
      //## Open the connection 
      mysqlConn.Open(); 

      return true; 
     } 
     catch (Exception Ex) 
     { 
      System.Windows.Forms.MessageBox.Show(Ex.Message, "Error", 
       System.Windows.Forms.MessageBoxButtons.OK, 
       System.Windows.Forms.MessageBoxIcon.Error); 
      return false; 
     } 
    } 

    public override void db_close_connection() 
    { 
     try 
     { 
      if (mysqlConn != null) 
      { 
       mysqlConn.Close(); 
       mysqlConn.Dispose(); 
      } 
     } 
     catch(Exception Ex) 
     { 
      System.Windows.Forms.MessageBox.Show(Ex.Message, "Error", 
       System.Windows.Forms.MessageBoxButtons.OK, 
       System.Windows.Forms.MessageBoxIcon.Error); 

     } 

    } 
} 

從數據庫連接形式,我實例化類是這樣的:

db_connections db_conn = new db_connections(); 
    db_connections.DB_SERVER = txtDbServer.Text; 
    db_connections.DB_NAME = txtDbName.Text; 
    db_connections.DB_USER = txtDbUser.Text; 
    db_connections.DB_PASSWORD = txtDbPass.Text; 

    //##Just testing the connection 
    //##Once the connection succes, the database setting cannot be opened again 
    //##until the application is terminated or any really special event request 
    if (db_conn.db_open_connection() == true) 
    { 
     MessageBox.Show("Successfully connect to the database!!"); 
     this.Owner.Controls["btnUpload"].Enabled = true; 
     this.Owner.Controls["btnDb"].Enabled = false; 
     this.Close(); 
    } 

我想知道:

  1. 這是真的,使用靜態變量不安全?如果是的話,有什麼建議來重構我的代碼?

  2. 使用mysqlConn.Dispose()我的關注,在db_operations類中的每個函數裏,我只是叫db_operations類打開和關閉連接(不創建或修改connection string)。那麼足夠使用mysqlConn.Close();來關閉連接?

  3. 有什麼建議可以讓我的db_connections更安全嗎?

+0

「不安全」是什麼意思? – RBarryYoung

+0

哈哈,我不知道朋友,但有點從來沒有感到安全每次我寫代碼,我需要探索更多的每個代碼我寫,只是爲了確保我做了一個很好的代碼:D – r3d

+0

-1:你是一個問我們這是否安全。你需要澄清你的意思。 – RBarryYoung

回答

0

不,靜態不通常不安全。但是,你用它們不是用來使用它們的。例如,您創建了類db_connections的實例,然後將值分配給類db_connections的靜態屬性,然後使用該類的對象方法,然後再使用靜態屬性。靜態屬性決不會與它所聲明的類的特定對象實例相關聯。靜態屬性有點像PHP中的全局變量 - 在給定的上下文中(通常整個應用程序,每個線程也是可能的),它只存在一次。因此,您可以將配置信息存儲在某個類的靜態屬性中,但是您必須記住,您可以隨時只保留一個信息。您可以例如不爲不同的數據庫創建兩個配置。所以無論何時你改變一個靜態屬性,你都會改變它訪問該屬性的每一段代碼。

一個小例子靜態屬性:

public class TestClass 
{ 

    public static string Text1 { get; set; } 
    public string Text2 { get; set; } 

    public void WriteText1() 
    { 
     Console.WriteLine(TestClass.Text1); 
    } 

    public void WriteText2() 
    { 
     Console.WriteLine(this.Text2); 
    } 
} 



public class Program 
{ 

    public static void Main(string[] args) 
    { 
     TestClass class1 = new TestClass; 
     TestClass.Text1 = "Some Text"; 
     class1.Text2 = "More Text"; 

     class1.WriteText1(); 
     class1.WriteText2(); 

     TestClass class2 = new TestClass; 
     TestClass.Text1 = "Another Text"; 
     class2.Text2 = "And a fourth text"; 

     class2.WriteText1(); 
     class2.WriteText2(); 

     class1.WriteText1(); 
    } 
} 

輸出上是這樣的:

Some Text 
More Text 
Another Text 
And a fourth text 
Another Text 

上class1.WriteText1最後一次調用()中寫入相同的輸出class2.WriteText1。兩個對象(class1和class2)都訪問相同的靜態屬性Text1。這與Text2之類的實例屬性不同。兩個對象都包含名爲Text2的屬性,但值不同,它們是單個對象的一部分。爲單個對象更改該屬性的值,並且僅在該對象內更改該值,其他對象在具有相同屬性時保留其自己的值。

+0

嗨@Dirk Trilsbeek,無論如何都有出色的答案......
>但是你用它們的方式並不意味着要使用
我同意或者更好的使用**應用程序配置文件**來存儲這種信息?
>靜態屬性決不會連接到它們聲明的類的特定對象實例
我是sory朋友,你能更具體地解釋一下(仍然困惑)嗎? – r3d

+0

靜態屬性未連接到任何對象。如果您有一個具有屬性的對象並且您創建了第二個對象,則第二個對象的屬性是該對象的一部分。這兩個對象不共享它們的屬性,屬性值可能不同。訪問靜態屬性的兩個對象實際上訪問相同的屬性。如果一個對象改變了這些屬性,那麼對於使用它們的其他人也會改變它們。 –

+0

我在靜態屬性上添加了一個小代碼示例,我的意思是「未連接到特定對象」。 –

0

使用應用程序配置文件來存儲此類信息。如果使用asp.net,則可以使用Web.Config文件來存儲所有連接字符串。如果你正在使用Winforms,那麼你可以使用App.config來做同樣的事情。

你可以閱讀更多關於connectionStrings節在這裏:http://msdn.microsoft.com/en-us/library/ms254494.aspx

而你只是用ConfigurationManager中類爲使訪問這些信息:

MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["DBConnString"].ConnectionString); 

這也是一個很好的來源,學習如何定義您的連接字符串適用於所有類型的數據庫和驅動程序:http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config

+0

hey @lcarus,thx快速回復:D,對於一個很好的鏈接課程:D – r3d