首页 \ 问答 \ 如何使用Datagridview绑定源C#更新SQL Server数据库(How to update SQL Server database using Datagridview binding source C#)

如何使用Datagridview绑定源C#更新SQL Server数据库(How to update SQL Server database using Datagridview binding source C#)

我正在用C#编写Winforms应用程序,使用户可以使用datagridview编辑和更新数据库。

问题是,我无法让它发挥作用。 我设法实现的唯一目标是更新datagridview显示的内容,但是当我进入数据库表时,数据没有变化。 我搜索了很多网站讨论这个问题,但没有什么对我有用。

到目前为止我尝试过的事情:

  • 将数据库绑定到datagridview
  • 将“ Copy to Output Directory属性更改为Copy if newer
  • 使用dataAdapter.Update((DataTable)bindingSource1.DataSource)执行或不执行任何更新查询。
  • 在没有dataAdapter.update(...)情况下执行更新查询。

这是我的代码:

public Form1()
{
    InitializeComponent();
    GetData("SELECT * FROM Table1");
}

void GetData(string selectCommand)
{
    SqlConnection conn = new SqlConnection(Properties.Settings.Default.NewDBTESTConnectionString);

    dataAdapter = new SqlDataAdapter(selectCommand, conn);
    commandBuilder = new SqlCommandBuilder(dataAdapter);

    table = new DataTable();
    dataAdapter.Fill(table);

    bindingSource1.DataSource = table;
    dataGridView1.DataSource = bindingSource1;
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
        dataAdapter.Update((DataTable)bindingSource1.DataSource);    
}

I'm writing a Winforms app in C# that enables the user to edit and update database using the datagridview.

The problem is, I can't make it to work. The only thing that I managed to achieve is updating what the datagridview is showing, but when I go into the database table, there is no change in the data. I have search a lot of sites discussing about that problem but nothing yet worked for me.

Things I have tried so far:

  • binding the database to the datagridview
  • changing the Copy to Output Directory property to Copy if newer
  • using the dataAdapter.Update((DataTable)bindingSource1.DataSource) with or without any update query execute.
  • execute update query without dataAdapter.update(...).

Here is my code:

public Form1()
{
    InitializeComponent();
    GetData("SELECT * FROM Table1");
}

void GetData(string selectCommand)
{
    SqlConnection conn = new SqlConnection(Properties.Settings.Default.NewDBTESTConnectionString);

    dataAdapter = new SqlDataAdapter(selectCommand, conn);
    commandBuilder = new SqlCommandBuilder(dataAdapter);

    table = new DataTable();
    dataAdapter.Fill(table);

    bindingSource1.DataSource = table;
    dataGridView1.DataSource = bindingSource1;
}

private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{
        dataAdapter.Update((DataTable)bindingSource1.DataSource);    
}

原文:https://stackoverflow.com/questions/26946397
更新时间:2023-04-21 21:04

最满意答案

你应该使用类似的东西

setInterval("myFunc()",5000);

function myFunc()
{
    $('#Notify-Bar').html('a new notification');
}

You should use something like

setInterval("myFunc()",5000);

function myFunc()
{
    $('#Notify-Bar').html('a new notification');
}

相关问答

更多
  • 我重构了我的代码,使用匿名函数来解决这个问题的setInterval和setTimeout方法。 (-> MYAPP.utilities2 = playAudio: (times, interval) -> audio_url = $(".play-audio").data("audio") myAudio = new Audio(audio_url) return myAudio.play() if times ...
  • someFuncObject.call(o2)实际上调用了该函数。 这意味着,传递给setInterval是从someFuncObject()返回的someFuncObject() 。 setInterval期望的参数是函数本身,而不是函数返回的函数。 如果要在someFuncObject控制此值,可以使用匿名函数,如: window.setInterval(function() { someFuncObject.call(o2); }, 2000); someFuncObject.call(o2 ...
  • 用这个 intervalId = window.setInterval(UpdateProgress, 1500); 带有文字参数的setInterval将在无法访问UpdateProgress的全局范围内对此进行eval 。 Use this intervalId = window.setInterval(UpdateProgress, 1500); setInterval with a literal argument will eval this in the global scope whe ...
  • 全部都在同一个'page.php' php部分: Javascript部分:(用jquery)