2013-03-01 105 views
7

我對如何從訪問數據庫獲取數據有點困惑。首先將它收集到列表中然後從列表中獲取這些數據是合適的,或者直接將它們存儲在數據庫中可以嗎?從Access數據庫獲取數據的正確方法

我的代碼工作得很好,但我想知道是否有更好的方法來做到這一點? :

private void button3_Click(object sender, EventArgs e) 
    { 
     OleDbConnection connection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\redgabanan\Desktop\Gabanan_Red_dbaseCon\Red_Database.accdb"); 
     connection.Open(); 
     OleDbDataReader reader = null; 
     OleDbCommand command = new OleDbCommand("SELECT * from Users WHERE LastName='"+textBox8.Text+"'", connection); 
     reader = command.ExecuteReader(); 
     listBox1.Items.Clear(); 

     while (reader.Read()) 
     { 

      listBox1.Items.Add(reader[1].ToString()+","+reader[2].ToString()); 
     } 

     connection.Close(); 

*我從數據庫中直接獲取記錄,然後將其顯示在列表框中。

+0

爲自己處理未處理的異常。 – kiran 2014-02-27 07:14:02

回答

15

的一件事就是伸出像突兀是SQL注入攻擊和使用參數化的查詢,如:

OleDbCommand command = new OleDbCommand("SELECT * from Users WHERE LastName='@1'", connection); 

command.Parameters.AddWithValue("@1", textBox8.Text) 

你做的是什麼完全可以接受的,雖然你一般會過得更好使用SQL數據庫。

編輯: 這裏是你如何從GUI單獨的業務邏輯:

Class BusLogic 
{ 
public List<string> ListboxItems = new List<string>(); 
public void PopulateListBoxItems(string userName) 
{ 
    string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\redgabanan\Desktop\Gabanan_Red_dbaseCon\Red_Database.accdb"; 
    using (OleDbConnection connection = new OleDbConnection(connString)) 
    { 
     connection.Open(); 
     OleDbDataReader reader = null; 
     OleDbCommand command = new OleDbCommand("SELECT * from Users WHERE LastName='@1'", connection);    
     command.Parameters.AddWithValue("@1", userName) 
     reader = command.ExecuteReader();  
     while (reader.Read()) 
     { 
      ListboxItems.Add(reader[1].ToString()+","+reader[2].ToString()); 
     }  
    } 
}  
} 

GUI

private void button3_Click(object sender, EventArgs e) 
{   
     var busLogic = new BusLogic(); 
     busLogic.PopulateListBoxItems(textBox8.Text);   
     \\listBox1.Items.Clear(); 
     ListboxItems.DataSource = busLogic.ListboxItems; 
} 
+0

正確,這是很容易SQL注入,更好地使用參數AddwithValue :) – Pyromancer 2013-03-01 01:05:20

+2

謝謝。這就是我想知道我的代碼是好的。我現在使用了Parametised查詢。 – 2013-03-01 01:13:17

+0

如果你沒有任何錯誤,這很好:) – Pyromancer 2013-03-01 01:15:40

1

您可能可能會將不同類中的數據訪問函數分開,或者創建通用函數來檢索記錄。

+0

所以你的意思是,我的工作沒問題? – 2013-03-01 01:05:07

+0

+1謝謝你。我已經包含代碼在我的回答 – 2013-03-01 01:14:01

2

我想說的答案是 「是」 兩者。

你現在正在做的事情對於簡單情況是完全可以接受的。請注意,它不會很好地「縮放」。也就是說,加載10或20個項目是好的。但是,如果它變成一萬或一百萬,會發生什麼?

在這種情況下,您希望查看使用模型 - 視圖 - 控制器(MVC)體系結構。這本身就是一個主題,但基本上你可以將列表框(「視圖」)與數據(「模型」)分開。

this site for a C#-centric MVC discussion

在你現在在做什麼,一個成熟的MVC架構之間,你可能只想做你的建議 - 加載列表中第一個,然後將它們添加到列表框中。如果你只加載一次,那麼你就什麼也得不到,但是如果列表被全部加載,那麼每次只需訪問一次就可以節省數據庫IO開銷。

你以爲問這個問題的事實表明你在正確的軌道上。

+0

Thanks.Appreciated much。 – 2013-03-01 01:18:11

2

雖然你的代碼工作沒有任何問題,我建議你在this example執行一些異常處理的,因爲這兩個OleDbConnection.Open()OleDbCommand.ExecuteReader()可能拋出InvalidOperationException

將連接換成using聲明也很常見,因此最終connection.close()會自動調用,但這只是個人偏好。

+0

+1謝謝你。我在我的回答中包含了證明這一點的代碼 – 2013-03-01 01:13:39

相關問題