gridview 批量保存到SQL表中,点保存只保存当前页,再点保存就会把所有页都写到后台
protected void btnsave_Click(object sender, EventArgs e)
{
gvRecord.AllowPaging = false;
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString);
SqlTransaction tran = null;
try
{
conn.Open();
for (int i = 0; i < gvRecord.Rows.Count; i++)
{
//sda.SelectCommand.Transaction = sqlT;
string sqlStr = "";
SqlCommand comm = new SqlCommand();
tran = conn.BeginTransaction();
string strA = gvRecord.Rows[i].Cells[0].Text.Trim().ToString();
string strB = gvRecord.Rows[i].Cells[1].Text.Trim().ToString();
string strC = gvRecord.Rows[i].Cells[2].Text.Trim().ToString();
sqlStr += "INSERT INTO dbo.tb_mm (A, B, C,) VALUES (‘“+A+”’,‘“+B+”’,‘“+C+”’,);";
comm.CommandText = sqlStr;
comm.Connection = conn;
comm.Transaction = tran;
comm.ExecuteNonQuery();
tran.Commit();
}
}
catch (Exception ex)
{
Response.Write("更新失败,失败原因:" + ex.Message);
tran.Rollback(); //事务回滚
}
finally
{
conn.Close();
}
}