c# - Anything like SqlBulkCopy for Update statements? -


this application i'm developing read in bunch of records, validate / modify data, send data db.

i need "all or none" approach; either update of rows, or don't modify of data in db.

i found sqlbulkcopy class has method writing of rows in datatable database unacceptable purposes. wrote quick app that'd write few rows db, populate datatable database, modify data, call writetoserver(datatable dt) again. though datatable has of primary key information, added new rows data , new primary keys rather updating data rows have matching primary keys.

so, how can bulk update statements mssql database c# winforms application?

i don't think relevant, i'll include code anyways. i'm not great when comes interacting databases, it's possible i'm doing dumb , wrong.

static void main(string[] args) {     using (sqlconnection conn = new sqlconnection("myconnectioninfo"))     {         //datatable dt = maketable();         datatable dt = filldatatable(conn);          using (sqlbulkcopy sbc = new sqlbulkcopy(conn))         {             sbc.destinationtablename = "testtbl";              try             {                 (int = 0; < dt.rows.count; i++)                 {                     dt.rows[i][1] = i;                     dt.rows[i][2] = i+1;                     dt.rows[i][3] = i+2;                 }                  sbc.writetoserver(dt);             }             catch (exception ex)             {                 console.writeline(ex.message);             }                         {                 console.writeline("press key continue");                 console.readkey();             }         }     } }  private static datatable maketable() {     datatable newproducts = new datatable("testtbl");      datacolumn testpk = new datacolumn();     testpk.datatype = system.type.gettype("system.int32");     testpk.columnname = "testpk";     testpk.allowdbnull = false;     testpk.autoincrement = true;     testpk.unique = true;     newproducts.columns.add(testpk);      datacolumn col1 = new datacolumn();     col1.datatype = system.type.gettype("system.string");     col1.columnname = "col1";     col1.allowdbnull = false;     newproducts.columns.add(col1);      // add 2 more columns above block       datarow row = newproducts.newrow();     row["col1"] = "cc-101-wh";     row["col2"] = "cyclocomputer - white";     row["col3"] = dbnull.value;     newproducts.rows.add(row);      // add 2 more rows above block      newproducts.acceptchanges();     return newproducts; }  private static datatable filldatatable(sqlconnection conn) {     string query = "select * nyms.dbo.testtbl";      using (sqlcommand cmd = new sqlcommand(query, conn))     {         conn.open();         datatable dt = new datatable();         dt.load(cmd.executereader());         return dt;     } } 

the call maketable() commented out because used when first ran application insert data, while trying update test data use filldatatable populate it


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

css - Can I use the :after pseudo-element on an input field? -