i am using the following code to insert a datatable to an existing table of 开发者_如何学JAVAdatabase but it's giving exception "Update requires a valid InsertCommand when passed DataRow collection with new rows"
where query is select * from placed_student
public Boolean insert(string query, DataTable dt)
{
try
{
SqlDataAdapter sqlDA = new SqlDataAdapter(query, _sqlCon);
DataTable dtValues = new DataTable();
sqlDA.Fill(dtValues);
sqlDA.Update(dt);
return false;
}
catch(Exception ex)
{
logger.Error("Error when executing Query ", ex);
return false;
}
}
Use SqlCommandBuilder
to generate the InsertCommand for your DA.
http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommandbuilder.aspx
精彩评论