大家好! 我现在在dataGridView中修改、添加和删除数据,请问我要怎么做才能将更改的数据返回到数据库中?以下是部分代码。愿赐教!
string connstring = @"server=localhost;integrated security=true;database=ps";
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
DataSet ds = new DataSet();
try
{
string sql = @"select * from worker_table where worker_id='" + this.textBox1.Text.Trim() + "'";
SqlDataAdapter sdal = new SqlDataAdapter(sql, conn);
sdal.Fill(ds, "tb1"); BindingSource bs = new BindingSource();
bs.DataMember = "tb1";
bs.DataSource = ds;
this.dataGridView1.DataSource = bs;
}
string connstring = @"server=localhost;integrated security=true;database=ps";
SqlConnection conn = new SqlConnection(connstring);
conn.Open();
DataSet ds = new DataSet();
try
{
string sql = @"select * from worker_table where worker_id='" + this.textBox1.Text.Trim() + "'";
SqlDataAdapter sdal = new SqlDataAdapter(sql, conn);
sdal.Fill(ds, "tb1"); BindingSource bs = new BindingSource();
bs.DataMember = "tb1";
bs.DataSource = ds;
this.dataGridView1.DataSource = bs;
}
解决方案 »
- C# WinForm DataGridView 自动启单元格处于编辑状态
- 记事本的按键功能?
- 谁提供个C#.NET版的新浪编辑器,或是帮我把下面这个改一下也行
- 域内用户使用Exchange发送邮件问题请教
- 求助!~ c#.net 登录页面 oracle 指正一下
- 紧急求助!关于上传
- [求代码]求一个类似NETTERM之类的telnet client的C#代码.200分相送
- 如何修改Xml文件的问题,帮忙!!!!!
- 请问关于ArrayList和Hashtable的复制问题
- 100分大礼送新春,只请教C#的COM串口通讯问题?
- exec master..xp_cmdshell 'bcp.....' 在C# Winform程序中怎样用??????
- C#怎么实现启用和禁用本地网卡?在线等~~
CellEndEdit
UserAddedRow
UserDeletedRow
事件
就可以在后台判断操作了.
DataRowVersion.Current
DataRowVersion.Original
来取出修改的值,
只需要拼写sql文就可以了
{ private DataTable DT = new DataTable();
private SqlDataAdapter SDA = new SqlDataAdapter(); public 修改资料()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
} private void button2_Click(object sender, EventArgs e)
{ if (MessageBox.Show("确定要保存嘛?", "确认保存", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{ try
{
SqlCommandBuilder SCB = new SqlCommandBuilder(SDA);
SDA.Update(DT);
MessageBox.Show("更新成功!");
}
catch (System.Exception ex)
{
MessageBox.Show(ex.ToString());
return; }
} } private void 修改资料_Load(object sender, EventArgs e)
{ string connstring = @"server=localhost;integrated security=true;database=ps";
string sql = @"select * from worker_table";
SqlConnection conn = new SqlConnection(connstring);
SqlCommand cmd = new SqlCommand(sql, conn); SDA.SelectCommand = cmd;
SDA.Fill(DT);
dataGridView1.DataSource = DT;
}
}
大家帮我看看这个错在什么地方?每次执行都会弹出一个错误报道框,谢谢!!
不能单独设置select,
把下面2行,换成1行
SqlCommand cmd = new SqlCommand(sql, conn);
SDA.SelectCommand = cmd;
↓↓↓↓
SDA = new SqlDataAdapter(sql, conn);SelectCommand和UpdateCommand就都可以用了