gridview 批量保存到SQL表中,点保存只保存当前页,再点保存就会把所有页都写到后台

dongxu572 dongxu572 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
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();
}

}
...全文

相关推荐

评论 1
收藏 已收藏
分享
返回顶部
dongxu572
ASP 28,401 成员 356,940 内容