/// <summary> /// 执行数据库回滚操作,用于sql语句执行失败后,恢复执行前的数据 /// </summary> /// <param name="TableName">目标表</param> /// <param name="dt">源数据</param> public static int RollBack(string strSql) { SqlConnection conn = new SqlConnection(connectionString);//实例化数据连 conn.Open();//打开数据库连接 SqlCommand command = conn.CreateCommand(); SqlTransaction transaction = null; transaction = conn.BeginTransaction(); command.Connection = conn; command.Transaction = transaction; int count = 0; try { if (strSql != "") { command.CommandText = strSql; count = command.ExecuteNonQuery(); } transaction.Commit(); } catch { transaction.Rollback(); } finally { conn.Close();//关闭数据库连接 } return count; }