将更改更新到数据库 [英] update changes into database

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

问题描述

大家好,

请帮助我解决以下查询:

我想通过在datagridview中进行更改来更新数据库后端的表.

我想要的是,当用户在datagridview中进行更改并单击保存"或更新"按钮时,在datagridview中进行的更改应反映在databse的表中.

例如:

我有Datagridview,在WinForm上有Save按钮,在databse的后端有表EmpDetails.我用表EmpDetails填充Datagridview.当用户单击保存"按钮在Datagridview中进行更改后,在datagridview中进行的任何更改都应反映在表EmpDetails的后端.


我正在使用Visual Studio2008,将c#和MS Access作为数据库使用.

Hi all,

Please help me in solving following query:

I want to update table at backend in database throughmakeing changes in datagridview.

What I want is, when User makes changes in datagridview and clicks save or update button, the changes which are made in datagridview should get reflect in table in databse.

For ex:

I am having Datagridview, and Save button on WinForm and table EmpDetails at backend in databse. I am filling Datagridview with table EmpDetails. After making changes in Datagridview when user clicked Save button, whichever changes made in datagridview should get reflect at backend in table EmpDetails.


I am using visual studio2008 with c# and MS Access as database.

推荐答案

希望您在表"ClassAdmin"&中有一个唯一键.这应该可以工作,让我知道任何错误消息

Hope you have a unique key in the table "ClassAdmin" & this should work and let me know any error msgs

public partial class Form1 : Form
   {

       OleDbDataAdapter adpt;
       DataSet ds = new DataSet();

       public Form1()
       {
           InitializeComponent();

           try
           {

               OleDbConnection conn = new OleDbConnection("Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\\test.mdb;");
               adpt = new OleDbDataAdapter("select * from ClassAdmin Order by Class_name", conn);

               conn.Open();
               adpt.Fill(ds);
               OleDbCommandBuilder cbld = new OleDbCommandBuilder(adpt);
               dataGridView1.DataSource = ds.Tables[0]; //conn.Close();
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }


       private void button3_Click(object sender, EventArgs e)
       {
           try
           {
               adpt.Update(ds);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.Message);
           }
       }
   }


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

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