62,046
社区成员
发帖
与我相关
我的任务
分享
public static int UpdateData(DataTable dt)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WebSite"].ToString());
SqlDataAdapter myAdapter = new SqlDataAdapter();
SqlCommand myCommand = new SqlCommand("select * from "+dt.TableName, conn);
myAdapter.SelectCommand = myCommand;
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
return myAdapter.Update(dt);
}
public static int SqlBulkUpdate(DataTable dt)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["WebSite"].ToString());
int affect = 0;
try
{
SqlCommand myCommand = new SqlCommand("SELECT * FROM " + dt.TableName, conn);
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myAdapter);
myAdapter.UpdateCommand = myCommandBuilder.GetUpdateCommand();
conn.Open();
affect = myAdapter.Update(dt);
dt.AcceptChanges();
conn.Close();
}
catch (Exception err)
{
conn.Close();
throw err;
}
return affect;
}