| |

VerySource

 Forgot password?
 Register
Search
View: 648|Reply: 4

How to update data in dataset?

[Copy link]

4

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-2-13 23:00:01
| Show all posts |Read mode
custDA.Fill (custDS, "Customers"); // How to update after the custDS has changed (only modified) after completing the filling. The following command is also unsuccessful.
 updateCMD.Update (custDS); // Update all tables in the database
 updateCMD.Update (custDS, "Customers"); // Update a table
Reply

Use magic Report

0

Threads

322

Posts

115.00

Credits

Newbie

Rank: 1

Credits
115.00

 China

Post time: 2020-4-12 20:30:01
| Show all posts
1. Make sure there is an Update command.
2.ds.AcceptChanges ();
Reply

Use magic Report

0

Threads

17

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-4-14 19:45:01
| Show all posts
System.Data.SqlClient.SqlCommandBuilder cb = new System.Data.SqlClient.SqlCommandBuilder (custDA);
custDA.Update (ds);
Reply

Use magic Report

4

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-4-15 13:30:01
| Show all posts
SqlConnection nwindConn = new SqlConnection (@ "server = TEST\HR; database = test; uid = sa; pwd = 123456");
             SqlDataAdapter dataAdpater = new SqlDataAdapter ("SELECT CustomerID, Address, CompanyName FROM Customers", nwindConn);
        dataAdpater.UpdateCommand = new SqlCommand ("UPDATE Customers SET CompanyName = @CompanyName" + "WHERE CustomerID = @CustomerID", nwindConn);
        dataAdpater.UpdateCommand.Parameters.Add ("@ CompanyName", SqlDbType.NVarChar, 15, "CompanyName");
        SqlParameter parameter = dataAdpater.UpdateCommand.Parameters.Add ("@ CustomerID", SqlDbType.Int);
        parameter.SourceColumn = "CustomerID";
        parameter.SourceVersion = DataRowVersion.Original;
        DataSet dataSet = new DataSet ();
        dataAdpater.Fill (dataSet, "Customers");

        DataRow row = dataSet.Tables ["Customers"]. Rows [0];
        row ["CompanyName"] = "New 1111CompanyName";
        dataSet.AcceptChanges ();

        DataRow row1 = dataSet.Tables ["Customers"]. Rows [1];
        row1 ["CompanyName"] = "New 12211CompanyName";

        dataAdpater.Update (dataSet, "Customers");
       
       
               ////Display Data
        Label1.Text = dataSet.Tables ["Customers"]. Rows [0] ["CompanyName"]. ToString ();
        Label2.Text = dataSet.Tables ["Customers"]. Rows [0] ["CustomerID"]. ToString ();
        Label3.Text = dataSet.Tables ["Customers"]. Rows [1] ["CompanyName"]. ToString ();
        Label4.Text = dataSet.Tables ["Customers"]. Rows [1] ["CustomerID"]. ToString ();

The error displayed after execution is: "Concurrency violation: UpdateCommand affected 0 of the expected 1 record."

Because I want to record multiple changes at the same time, I wonder if it is possible.
Reply

Use magic Report

4

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

 Author| Post time: 2020-4-15 21:45:02
| Show all posts
bastardI do n’t understand your paragraph. I turned out that the code was correct, but the CustomerID could not be empty and I did n’t look at the database at the beginning. When it was empty, I reported that error: "Concurrency violation: UpdateCommand affected 0 of the expected 1 record ".
I have worked hard to answer my question. Thank you.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list