开发者

C# ASP.NET Update database with datatable

开发者 https://www.devze.com 2023-01-02 16:32 出处:网络
Scenario: I\'m just trying to update my database with the changes made by the user to their information. Here is my code:

Scenario: I'm just trying to update my database with the changes made by the user to their information. Here is my code:

SqlCommandBuilder cb = new SqlCommandBuilder(da);

dt.Rows[0][2] = txtname.Text;
dt.Rows[0][3] = txtinterests.Text;
dt.Rows[0][4] = txtlocation.Text;

da.SelectCommand = new SqlCommand(sqlcommand, conn);
da.Update(dt);

I know its going to be something开发者_运维技巧 obvious, but what have I missed? There are no errors, everything compiles correctly, but nothing happens. The record remains unchanged.


You need to define the UpdateCommand on the dataadapter (possibly the InsertCommand too).

For each Modified row in the datatable, it will fire the command you specify as the UpdateCommand.
For each New row in the datatable, it will fire the command you specify as the InsertCommand.

Check out the MSDN reference here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号