c# - Problem with ADO.NET UPDATE code -
could take quick peek @ ado.net code? trying update row dataset, isn't working. missing elemental piece of code, , eluding me. have verified datarow has correct data in it, row accurate.
many in advance.
try { //basic ado.net objects sqldataadapter dbadapter = null; dataset returnds2 = new dataset(); //a new sql connection sqlconnection myconn = new sqlconnection(); myconn.connectionstring = "server=myserver.mydomain.com;" + "database=mydatabase;" + "user id=myuserid;" + "password=mypassword;" + "trusted_connection=true;"; //the sqlquery string sqlquery = "select * avlupdatemessages id = 21"; //another ado.net object command sqlcommand cmd = new sqlcommand(); cmd.connection = myconn; cmd.commandtext = sqlquery; //open connection, execute sql statement , close connection. myconn.open(); //instantiate , fill sqldataadapter dbadapter = new sqldataadapter(cmd); dbadapter.fill(returnds2, @"avlupdatemessages"); //loop through of rows; have verified rows correct , returns correct data db (int = 0; <= returnds2.tables[0].rows.count - 1; i++) { datarow row = returnds2.tables[0].rows[i]; row.beginedit(); row["updatedtext"] = @"this test..."; row.endedit(); } //let's accept changes dbadapter.update(returnds2, "avlupdatemessages"); returnds2.acceptchanges(); myconn.close(); }
you might able use sqlcommandbuilder out. after fill
call, add following statement. associate command builder data adapter , (if there primary key available) should generate update statement you. note there expense behind command builder. may not relative else, involve looking @ schema information (to primary key information, field names, field types, etc.) table , generating insert, delete, , update statements involving fields in table.
sqlcommandbuilder cb = new sqlcommandbuilder(dbadapter);
Comments
Post a Comment