public async Task <BusinessActionGroup> QueryByName(string name) { BusinessActionGroup group = null; await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _businessSecurityRuleConnectionFactory.CreateReadForBusinessSecurityRule(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } using (SqlCommand commond = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, Transaction = sqlTran, CommandText = string.Format(@"select {0} from BusinessActionGroup WITH (SNAPSHOT) where [name]=@name", StoreHelper.GetBusinessActionGroupSelectFields(string.Empty)) }) { var parameter = new SqlParameter("@name", SqlDbType.NVarChar, 150) { Value = name }; commond.Parameters.Add(parameter); commond.Prepare(); SqlDataReader reader = null; using (reader = await commond.ExecuteReaderAsync()) { if (await reader.ReadAsync()) { group = new BusinessActionGroup(); StoreHelper.SetBusinessActionGroupSelectFields(group, reader, string.Empty); } reader.Close(); } } }); return(group); }
/// <summary> /// 为业务动作组从DbDataReader中赋值 /// </summary> /// <param name="group"></param> /// <param name="reader"></param> /// <param name="prefix"></param> public static void SetBusinessActionGroupSelectFields(BusinessActionGroup group, DbDataReader reader, string prefix) { group.ID = (Guid)reader[string.Format("{0}id", prefix)]; if (reader[string.Format("{0}name", prefix)] != DBNull.Value) { group.Name = reader[string.Format("{0}name", prefix)].ToString(); } if (reader[string.Format("{0}createtime", prefix)] != DBNull.Value) { group.CreateTime = (DateTime)reader[string.Format("{0}createtime", prefix)]; } if (reader[string.Format("{0}modifytime", prefix)] != DBNull.Value) { group.ModifyTime = (DateTime)reader[string.Format("{0}modifytime", prefix)]; } }
public async Task Update(BusinessActionGroup group) { await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _businessSecurityRuleConnectionFactory.CreateReadForBusinessSecurityRule(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } using (SqlCommand commond = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, Transaction = sqlTran, CommandText = @"update BusinessActionGroup WITH (SNAPSHOT) set [name]=@name,[modifytime]=getutcdate() where [id]=@id" }) { var parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier) { Value = group.ID }; commond.Parameters.Add(parameter); parameter = new SqlParameter("@name", SqlDbType.NVarChar, 150) { Value = group.Name }; commond.Parameters.Add(parameter); commond.Prepare(); await commond.ExecuteNonQueryAsync(); } }); }
public async Task <QueryResult <BusinessActionGroup> > QueryByName(string name, int page, int pageSize) { QueryResult <BusinessActionGroup> result = new QueryResult <BusinessActionGroup>(); await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _businessSecurityRuleConnectionFactory.CreateReadForBusinessSecurityRule(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } using (SqlCommand commond = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, Transaction = sqlTran, CommandText = string.Format(@"set @currentpage=@page select @count= count(*) from BusinessActionGroup WITH (SNAPSHOT) where [name] like @name if @pagesize*@page>=@count begin set @currentpage= @count/@pagesize if @count%@pagesize<>0 begin set @currentpage=@currentpage+1 end if @currentpage=0 set @currentpage=1 end else if @page<1 begin set @currentpage=1 end select {0} from BusinessActionGroup WITH (SNAPSHOT) where [name] like @name order by [actsequence] offset (@pagesize * (@currentpage - 1)) rows fetch next @pagesize rows only;" , StoreHelper.GetBusinessActionGroupSelectFields(string.Empty)) }) { var parameter = new SqlParameter("@page", SqlDbType.Int) { Value = page }; commond.Parameters.Add(parameter); parameter = new SqlParameter("@pagesize", SqlDbType.Int) { Value = pageSize }; commond.Parameters.Add(parameter); parameter = new SqlParameter("@name", SqlDbType.NVarChar, 150) { Value = $"{name.ToSqlLike()}%" }; commond.Parameters.Add(parameter); parameter = new SqlParameter("@count", SqlDbType.Int) { Direction = ParameterDirection.Output }; commond.Parameters.Add(parameter); parameter = new SqlParameter("@currentpage", SqlDbType.Int) { Direction = ParameterDirection.Output }; commond.Parameters.Add(parameter); commond.Prepare(); SqlDataReader reader = null; using (reader = await commond.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { var group = new BusinessActionGroup(); StoreHelper.SetBusinessActionGroupSelectFields(group, reader, string.Empty); result.Results.Add(group); } reader.Close(); result.TotalCount = (int)commond.Parameters["@count"].Value; result.CurrentPage = (int)commond.Parameters["@currentpage"].Value; } } }); return(result); }
public async Task Add(BusinessActionGroup group) { await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _businessSecurityRuleConnectionFactory.CreateAllForBusinessSecurityRule(), async (conn, transaction) => { SqlTransaction sqlTran = null; if (transaction != null) { sqlTran = (SqlTransaction)transaction; } using (SqlCommand commond = new SqlCommand() { Connection = (SqlConnection)conn, CommandType = CommandType.Text, Transaction = sqlTran }) { if (group.ID == Guid.Empty) { commond.CommandText = @"insert into BusinessActionGroup WITH (SNAPSHOT) ([id],[name],[createtime],[modifytime]) values(default,@name,getutcdate(),getutcdate()); select @newid=[id] from BusinessActionGroup WITH (SNAPSHOT) where [sequence]=SCOPE_IDENTITY()"; } else { commond.CommandText = @"insert into BusinessActionGroup WITH (SNAPSHOT) ([id],[name],[createtime],[modifytime]) values(@id,@name,getutcdate(),getutcdate())"; } SqlParameter parameter; if (group.ID != Guid.Empty) { parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier) { Value = group.ID }; commond.Parameters.Add(parameter); } else { parameter = new SqlParameter("@newid", SqlDbType.UniqueIdentifier) { Direction = ParameterDirection.Output }; commond.Parameters.Add(parameter); } parameter = new SqlParameter("@name", SqlDbType.NVarChar, 150) { Value = group.Name }; commond.Parameters.Add(parameter); commond.Prepare(); await commond.ExecuteNonQueryAsync(); if (group.ID == Guid.Empty) { group.ID = (Guid)commond.Parameters["@newid"].Value; } } }); }