c# - Mysql dataadapter InsertCommand -
i have following code
mysqlconnection mycon = new mysqlconnection(system.configuration.configurationmanager.appsettings["mysqlcon"]); mysqldataadapter dadap = new mysqldataadapter(); dadap.insertcommand = new mysqlcommand("insert pmduedates(ecid,ecclass,ddate,pmtype) values(ecid,ecclass,ddate,pmtype)", mycon); dadap.insertcommand.parameters.add("ecid", mysqldbtype.varchar, 20, "eqpid"); dadap.insertcommand.parameters.add("ecclass", mysqldbtype.varchar, 100, "class"); dadap.insertcommand.parameters.add("ddate", mysqldbtype.varchar, 100, "mainttype"); dadap.insertcommand.parameters.add("pmtype", mysqldbtype.date, 10, "duedate"); mycon.open(); dadap.update(dtdue); mycon.close();
in above code dtdue datatable , eqpid,class,mainttype , duedate datacolumns. when executing code n't gives error data not updating in table pmduedates.
why should dataadapter
insert records in datatable
? nobody told so. insert rows if in rowstate.added
, can force in way:
foreach (datarow row in dtdue.rows) row.setadded();
but since providing insert-command , parameters manually anyway, execute query manually:
using(var mycon = new mysqlconnection(system.configuration.configurationmanager.appsettings["mysqlcon"])) using(var cmd = new mysqlcommand("insert pmduedates(ecid,ecclass,ddate,pmtype) values(ecid,ecclass,ddate,pmtype)", mycon)) { cmd.insertcommand.parameters.add("ecid", mysqldbtype.varchar, 20, "eqpid"); cmd.insertcommand.parameters.add("ecclass", mysqldbtype.varchar, 100, "class"); cmd.insertcommand.parameters.add("ddate", mysqldbtype.varchar, 100, "mainttype"); cmd.insertcommand.parameters.add("pmtype", mysqldbtype.date, 10, "duedate"); mycon.open(); int countinserted = cmd.executenonquery(); } // using closes connection implicitely
Comments
Post a Comment