在 C# 中使用 Datagrid 更新数据库 [英] Updating Database using Datagrid in C#

查看:15
本文介绍了在 C# 中使用 Datagrid 更新数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是在处理最近需要帮助的事情,我又卡在这里了....我正在使用数据网格从数据库中检索值,我想使用类似的数据网格更新数据库,但不知何故我遇到了麻烦,谁能指导我怎么做那..... 我的意思是当执行查询并检索所选数据时,如果用户想要更新他/她可以在显示值的数据网格上执行的操作....

just working on the something lately need help i m again stuck here.... i m retrieving values from database using datagrid and i want to update the database using that similar datagrid but somehow i m having trouble, can anyone guide me how to do that..... i mean when the query is executed and selected data is retrieved if the user wants to update something he/she can do it at the datagrid in which value is being displayed....

if (textBox1.Text != ""|| textBox1.Text==null)
        {
            textBox3.Enabled = false;
            dateTimePicker1.Enabled = false;
            dateTimePicker2.Enabled = false;
            String txt = textBox1.Text;
            dataGridView1.DataSource = null;
            dataGridView1.Rows.Clear();
            dataGridView1.Refresh();

            OleDbDataAdapter dAdapter = new OleDbDataAdapter("SELECT * FROM [BDetails] WHERE ([BranchCode] = '" + @txt + "')", connParam);
            OleDbCommandBuilder cBuilder = new OleDbCommandBuilder(dAdapter);


             dataTable = new DataTable();
            DataSet ds = new DataSet();

            dAdapter.Fill(dataTable);

            if (dataTable.Rows.Count > 0)
            {
                for (int i = 0; i < dataTable.Rows.Count; i++)
                {
                    dataGridView1.Rows.Add(dataTable.Rows[i][0], dataTable.Rows[i][1], dataTable.Rows[i][2], dataTable.Rows[i][3], dataTable.Rows[i][4], dataTable.Rows[i][5], dataTable.Rows[i][6], dataTable.Rows[i][7], dataTable.Rows[i][8], dataTable.Rows[i][11], dataTable.Rows[i][12]);

                }

            }// end inner if
            else
            {
                MessageBox.Show("No Record Found");
                textBox3.Enabled = true;
                dateTimePicker1.Enabled = true;
                dateTimePicker2.Enabled = true;
            }// end inner else


        }// end outer if
        else 
        {
            MessageBox.Show("Please Enter Branch Code");
            bookConn.Close();

        }// end outer else

我是绝望的人帮我

private void button8_Click(object sender, EventArgs e){读取数据();//保存数据();

private void button8_Click(object sender, EventArgs e) { ReadData(); // SaveData();

    }// end function

    void ReadData()
    {
        this.ds = new DataSet();
      //  string connString = "CONNICTION STRING GOES HERE";
        dAdapter = new OleDbDataAdapter("select * from BDetails", connParam);
        this.dAdapter.Fill(this.ds,"[BDetails]");
        this.ds.AcceptChanges();
        //set the table as the datasource for the grid in order to show that data in the grid
        this.dataGridView1.DataSource = ds.DefaultViewManager;
    }// end function

    void SaveData()
    {
        DataSet changes = this.ds.GetChanges();
        if (changes != null)
        {
            //Data has changes. 
            //use update method in the adapter. it should update your datasource
            int updatedRows = this.dAdapter.Update(changes);
            this.ds.AcceptChanges();
        }
    }// end function

推荐答案

Well Abeer.确实有很多方法可以处理数据,而且在很多时候,开发人员倾向于以一种或另一种方式工作.从您的问题中,我可以看到使用 ADO.NET 可能是新手,因此我建议您阅读有关在 .NET 中使用数据的一些内容(DataTables、DataSets、DataGrids、DataAdapters、Data Binding 等)

Well Abeer. there are really many ways to work with data and in a lot of times it is a developer preference to work one way or another. From your question, I can see that might be new to working with ADO.NET so I would suggest you do some reading on working with data in .NET (DataTables, DataSets, DataGrids, DataAdapters, Data Binding, ... etc)

我认为(根据我的短暂经验)从数据源读取数据和向数据源写入数据的最简单方法是使用 DataAdapter 将数据读入数据集,然后将数据集设置为 gridview 的数据源,其中用户可编辑.要写回更改,只需使用适配器中的更新方法.这是一个示例代码

I think (from my short experience) the simplest way to read and write data from and to data source is to use a DataAdapter to read the data into a dataset and then set the dataset as the datasource for a gridview where a user can edit. to write back the changes, just use the update method in the adapter. here is a sample code

DataSet ds;
OleDbDataAdapter dataAdapter;
void ReadData()
    {
        this.ds = new DataSet();
        string connString = "CONNICTION STRING GOES HERE";
        this.dataAdapter = new OleDbDataAdapter("QUERY GOES HERE", connString);
        this.dataAdapter.Fill(this.ds, "TABLE1");
        this.ds.AcceptChanges();
        //set the table as the datasource for the grid in order to show that data in the grid
        this.dataGridView1.DataSource = ds.DefaultViewManager;
    }

    void SaveData()
    {
        DataSet changes = this.ds.GetChanges();
        if (changes != null)
        {
            //Data has changes. 
            //use update method in the adapter. it should update your datasource
            int updatedRows = this.dataAdapter.Update(changes);
            this.ds.AcceptChanges();
        }
    }

请参阅以下内容,因为它提供了有关使用 DataGrid 控件的更长示例

see the following as it gives a longer sample about using DataGrid control

http://www.codeproject.com/Articles/9986/使用数据网格控件

有关 DataTable、DataSet 和 DataGrids 的一些介绍,请参阅

and for some intro on DataTable, DataSets and DataGrids see

http://www.codeproject.com/Articles/6179/A-Practical-Guide-to-NET-DataTables-DataSets-and-D

这篇关于在 C# 中使用 Datagrid 更新数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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