开发者

How to update key filled value via stored procedures and DatagridView?

开发者 https://www.devze.com 2023-02-13 01:29 出处:网络
I have stored procedure which update Customers and a datagridview to work with the data. Editing not primary filed values works well, but if I try to update primary key filed value I get an exception

I have stored procedure which update Customers and a datagridview to work with the data. Editing not primary filed values works well, but if I try to update primary key filed value I get an exception "Concurrency violation: the UpdateCommand affected 0 of the expected 1 records."

This is my sp:

create procedure dbo.UpdateCustomers
@CustomerID nchar(5),
@CompanyName nvarchar(40),
@ContactName nvarchar(30),
@ContactTitle nvarchar(30),
@Address nvarchar(60),
@City nvarchar(15),
@Region nvarchar(15),
@PostalCode nvarchar(10),
@Country nvarchar(15),
@Phone nvarchar(24),
@Fax nvarchar(24)
as
update Customers
set CustomerID = @CustomerID, CompanyName = @CompanyName, ContactName = @ContactName, ContactTitle = @ContactTitle, Address = @Address,
    City = @City, Region = @Region, PostalCode = @PostalCode, Country = @Country, Phone = @Phone, Fax = @Fax
where CustomerID = @CustomerID
go

And in code:

public void UpdateData()
            {
                using (var cn = new SqlConnection(_cs))
                {

                    ....

                    var updateCmd = new SqlCommand("UpdateCustomers", cn);
                    updateCmd.CommandType = CommandType.StoredProcedure;

                    updateCmd.Parameters.Add(
                        new SqlParameter("@CustomerID", SqlDbType.NVarChar, 5, "CustomerID"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@CompanyName", SqlDbType.NVarChar, 40, "CompanyName"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@ContactName", SqlDbType.NVarChar, 30, "ContactName"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@ContactTitle", SqlDbType.NVarChar, 30, "ContactTitle"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@Address", SqlDbType.NVarChar, 60, "Address"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@City",开发者_如何学C SqlDbType.NVarChar, 15, "City"));
                    updateCmd.Parameters.Add(
                        new SqlParameter("@Region", SqlDbType.NVarChar, 15, "Region"));

                    ...

                    _da.UpdateCommand = updateCmd;
                    var count = _da.Update(_ds, "Customers"); //exception
                    GetData();
                }
            }

I understand that when this procedure executed with new CustomerID there is no rows affected to update. But, how can I pass from application new and old CustomerID?

Something like this in SQL:

update Customers set CustomerID = 'NEWVALUE' where CustomerID = 'BSBEV'
....

etc.


You should never update the primary key.

I suspect that you are getting this error because you are trying to update a row that does not exist.

You may want to read up on Optimisitc Concurrency.

If you want to change the Primary key, you should probably just insert a new record. What is your reason for doing this?


Do you use identity on the primary key you are trying to update? If so you should remove it to allow it to be edited this way.

0

精彩评论

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