开发者

c# database update/insert with tableadapter

I'm having a "small" question concerning inserting and updating a database in C#. I'm using a table adapter for inserting and updating. The information is coming from an XML file that contains customer information. My primary key is customer_nr which is provided by the XML file.

Now my question is when loading the XML file, somehow I want to check if the database already have the specific customer_nr. If the customer_nr exist, update the database (if the customer changes address this should be updated), else if the customer_nr does not exist insert a new row(with the customers information).

My test code:

(adapter.getCustomerNr is an SQL command in the table adapter where i select all the customer_numbers.)

DataSet1TableAdapters.customer_infoTableAdapter adapter = new DataSet1Tab开发者_运维问答leAdapters.customer_infoTableAdapter();
foreach (var t in customerInfo)
{
    if (adapter.CustomerNr() != null)
    {
        adapter.Update(t.firstName,t.lastName,t.customerNr);
    }
    else {
        adapter.Insert(t.customerNr, t.firstName, t.lastName); 
    }
}

As I'm very new to C# I'm curious if this even works. But its something that i thought should work. If someone could help me or give me some example how to further go on I would be very grateful.


In your update command, make it use a stored procedure that would check before inserting the record:

IF NOT EXISTS(SELECT customer_nr FROM Customers where customer_nr = @customer_nr)
    BEGIN

        ...(your query)

    END
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜