dataAdapter .Fill和.Update的比较

我一直在阅读MSDN资源和几个论坛,但仍然不明白这两个dataAdapter.Fill()dataAdapter.Update()之间有什么区别,我试图用它们来从我的程序更新数据库并且它有效,但是当我尝试删除update()函数时,它仍然可以正常工作,因此我认为它没用。

有人可以澄清一下吗?

编辑:这是我删除的代码:

 string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\Public\\Documents\\inventorySystem\\branches\\Database\\inventorySystemDatabase.accdb"; string query = "DELETE FROM Product WHERE product_id=" + productDataGridView[1, e.RowIndex].Value.ToString(); OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString); OleDbCommandBuilder deleteBuilder = new OleDbCommandBuilder(dAdapter); DataTable deleteTable = new DataTable(); dAdapter.Update(deleteTable); 

– 我必须做一个额外的select命令来更新datagridview –

工作样本

 using System; using System.Data; using System.Windows.Forms; using System.Data.OleDb; namespace WindowsFormsApplication1 { public partial class Form1 : Form { private OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"C:\\test.mdb\";"); private OleDbDataAdapter adapter; DataTable table = new DataTable("person"); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { con.Open(); ; adapter = new OleDbDataAdapter("select ID, p_name, p_age from person", con); adapter.Fill(table); OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter); adapter.DeleteCommand = builder.GetDeleteCommand(); adapter.UpdateCommand = builder.GetUpdateCommand(); adapter.InsertCommand = builder.GetInsertCommand(); dataGridView1.DataSource = table; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { con.Close(); con.Dispose(); } private void btnDelete_Click(object sender, EventArgs e) { DataRow[] row = table.Select("p_age = 10"); if (row.Length > 0) { for (int i = 0; i < row.Length; i++) { row[i].Delete(); } } adapter.Update(table); } } } 

简单来说。

DataAdapter.Fill()用于从数据库加载数据

示例:显示从数据库到gridview的数据

 using (DataTable table = new DataTable()) { using (OleDbDataAdapter adapter = new OleDbDataAdapter("select name,age from person", conObject)) { adapter.Fill(table); BindingSource bs = new BindingSource { DataSource = table }; dgReader.DataSource = bs; } } 

一旦编辑完成, DataAdapter.Update()就会使用底层连接将所有已更改的数据信息提交到数据库。

DataAdapter.Fill方法()

Fill方法使用由关联的SelectCommand属性指定的SELECT语句从数据源中检索行。 与SELECT语句关联的连接对象必须有效,但不需要打开它。 如果在调用Fill之前关闭连接,则会打开它以检索数据,然后关闭。 如果在调用Fill之前连接已打开,则它将保持打开状态。

然后,Fill操作将行添加到DataSet中的目标DataTable对象,如果它们尚不存在,则创建DataTable对象。 创建DataTable对象时,“填充”操作通常仅创建列名元数据。 但是,如果将MissingSchemaAction属性设置为AddWithKey,则还会创建相应的主键和约束。

DataAdapter.Update()

更新以逐行方式执行。 对于每个插入,修改和删除的行,Update方法确定对其执行的更改类型(插入,更新或删除)。 根据更改类型,执行“插入”,“更新”或“删除”命令模板以将修改的行传播到数据源。 当应用程序调用Update方法时,DataAdapter会检查RowState属性,并根据DataSet中配置的索引顺序,为每行迭代执行所需的INSERT,UPDATE或DELETE语句。 例如,由于DataTable中行的排序,Update可能会执行DELETE语句,然后执行INSERT语句,然后执行另一个DELETE语句。

应该注意的是,这些陈述不是作为批处理执行的; 每行都单独更新。 应用程序可以在必须控制语句类型序列的情况下调用GetChanges方法(例如,在UPDATE之前插入INSERT)。 有关更多信息,请参阅使用DataAdapter更新数据源(ADO.NET)。

简而言之就是定义。

DataAdapter.Fill()代表从服务器到数据库的SELECT查询语句。

 // 1 // Open connection using (SqlConnection c = new SqlConnection( Properties.Settings.Default.DataConnectionString)) { c.Open(); // 2 // Create new DataAdapter using (SqlDataAdapter a = new SqlDataAdapter("SELECT * FROM EmployeeIDs", c)) { // 3 // Use DataAdapter to fill DataTable DataTable t = new DataTable(); a.Fill(t); // 4 // Render data onto the screen // dataGridView1.DataSource = t; // <-- From your designer } } 

DataAdapter.Update()代表从服务器到数据库的UpdateInsertDelete查询语句。

 public DataSet CreateCmdsAndUpdate(DataSet myDataSet,string myConnection,string mySelectQuery,string myTableName) { OleDbConnection myConn = new OleDbConnection(myConnection); OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(); myDataAdapter.SelectCommand = new OleDbCommand(mySelectQuery, myConn); OleDbCommandBuilder custCB = new OleDbCommandBuilder(myDataAdapter); myConn.Open(); DataSet custDS = new DataSet(); myDataAdapter.Fill(custDS); //code to modify data in dataset here //Without the OleDbCommandBuilder this line would fail myDataAdapter.Update(custDS); myConn.Close(); return custDS; } 

参考:
C#SqlDataAdapter
DataAdapter.Update方法