C#如何将datagridview中的更改保存到sql? [英] C# how to save changes in datagridview to sql?

查看:221
本文介绍了C#如何将datagridview中的更改保存到sql?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何保存对datagridview所做的更改.贝娄是我到目前为止的代码.

Hi, how do I save changes been made on datagridview. Bellow is the code have I have so far.

private void refreshInvoice() //Load all Invoice result.
{
    SqlDataAdapter dataAdapter = new SqlDataAdapter("select ID_Invoice, Description_Invoice, Amount_Invoice, Invoice_Date_Invoice, Invoice_Note, Invoice_Number_Invoice, Invoice_Status, Paid_Invoice_Number, Supplier_Invoice, Upload_Date_Invoice  from StoringSystemInvoiceTable", connectionString);
    SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
    //new data table
    DataTable table = new DataTable();
    table.Locale = CultureInfo.InvariantCulture;
    dataAdapter.Fill(table);
    storingSystemContractTableBindingSource.DataSource = table;
    //resize the DataGridView columns to fit newly loaded content.
    storingSystemContractTableDataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);

    //user can resize column and add rows
    storingSystemContractTableDataGridView.AllowUserToResizeColumns = true;
    storingSystemContractTableDataGridView.AllowUserToAddRows = true;
    //storingSystemContractTableBindingNavigator.BindingSource();
    storingSystemContractTableDataGridView.DataSource = storingSystemContractTableBindingSource;
}

推荐答案

最初,您将通过使用以下代码来获得


Hi Initially you will get the by using below code


DataSet ds = new DataSet();

    static SqlConnection con = new SqlConnection("user id=sa;password=just4fun;database=sdp;data source=sitt244");

    static SqlCommand cmd = new SqlCommand("select * from employee ", con);



SqlDataAdapter da = new SqlDataAdapter(cmd);


private void GetData()
       {
           dataGridView1.Visible = true;
           da.Fill(ds,"e");
           dataGridView1.DataSource = ds.Tables["e"];
           MessageBox.Show("You got the data");
       }


       private void button1_Click(object sender, EventArgs e)
       {
           GetData();
           
       }



如果要插入数据或保存更改,请在"Savechanges"按钮中编写以下代码,然后点击



If you want to insert data or save changes then write the below code in "Savechanges" button click

SqlCommandBuilder cmd = new SqlCommandBuilder(da);
           try
           {
               da.Update(ds, "e");
               MessageBox.Show("Saved");


           }
           catch (Exception ee)
           {
               MessageBox.Show(ee.Message);
           }




步骤-1获取数据集
步骤-2使用适当的选择语句填充数据集
步骤-3将数据集关联到Gridview(AllowAddNewRow属性为True)

更新现有数据
如果更新现有数据,则使用sqladapter对象的Update()方法.

插入新数据
首先在数据表中添加新记录,然后调用方法(UPDATE())
Hi

Step - 1 Take Dataset
Step - 2 Fill Dataset using appropriate select statment
Step - 3 Assgin dataset to Gridview(AllowAddNewRow Property to True)

Update Existing Data
if you update existing data then use Update() method of sqladapter object.

Insert new data
First Add new Record in datatable then Call Method (UPDATE())


这篇关于C#如何将datagridview中的更改保存到sql?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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