How can I update only submitted fields?
I need to update various fields in a table.
Question part 1: How can I only send the necessary arguments? I can do that in JavaScript like below:
myUpdateFunction( {tableID: 45, personFirstName: 'Blah'} );
// and then
myUpdateFunction( {tableID = 48, personFirstName: 'Blah', personLastName: 'Blah'} );
Question part 2: How can I handle the arguments and build the sql query? Is there any smart method exist or should I simply use if..else blocks?
P.S: I use compact version 4 of SQL.
开发者_开发知识库 string sql = @"UPDATE [personList]
SET
personFirstName=@personFirstName
personLastName=@personLastName
personPhoto=@personPhoto
WHERE personID=@personID";
cmd = new SqlCeCommand(sql, cn);
cmd.Parameters.AddWithValue("@personID", personID);
cmd.Parameters.AddWithValue("@personFirstName", personFirstName);
cmd.Parameters.AddWithValue("@personLastName", personLastName);
cmd.Parameters.AddWithValue("@personPhoto", personPhoto);
cmd.ExecuteNonQuery();
Those are your sql queries:
UPDATE TableName
SET personFirstName='Blah'
WHERE tableID= 45
UPDATE TableName
SET personFirstName='Blah' AND personLastName='Blah'
WHERE tableID= 48
Can't help you with Javascript... sorry!
精彩评论