更新从datagrid到数据库的更改(使用我的代码) [英] update changes from datagrid to database (using my code)

查看:73
本文介绍了更新从datagrid到数据库的更改(使用我的代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我使用它来获取从SQL填充的数据网格:



< pre lang =c#> private void button1_Click( object sender,EventArgs e)
{
SqlConnection con = new SqlConnection( 数据源= SERVER; +
初始目录= Partner_database; +
Integrated Security = True);
SqlCommand command = new SqlCommand( select *来自[dbo]。[Test_table],con);

SqlDataAdapter sda = new SqlDataAdapter(command);
DataSet set_date = new DataSet( cucu1 );
sda.Fill(set_date, cucu1);
dataGridView1.DataSource = set_date;
dataGridView1.DataMember = cucu1;
}





我尝试了一些其他问题的回复,但没有让它在我的代码上运行。



我需要的是有一个按钮将更改保存回数据库,如:

  private   void  button2_Click( object  sender,EventArgs e)
{
set .AcceptChanges();

}





我似乎无法找到解决方案。

谢谢为了你的帮助。



Danut

解决方案

请参考此链接



添加,编辑和删除在DataGridView中使用分页 [ ^ ]


您有n行列数和n行数,因此您需要做的是



  int  i =  0 ; 

while (!(i == dgv.rows.count))
{

< span class =code-keyword> string sql = @ ; // 构建你的动态查询只是根据行数来更新查询
sqlhelp(sql);
i ++;
}


private Datatable SqlHelp( string sql)
{
SqlConnection con = new SqlConnection( 数据源= SERVER; +
初始目录= Partner_database ; +
Integrated Security = True);
SqlCommand command = new SqlCommand(sql,con);

SqlDataAdapter sda = new SqlDataAdapter(command);
DataSet set_date = new DataSet( cucu1 );
sda.Fill(set_date, cucu1);
return set_date.table( 0 );
}


请遵循本教程:

Asp.net在gridview中插入,编辑,更新,删除数据 [ ^ ]



-KR


Hello,

I am using this to get a datagrid populated from SQL:

private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=SERVER;" +
                                              "Initial Catalog=Partner_database;" +
                                              "Integrated Security=True");
        SqlCommand command = new SqlCommand("select * from [dbo].[Test_table]", con);

        SqlDataAdapter sda = new SqlDataAdapter(command);
        DataSet set_date = new DataSet("cucu1");
        sda.Fill(set_date,"cucu1");
        dataGridView1.DataSource = set_date;
        dataGridView1.DataMember = "cucu1";
    }



I tried some of the replies to other questions but did not make it run on my code.

What i need is to have a button to save the changes back to the database like:

private void button2_Click(object sender, EventArgs e)
   {
     this.set.AcceptChanges();
   
   }



I cannot seem to find a solution.
Thank you for your help.

Danut

解决方案

please refer this link

Add, Edit, and Delete in DataGridView with Paging[^]


You have n number of columns and n number of rows so what you will have to do is

int i =0;

while (!(i == dgv.rows.count))
{

string sql = @"";//build ur dynamic query just update query according to the no of rows
sqlhelp(sql);
i++;
}


private Datatable SqlHelp(string sql)
{
SqlConnection con = new SqlConnection("Data Source=SERVER;" +
                                              "Initial Catalog=Partner_database;" +
                                              "Integrated Security=True");
        SqlCommand command = new SqlCommand(sql, con);
 
        SqlDataAdapter sda = new SqlDataAdapter(command);
        DataSet set_date = new DataSet("cucu1");
        sda.Fill(set_date,"cucu1");
return set_date.table(0);
}


Please follow this tutorial :
Asp.net insert, Edit, update, delete data in gridview[^]

-KR


这篇关于更新从datagrid到数据库的更改(使用我的代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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