Updating Database using Datagrid in C#

后端 未结 1 1624
闹比i
闹比i 2020-12-10 09:12

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 simil

相关标签:
1条回答
  • 2020-12-10 10:10

    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)

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

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

    http://www.codeproject.com/Articles/9986/Using-the-DataGrid-Control

    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

    0 讨论(0)
提交回复
热议问题