c# - Pass getdate() in dynamic query as parameter -


good morning everyone! building dynamical query parameters seen in code below.

     c.open();                 string columns = "(";                 string values = "(";                 foreach (datagridviewcolumn column in rootdataview.columns)                 {                     if (virtualcolumns.contains(column.name))                     {                     }                     else                     {                         columns += column.name + ",";                         values += "@" + column.name + " ,";                     }                 }                 columns = columns.substring(0, columns.length - 1) + ")";                 values = values.substring(0, values.length - 1) + ")";                 sqlcommand command = new sqlcommand("insert " + tablename + " " + columns + " values " + values, c);                 foreach (datagridviewcolumn column in rootdataview.columns)                 {                     if (virtualcolumns.contains(column.name))                     {                     }                     else                     {                         if (column.name != "timefrom")                         {                             command.parameters.addwithvalue("@" + column.name, editedrow.cells[column.name].value);                         }                         else                         {                             command.parameters.addwithvalue("@" + column.name, system.datetime.now);                         }                     }                 }                 command.executenonquery(); 

but have encountered problem on line

command.parameters.addwithvalue("@" + column.name, system.datetime.now); 

i need synchronize time sql server, need somehow pass getdate() parameter, when enter this

command.parameters.addwithvalue("@" + column.name, "getdate()"); 

it being sent string/varchar not call function.

i know if there way pass function parameter, or if need replace @timefrom part of query string.

edit:please note bold part, question - asking yes/no , explanation why

you have try this:

            c.open();             string columns = "(";             string values = "(";             foreach (datagridviewcolumn column in rootdataview.columns)             {                 if (virtualcolumns.contains(column.name))                 {                 }                 else                 {                     columns += column.name + ",";                     if (column.name != "timefrom")                     {                         values += "@" + column.name + " ,";                     } else {                         //change in string                         values += "getdate() ,";                     }                 }             }             columns = columns.substring(0, columns.length - 1) + ")";             values = values.substring(0, values.length - 1) + ")";             sqlcommand command = new sqlcommand("insert " + tablename + " " + columns + " values " + values, c);             foreach (datagridviewcolumn column in rootdataview.columns)             {                 if (virtualcolumns.contains(column.name))                 {                 }                 else                 {                     if (column.name != "timefrom")                     {                         command.parameters.addwithvalue("@" + column.name, editedrow.cells[column.name].value);                     }                 }             }             command.executenonquery(); 

basically, not treat timefrom parameter use getdate() set it's value in insert.

sql server , client library using, not support passing parameters containing sql. reason why construct not work:

command.parameters.addwithvalue("@" + column.name, "getdate()"); 

the thing allowed pass in addwithvalue translate literal sql value.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -