这段代码中的update怎么没起作用?

litgle 2008-12-08 11:55:14
我更改了几个textBox的内容,跟踪发现orders的记录也作了相应的修改,但行状态却是unchanged,所以单击button1也更新不了数据库的数据。


namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection c1;
SqlDataAdapter da;
DataTable orders;

private void button1_Click(object sender, EventArgs e)
{

da.Update(orders);

c1.Close();
}

private void button3_Click(object sender, EventArgs e)
{
c1 = new SqlConnection("server=.;database=db;uid=sa;pwd=");
c1.Open();

da = new SqlDataAdapter("select * from tb_ltd_info", c1);
SqlCommandBuilder cb = new SqlCommandBuilder(da);
orders = new DataTable();
da.Fill(orders);

this.textBox1.DataBindings.Add("text", orders, "Ltd_Name", false, DataSourceUpdateMode.OnPropertyChanged);
this.textBox2.DataBindings.Add("text", orders, "Ltd_Add", false, DataSourceUpdateMode.OnPropertyChanged);
this.textBox3.DataBindings.Add("text", orders, "Ltd_Phone", false, DataSourceUpdateMode.OnPropertyChanged);
this.textBox4.DataBindings.Add("text", orders, "Ltd_Fax", false, DataSourceUpdateMode.OnPropertyChanged);
this.textBox5.DataBindings.Add("text", orders, "Ltd_EMail", false, DataSourceUpdateMode.OnPropertyChanged);

}
}
}
...全文
249 30 打赏 收藏 转发到动态 举报
写回复
用AI写文章
30 条回复
切换为时间正序
请发表友善的回复…
发表回复
flyjimi 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 litgle 的回复:]
经过试验,发现要以历遍的方式把DataTable的每一行都提交EndEdit()才能更新,代码如下,只是在更新前加了两行:

C# code
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection c1;
SqlDataAdapter da;
DataTable orders;

private void button1_…
[/Quote]

如果你使用BindingSouce,直接调用BindingSouce.EndEdit()就把每一行都EndEdit了吧
weiphone 2008-12-09
  • 打赏
  • 举报
回复
没看到SQL update 语句嘛
flyjimi 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 26 楼 clming327 的回复:]
这是由于DataRow绑定在TextBox之后,所做的修改保存到了DataRow里,却没有修改行的状态,你可以通过注册ColumnChanged事件达到目的:

C# codeorders.ColumnChanged += new DataColumnChangeEventHandler(Column_Changed);



C# code
private static void Column_Changed(object sender, DataColumnChangeEventArgs e)
{
e.Row.EndEdit();
}
[/Quote]

对的,但是用 RowChanged 是不是会更合适?
workwangkang 2008-12-09
  • 打赏
  • 举报
回复
没有更新数据库的数据用
BS.DataSource = orders
来更新就行了
clming327 2008-12-09
  • 打赏
  • 举报
回复
这是由于DataRow绑定在TextBox之后,所做的修改保存到了DataRow里,却没有修改行的状态,你可以通过注册ColumnChanged事件达到目的:
orders.ColumnChanged += new DataColumnChangeEventHandler(Column_Changed);


private static void Column_Changed(object sender, DataColumnChangeEventArgs e)
{
e.Row.EndEdit();
}
qinhl99 2008-12-09
  • 打赏
  • 举报
回复
up
lsj_zrp 2008-12-09
  • 打赏
  • 举报
回复
SqlCommandBuilder会根据你的查询语句自动生成Update,Delete,Insert命令,调用SqlDataAdapter.Update时会
根据你的DataSet中DataTable的DataRow的行状态来执行对应的操作更新数据库
使用SqlCommandBuilder来批处理更新数据库,要求你的数据表必须有主键
所以使用SqlCommandBuilder更新数据库失败,先查看数据表是否有主键,然后看DataRow的行状态
赵一一 2008-12-09
  • 打赏
  • 举报
回复
只是更新了数据集(也就是内存中的数据),所以你跟踪时发现值改变了的,但是你并没有将值传给数据库
command = new SqlCommand(
"UPDATE 表名 SET 更新字段" +
"WHERE 条件", connection);
然后用command.Parameters.Add("...")来赋值...
正解
dq512000 2008-12-09
  • 打赏
  • 举报
回复
9楼已说了。。。。。
hernmmy 2008-12-09
  • 打赏
  • 举报
回复
加上这个试试
SQlcommand cmd=new SqlCommand(sqlString,conn);
cmd.ExecuteNonQuery();
hawaiiboys 2008-12-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 GTX280 的回复:]
C# code
public static SqlDataAdapter CreateCustomerAdapter(
SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter();

// Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection);

// Add the parameters for the SelectCommand.
command.Parameters.Ad…
[/Quote]
正解
litgle 2008-12-09
  • 打赏
  • 举报
回复
期待答案.
yangshenghong 2008-12-08
  • 打赏
  • 举报
回复
好像是值更新了数据集而没有更新数据库
GTX280 2008-12-08
  • 打赏
  • 举报
回复
这是MSDN上的例子,重点是这段:
  //要加上这个UpdateCommand
// Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);


// Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;

adapter.UpdateCommand = command;

GTX280 2008-12-08
  • 打赏
  • 举报
回复

public static SqlDataAdapter CreateCustomerAdapter(
SqlConnection connection)
{
SqlDataAdapter adapter = new SqlDataAdapter();

// Create the SelectCommand.
SqlCommand command = new SqlCommand("SELECT * FROM Customers " +
"WHERE Country = @Country AND City = @City", connection);

// Add the parameters for the SelectCommand.
command.Parameters.Add("@Country", SqlDbType.NVarChar, 15);
command.Parameters.Add("@City", SqlDbType.NVarChar, 15);

adapter.SelectCommand = command;

// Create the InsertCommand.
command = new SqlCommand(
"INSERT INTO Customers (CustomerID, CompanyName) " +
"VALUES (@CustomerID, @CompanyName)", connection);

// Add the parameters for the InsertCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");

adapter.InsertCommand = command;

  //要加上这个UpdateCommand
// Create the UpdateCommand.
command = new SqlCommand(
"UPDATE Customers SET CustomerID = @CustomerID, CompanyName = @CompanyName " +
"WHERE CustomerID = @oldCustomerID", connection);


// Add the parameters for the UpdateCommand.
command.Parameters.Add("@CustomerID", SqlDbType.NChar, 5, "CustomerID");
command.Parameters.Add("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName");
SqlParameter parameter = command.Parameters.Add(
"@oldCustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;


adapter.UpdateCommand = command;

// Create the DeleteCommand.
command = new SqlCommand(
"DELETE FROM Customers WHERE CustomerID = @CustomerID", connection);

// Add the parameters for the DeleteCommand.
parameter = command.Parameters.Add(
"@CustomerID", SqlDbType.NChar, 5, "CustomerID");
parameter.SourceVersion = DataRowVersion.Original;

adapter.DeleteCommand = command;

return adapter;
}

bo3235 2008-12-08
  • 打赏
  • 举报
回复
da.Update(orders);
这个只修改了内存的数据,未更新到数据库.
da = new SqlDataAdapter("select * from tb_ltd_info", c1);
仅仅是读数据操作
你可以查下MSN相关资料,暂时工作很忙,如果晚上还没有解决的话,发个例子给你

litgle 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bo3235 的回复:]
只修改了内存的数据,未更新到数据库.... 还需要相关的SQL操作指令方法
[/Quote]

不是的,看这里:

SqlCommandBuilder cb = new SqlCommandBuilder(da);


da.Update(orders);
bo3235 2008-12-08
  • 打赏
  • 举报
回复
只修改了内存的数据,未更新到数据库.... 还需要相关的SQL操作指令方法
litgle 2008-12-08
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 birdlonger 的回复:]
button1_click 里面写成下面这样试下.应该就可以的!

da.Update(orders);
orders.AcceptChanges();
[/Quote]

你说的这个办法在之前我发的一个帖子里有人说过,但是试验后发现无效。
birdlonger 2008-12-08
  • 打赏
  • 举报
回复
晕,原来你还没绑定数据源.sorry!
加载更多回复(10)

110,570

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧