2013-08-16 55 views
0

我有一個關於從一種方法調用數據到另​​一個方法的問題。檢索在一個空白中創建的數據列表並在另一個空白中顯示

基本上我已經致力於凝結和簡化我的代碼。在這種情況下,我連接到.MDB文件並從中讀取1位數據。最初我在所有需要的地方都有連接字符串,但是在學習了越來越多關於課程和內容的東西之後,我被告知複製和粘貼是不好的。所以我想把我的連接字符串,並使它...它是自己的無效,然後調用它所需的點變量。問題是如何返回從數據庫檢索到的數據列表?這裏是代碼,因爲我現在可以正常工作。

string directory = @"C:\"; 
      string[] folders = Directory.GetDirectories(directory, "Rameses-*"); 
      List<string> dataList = new List<string>(); 
      foreach (string foldername in folders) 
      { 
       using (var conection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=" + foldername + "\\Program\\random.mdb;Jet OLEDB:Database Password=******")) 
       { 
        conection.Open(); 
        var query = "Select u_company From t_user"; 
        var command = new System.Data.OleDb.OleDbCommand(query, conection); 
        var reader = command.ExecuteReader(); 
        while (reader.Read()) 
        { 
         dataList.Add(reader[0].ToString()); 
        } 
        reader.Close(); 
        conection.Close(); 
       } 
      } 
      error_handling(" Alternate Profiles Loaded into Selector"); 
      profselect.DataSource = dataList; 
      profselect.SelectedText = dataList.Last(); 

,但我認爲我能拿出的這是目前現貨連接,但是做這樣的事情,現在我無法從我的下拉inital連接獲取數據列表。有什麼建議麼?

 //Database Connection Base Function 
    public static void database_link(string query, string foldername) 
    { 
     List<string> dataList = new List<string>(); 
     using (var conection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;" + "data source=" + foldername + "\\Program\\Random.mdb;Jet OLEDB:Database Password=****")) 
     { 
      conection.Open(); 
      var command = new System.Data.OleDb.OleDbCommand(query, conection); 
      var reader = command.ExecuteReader(); 
      while (reader.Read()) 
      { 
       dataList.Add(reader[0].ToString()); 
      } 
      reader.Close(); 
      conection.Close(); 
     } 
    } 

    // Ramdata check and load from files and database 
    private void ramdata_check(object sender, EventArgs e) 
    { 
     try 
     { 
      string directory = @"C:\"; 
      string[] folders = Directory.GetDirectories(directory, "Myapp-*"); 
      List<string> dataList = new List<string>(); 
      foreach (string foldername in folders) 
      { 
       database_link("Select u_company From t_user", foldername); 
      } 
      error_handling(" Alternate Profiles Loaded into Selector"); 
      profselect.DataSource = dataList; 
      profselect.SelectedText = dataList.Last(); 
     } 
     catch 
{} 

回答

0

不要使共享連接調用爲空:使其創建連接,打開並返回它。 A using block將在完成時爲您關閉連接,因此您無需手動關閉連接:

public OleDbConnection OpenConnection() 
{ 
     var result = new OleDbConnection("My special connection string here")) 
    result.Open() 
    return result; 
} 

public List<string> void DoSomethingWithConnection() 
{ 
    using (var connection = OpenConnection()); 
    { 
     List<string> dataList = new List<string>(); 

     OleDbCommand command = new OleDbCommand("DoSomethingHere", connection); 
     var reader = command.ExecuteReader(); 
     while (reader.Read()) 
     { 
      dataList.Add(reader[0].ToString()); 
     } 

     return dataList; 
    // The connection is automatically closed when the 
    // code exits the using block. 
    } 
} 
+0

優秀。我改變了一下,但你的雖然有幫助我!謝謝 –

相關問題