SqlDataAdapter.Update(数据table)不工作(不更新数据库)
SqlDataAdapter.Update (data table) is not working (not updating the database)
我有下面的代码,我试图从 windows 应用程序中所做的 c# 网格视图更改更新数据库....
代码未更新 SQL 服务器数据库。我不确定问题到底发生在哪里。一切似乎都很好...
或者有没有办法再次使用 "update query statement" 更新数据库 table 重点是如何为数据网格视图中的任何值更改编写更新语句?
我认为绑定和 select 命令语句可能存在问题...
谁能指出我解决这个问题的正确方向?
public partial class KnowledgeBaseForm : Form
{
private SqlDataAdapter SDA = new SqlDataAdapter();
private DataTable DT = new DataTable();
private void button_retrievekb_Click(object sender, EventArgs e)
{
try
{
con.Open();
SqlDataAdapter SDA = new SqlDataAdapter(@"SELECT * From Table1", con);
SDA.Fill(DT);
bindingsource.DataSource = DT;
dataGridView.DataSource = bindingsource;
if (DT.Rows.Count > 0)
{
dataGridView.Columns[0].DefaultCellStyle.ForeColor = Color.Gray;
dataGridView.Columns[0].ReadOnly = true;
}
else
{
MessageBox.Show("No Knowledge Base Rules Found");
}
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
finally
{
con.Close();
}
}
private void button_update_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you really want to Update these values?", "Confirm Update", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//binding the datasource with the changes made in the gridview
bindingsource.DataSource = dataGridView.DataSource;
DT.AcceptChanges();
scb = new SqlCommandBuilder(SDA);
SDA.Update((DataTable) bindingsource.DataSource);
MessageBox.Show("Updates successfully submitted to CoSD");
}
}
}
试试这个:
public partial class KnowledgeBaseForm : Form
{
SqlDataAdapter SDA = new SqlDataAdapter();
DataTable DT = new DataTable();
DataSet ds = new DataSet();
private void button_retrievekb_Click(object sender, EventArgs e)
{
SDA = new SqlDataAdapter(@"SELECT * From Table1", con);
ds = new DataSet();
SDA.fill(ds,"SomeName");
dataGridView1.DataSource = ds.Tables[0];
}
private void button_update_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you really want to Update these values?", "Confirm Update", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
SqlCommandBuilder builder = new SqlCommandBuilder(SDA);
SDA.Update(ds,"SomeNme");
}
}
}
我有下面的代码,我试图从 windows 应用程序中所做的 c# 网格视图更改更新数据库....
代码未更新 SQL 服务器数据库。我不确定问题到底发生在哪里。一切似乎都很好...
或者有没有办法再次使用 "update query statement" 更新数据库 table 重点是如何为数据网格视图中的任何值更改编写更新语句?
我认为绑定和 select 命令语句可能存在问题...
谁能指出我解决这个问题的正确方向?
public partial class KnowledgeBaseForm : Form
{
private SqlDataAdapter SDA = new SqlDataAdapter();
private DataTable DT = new DataTable();
private void button_retrievekb_Click(object sender, EventArgs e)
{
try
{
con.Open();
SqlDataAdapter SDA = new SqlDataAdapter(@"SELECT * From Table1", con);
SDA.Fill(DT);
bindingsource.DataSource = DT;
dataGridView.DataSource = bindingsource;
if (DT.Rows.Count > 0)
{
dataGridView.Columns[0].DefaultCellStyle.ForeColor = Color.Gray;
dataGridView.Columns[0].ReadOnly = true;
}
else
{
MessageBox.Show("No Knowledge Base Rules Found");
}
}
catch (Exception ex)
{
MessageBox.Show("Error : " + ex.Message);
}
finally
{
con.Close();
}
}
private void button_update_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you really want to Update these values?", "Confirm Update", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
//binding the datasource with the changes made in the gridview
bindingsource.DataSource = dataGridView.DataSource;
DT.AcceptChanges();
scb = new SqlCommandBuilder(SDA);
SDA.Update((DataTable) bindingsource.DataSource);
MessageBox.Show("Updates successfully submitted to CoSD");
}
}
}
试试这个:
public partial class KnowledgeBaseForm : Form
{
SqlDataAdapter SDA = new SqlDataAdapter();
DataTable DT = new DataTable();
DataSet ds = new DataSet();
private void button_retrievekb_Click(object sender, EventArgs e)
{
SDA = new SqlDataAdapter(@"SELECT * From Table1", con);
ds = new DataSet();
SDA.fill(ds,"SomeName");
dataGridView1.DataSource = ds.Tables[0];
}
private void button_update_Click(object sender, EventArgs e)
{
if (MessageBox.Show("Do you really want to Update these values?", "Confirm Update", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
SqlCommandBuilder builder = new SqlCommandBuilder(SDA);
SDA.Update(ds,"SomeNme");
}
}
}