Updating and saving a dataset problem

I think I'm missing. Here I want to update the dataset and return it back to the database where it came from, but I keep getting:

Violation

Concurrency: UpdateCommand affected 0 of the expected 1 records.

Here is some code generating this error:

    public static void UpdateNorthWindWithDataset()
    {
        string connString =
            @"Data Source=localhost;Initial Catalog=NorthWind;Integrated Security=SSPI;";


        using (SqlConnection conn = new SqlConnection(connString))
        {
            conn.Open();


            // Declaring a DataAdapter and initiating it with a Select and updateCommand                
            SqlDataAdapter da = new SqlDataAdapter();

            SqlCommand selectCmd = new SqlCommand("SELECT CustomerId, City, Region " +
                                                  "FROM Customers"
                                                  , conn
                );

            da.SelectCommand = selectCmd;

            SqlCommand updateCmd = new SqlCommand(
                @"UPDATE Customers SET City='@City', Region='@Region'" +
                @"WHERE CustomerID = '@CustomerID'",
                conn
                );

            updateCmd.Parameters.AddRange(
                new SqlParameter[]
                    {
                        new SqlParameter()
                            {
                                ParameterName = "@CustomerID",
                                SourceColumn = "customerid"
                            },
                        new SqlParameter()
                            {
                                ParameterName = "@City",
                                SourceColumn = "city",
                                SqlDbType = SqlDbType.VarChar
                            },
                        new SqlParameter()
                            {
                                ParameterName = "@Region",
                                SourceColumn = "region",
                                SqlDbType = SqlDbType.VarChar
                            }
                    }
                );


            da.UpdateCommand = updateCmd;

            // filling dataset
            DataSet ds = new DataSet();
            da.Fill(ds, "srcCustomers");

            // declaring and editing datatable
            DataTable tblCustomers = ds.Tables["srcCustomers"];

            foreach (DataRow row in tblCustomers.Rows)
            {
                row["City"] = "justUpdated";
                row["Region"] = "justUpdated too";
            }

            da.Update(ds, "srcCustomers");
        }
    }

Now my endgoal uses such code with MsAccess throug OLEdb, but since I wanted it to be as clear as possible, I tried MSSQL (2k more here) with native.net support, but still got the error ...

+3
source share
1 answer

, , , , , - SQL, .

OLEDB, , ( , , , , , - , , ).

+2

Source: https://habr.com/ru/post/1708273/


All Articles