.NET批量更新

public bool BatchUpdate(int id, string reason)
        {
            using (SqlConnection connection = new SqlConnection(DbHelperSQL.connectionString))
            {
                connection.Open();
                SqlCommand command = connection.CreateCommand();
                SqlTransaction transaction = connection.BeginTransaction();
                command.Connection = connection;
                command.Transaction = transaction;
                try
                {
                    string sql = string.Empty;
                    if (!string.IsNullOrEmpty(reason))
                    {
                        sql = "update news set ispass=3,datecheck=getdate(),FailReason='" + reason + "' where id=" + id;
                    }
                    else
                    {
                        sql = "update news set ispass=2,datecheck=getdate() where id=" + id;
                    }
                    command.ExecuteNonQuery();
                    transaction.Commit();
                    return true;
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    return false;
                }
            }
        }

 

posted @ 2017-06-30 09:40  字里行间  阅读(212)  评论(0编辑  收藏  举报