I want to insert data into database table using SqlDataAdapter
from one database table to another database table but no effect after import rows from other table
Code
DataTable table1 = objDa开发者_如何学Pythontaset1.Tables[0];
DataTable tb = table1.Clone();
DataSet ds = new DataSet();
string ConnString = @"DataSource=.\SQLExpress;Database=WICC;Trusted_Connection=true;Connection Timeout=180;";
SqlConnection con = new SqlConnection(ConnString);
SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM tblSitePMRDetail", con);
SqlCommandBuilder build = new SqlCommandBuilder(adapter);
adapter.Fill(tb);
con.Open();
foreach (DataRow dr in table1.Rows)
{
tb.ImportRow(dr);
}
adapter.Update(tb);
con.Close();
Why is the table not updated?
ImportRow will fail if contraints are violated, and will only ADD, so check the number of rows you have in the table at the end. You will probably find that none are being added, especially if there is a primary key involved.
Given that tb is Cloned from table1, I would suggest that you skip the adapter.Fill unless you know that the row state in table1 is incorrect.
Or you might want to try using tb.Load(table1.CreateDataReader(), LoadOption.Upsert);
精彩评论