2013-05-04 318 views
2

我目前正在使用Windows Forms和SQLite创建一个小应用程序。阅读一些教程后,我实现了数据检索这个方法:使用SQLiteDataAdapter检索/插入数据

public DataTable GetDataTable(ref SQLiteDataAdapter adapter, string sql) 
     { 
      DataTable dt = new DataTable(); 

      // Connect to database. 
      using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 
      // Create database adapter using specified query 
      using (adapter = new SQLiteDataAdapter(sql, connection)) 
      // Create command builder to generate SQL update, insert and delete commands 
      using (SQLiteCommandBuilder command = new SQLiteCommandBuilder(adapter)) 
      { 
       // Populate datatable to return, using the database adapter     
       adapter.Fill(dt); 
      } 
      return dt; 
     } 

(除了其他GetDataTable不采取SQLiteDataAdapter作为参数)

我有三个班,我们姑且称之为UI,链接和数据库。除了在用户交互时显示数据和引发事件之外,UI什么也不做。该链接创建数据库和SQLiteDataAdapter,通过上述方法检索数据表,并将其绑定到UI上的数据网格视图。用户不能通过数据网格视图更改表格,但应通过一些文本框来完成。 (这是否使表绑定到dgv obosolete?)

什么是使用适配器从文本框到数据库的用户输入的最佳方式是什么?或者我应该使用DataReader和一些插入方法,而不是一个适配器?

据了解,UI通过Get方法公开其控件。有更好的解决方案吗?

private void Initialize() 
{ 
    // Subscribe to userInterface events 
    userInterface.DataGridViewSelectionChanged += new EventHandler(userInterface_DataGridViewSelectionChanged); 
    userInterface.NewClicked += new EventHandler(userInterface_NewClicked); 
    userInterface.SaveClicked += new EventHandler(userInterface_SaveClicked); 

    // Get dataGridView from userInterface and bind to database 
    bindingSource = new BindingSource(); 
    bindingSource.DataSource = database.GetDataTable(ref adapter, "SELECT * FROM SomeTable"); 
    userInterface.GetDataGridView().DataSource = bindingSource; 
} 

void userInterface_DataGridViewSelectionChanged(object sender, EventArgs e) 
{ 
    if (userInterface.GetDataGridView().SelectedRows.Count != 0) 
    { 
     DataGridViewRow row = userInterface.GetDataGridView().SelectedRows[0]; 
     userInterface.GetIDTextBox().Text = row.Cells["PrimaryKey].Value.ToString(); 
     userInterface.GetOtherIDTextBox().Text = row.Cells["ForeignKey"].Value.ToString(); 

     DataTable dt = database.GetDataTable("SELECT * from SomeTable WHERE ForeignKey=" + row.Cells["ForeignKey"].Value); 
     userInterface.GetLastNameTextBox().Text = dt.Rows[0]["LastName"].ToString(); 
     userInterface.GetFirstNameTextBox().Text = dt.Rows[0]["FirstName"].ToString(); 
     userInterface.GetCompanyTextBox().Text = dt.Rows[0]["Company"].ToString(); 
    }    
} 

void userInterface_NewClicked(object sender, EventArgs e) 
{ 
    // Get all text boxes and clear them 
    // Let the UI take care of this by itself?      
} 

void userInterface_SaveClicked(object sender, EventArgs e) 
{ 
     // Get text/data from all text boxes and insert (or update if editing table) into database 
     // adapter.Update(...)? 
} 

干杯!

回答

3

INSERT,UPDATE和DELETE操作是DbCommand的工作。你需要一个不同的方法,它使用sql字符串和用于INSERT的SQLiteParameter集合。

我会尽量写一些伪代码INSERT操作

public class MyHelperClass 
{ 
    public static int InsertCommand(string sql, SQLiteParameter[] parameters) 
    { 
     int result = 0; 
     using (SQLiteConnection connection = new SQLiteConnection(connectionString)) 
     using (SQLiteCommand cmd = new SQLiteCommand(sql, connection)) 
     { 
      cmd.Parameters.AddRange(parameters); 
      result = cmd.ExecuteNonQuery(); 
     } 
     return result; 
    } 
} 

现在你必须建立参数数组传递给帮助方法,这应该从你的UI代码来完成

string sqlCommand = "INSERT INTO table1 (FirstName, LastName) VALUES (@fName, @lName)"; 
SQLiteParameter[] p = new SQLiteParameter[2]; 
p[0] = new SQLiteParameter("@fName", TextBox1.Text); 
p[1] = new SQLiteParameter("@lName", TextBox2.Text); 
int rowAdded = MyHelperClass,InsertCommand(sql, p); 

UPDATE和DELETE命令的操作是相似的。另外,我建议您添加一个GetDataTable版本,该版本接受参数数组而不是使用字符串连接构建sql命令。在这里重复无数次字符串连接导致错误,最糟糕的是,易于暴露给sql注入的弱代码。

+0

感谢您的快速响应。如果我创建自己的INSERT,UPDATE和DELETE方法,对我来说使用SQLiteDataAdapter更有意义吗?如果DataGridView只显示表格,但通过一些文本框改变它,这是否会使数据从数据库绑定到DGV过时? – Lahey 2013-05-04 22:54:28

+0

那么,在这种情况下,dataadapter是无用的,但你可以在将来找到有用的。如果你正在构建一个基础库,以便在不同的项目中重用,我将尽力保留这个方法 – Steve 2013-05-04 23:16:54