.fill伪DataAdapter的和.Update的比较 [英] Comparison of dataAdapter .Fill and .Update

查看:210
本文介绍了.fill伪DataAdapter的和.Update的比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在读通过MSDN资源和几个论坛,还是不明白什么是这两个 DataAdapter.Fill方法()和<$ C $的区别C> dataAdapter.Update(),我试图用他们两个从我的程序更新数据库和它的作品,但是当我尝试删除更新()功能,它仍然可以正常使用,所以我认为它是无用的。

I've been reading through the MSDN resources and several forums and still don't understand what's the difference between those two dataAdapter.Fill() and dataAdapter.Update(), I tried to use both of them to update the database from my program and it works, but when I try to remove the update() function, it is still working perfectly, therefore I think of it as useless.

任何人都可以请澄清这

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

this is my code to delete:

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);



- 我要做出额外的选择命令来更新datagridview的 -

-- I have to make an extra select command to update the 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的。填写()用来加载从数据库中的数据

DataAdapter.Fill() is used to load data from database

示例:显示数据从数据库中GridView控件

Example : Showing Data From database to 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()提交所有更改的数据信息,使用底层连接的数据库。

and once the edits are done, the DataAdapter.Update() commits all the changed data information to the database using the underlying connection.

DataAdapter.Fill方法()

DataAdapter.Fill()

Fill方法使用由关联的SelectCommand属性指定的SELECT
语句中的数据源中检索行。与SELECT语句相关联的
连接对象必须是有效的,
但它并不需要是开放的。如果连接
填充被称为前关闭时,它被打开以检索数据,然后关闭。如果
连接打开填充被调用之前,它仍然开放。

The Fill method retrieves rows from the data source using the SELECT statement specified by an associated SelectCommand property. The connection object associated with the SELECT statement must be valid, but it does not need to be open. If the connection is closed before Fill is called, it is opened to retrieve data, then closed. If the connection is open before Fill is called, it remains open.

在填充操作然后添加行到目标DataTable对象
在DataSet,创建DataTable对象,如果他们不这样做已经
存在。当创建DataTable对象,填充操作正常
创建只有列名的元数据。但是,如果将MissingSchemaAction
属性被设置为AddWithKey,适当的主键和
约束还创建

The Fill operation then adds the rows to destination DataTable objects in the DataSet, creating the DataTable objects if they do not already exist. When creating DataTable objects, the Fill operation normally creates only column name metadata. However, if the MissingSchemaAction property is set to AddWithKey, appropriate primary keys and constraints are also created.

DataAdapter.Update()

DataAdapter.Update()

更新是在通过行的基础上进行的。对于每一个插入,
修改和删除的行,Update方法确定已对其执行(插入,更新或删除)
更改的类型。
根据变更的类型,插入,更新或删除命令
模板执行修改后的行传播给数据源。
当应用程序调用Update方法时,DataAdapter检查
RowState属性,并执行所需的INSERT,UPDATE或
DELETE语句反复的每一行,基于$的顺序在DataSet中配置的b $ b索引。例如,更新可能会执行
DELETE语句,其次是一个INSERT语句,然后又是
DELETE语句,由于在数据表的行的排序。

The update is performed on a by-row basis. For every inserted, modified, and deleted row, the Update method determines the type of change that has been performed on it (Insert, Update or Delete). Depending on the type of change, the Insert, Update, or Delete command template executes to propagate the modified row to the data source. When an application calls the Update method, the DataAdapter examines the RowState property, and executes the required INSERT, UPDATE, or DELETE statements iteratively for each row, based on the order of the indexes configured in the DataSet. For example, Update might execute a DELETE statement, followed by an INSERT statement, and then another DELETE statement, due to the ordering of the rows in the DataTable.

应当注意的是,这些陈述不作为批处理
处理中进行;每一行都是单独更新。应用程序可以调用的,你必须控制
语句类型的序列情况
GetChanges方法(例如,UPDATE前插入)。欲了解更多
的信息,请参阅更新数据源与DataAdapters(ADO.NET)。

It should be noted that these statements are not performed as a batch process; each row is updated individually. An application can call the GetChanges method in situations where you must control the sequence of statement types (for example, INSERT before UPDATE). For more information, see Updating Data Sources with DataAdapters (ADO.NET).

这篇关于.fill伪DataAdapter的和.Update的比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆