0

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();
Nime Cloud
  • 6,162
  • 14
  • 43
  • 75
  • do you have some code? we need to know how you access your database, with for example linq2sql or entity framework. – hcb Sep 13 '11 at 08:35
  • 1
    Give a try with http://msdn.microsoft.com/en-us/library/bb383977.aspx or http://msdn.microsoft.com/en-us/library/bb397696.aspx or http://msdn.microsoft.com/en-us/library/w5zay9db.aspx – Rosmarine Popcorn Sep 13 '11 at 08:38
  • Part1: Named and Optional Arguments in C# http://msdn.microsoft.com/en-us/library/dd264739.aspx – Nime Cloud Sep 13 '11 at 09:12

1 Answers1

2

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!

Seffix
  • 1,049
  • 1
  • 12
  • 16