将显示在datagrid中的结果保存在数据库表中 [英] Saving the result shown in datagrid in the database table

查看:60
本文介绍了将显示在datagrid中的结果保存在数据库表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经完成了以下代码,用于匹配数据库中的记录并在datagrid中显示,但是,如何将datagrid中显示的结果保存在数据库表中.

I have done the following code for matching record from database and show in datagrid but,how to save the result shown in datagrid in the database table.

private void button4_Click(object sender, EventArgs e)
            {
              
                if (txtcombo.Text == "Form ID")
                {                  
                    label1.Hide();
                    Global.Open_DataConn("doc", "conData");
                    OleDbDataAdapter da = new OleDbDataAdapter("SELECT WCDCH.formid, WCDCH.surname, WCDCH.firstname, WCDCH.midname, WCDCH.ttransid FROM WCDCH INNER JOIN bankdata ON WCDCH.formid = bankdata.formid", Global.conData);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                    label2.Show();
                    label4.Visible = true;          
                    label3.Text = ds.Tables[0].Rows.Count.ToString(); 
                    dataGridView1.Visible = true;      
                                       
                }
                else if (txtcombo.Text == "TransactionID")
                {                  
                    label2.Hide();
                    Global.Open_DataConn("doc", "conData");
                    OleDbDataAdapter da = new OleDbDataAdapter("SELECT WCDCH.formid, WCDCH.surname, WCDCH.firstname, WCDCH.midname, WCDCH.ttransid FROM WCDCH INNER JOIN bankdata ON WCDCH.ttransid = bankdata.ttransid", Global.conData);
                    DataSet ds = new DataSet();
                    da.Fill(ds);
                    dataGridView1.DataSource = ds.Tables[0];
                    label1.Show();
                    label4.Visible = true;               
                    label3.Text = ds.Tables[0].Rows.Count.ToString();                
                    dataGridView1.Visible = true;
                }
                else
                {
                    MessageBox.Show("Please,Select option which you want");
                }
          
            }                         

推荐答案

Hello Amit

用于将数据保存在数据库表中
使用插入查询,其中值将来自DataGridView单元格.


使用foreach循环迭代DataGridView行.


像:
Hello Amit

For Saving Data in database table
Use insert query in which values would be from DataGridView Cells .


Use foreach loop to iterate on DataGridView Rows .


like :
 int formId = 0 ;
string surname = "" ;
  foreach( DataGridViewRow row in yourdatagridviewName.rows)
{

   formId = Convert.ToIn32(row.Cells["Column Name / index ").Value) ;
   surname = (row.Cells["Column Name / Index ").Value).ToString();
   
   sqlquery = Insert InTo TableName (Id , Name) Values ( formId , surname) ;
   
   other function of database insert command -------- Connection obj , sql command , executeNonquery / etc .
  
   

}


此foreach循环将遍历datagridview的每一行,并将数据从单元格附加到变量,然后可以使用它们保存在Insert Query中.
希望你能做好你的工作.

还有一点,您可以通过在谷歌搜索上付出一些努力来获得这种解决方案.


this foreach loop will iterate through each row of your datagridview and will fatch data from cell to variables and then you can use them to save in Insert Query .
Hope you will do your work .

one thing more that you can get this type of solution by putting some efforts in googling .


这篇关于将显示在datagrid中的结果保存在数据库表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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