Beispiel #1
0
        /// <summary>
        /// 为队列从DbDataReader中赋值
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="reader"></param>
        /// <param name="prefix"></param>
        public static void SetCacheRemoteConfigurationSelectFields(CacheRemoteConfiguration configuration, DbDataReader reader, string prefix)
        {
            configuration.ID = (Guid)reader[string.Format("{0}id", prefix)];

            if (reader[string.Format("{0}name", prefix)] != DBNull.Value)
            {
                configuration.Name = reader[string.Format("{0}name", prefix)].ToString();
            }

            if (reader[string.Format("{0}remoteaddresses", prefix)] != DBNull.Value)
            {
                configuration.RemoteAddresses = reader[string.Format("{0}remoteaddresses", prefix)].ToString();
            }


            if (reader[string.Format("{0}createtime", prefix)] != DBNull.Value)
            {
                configuration.CreateTime = (DateTime)reader[string.Format("{0}createtime", prefix)];
            }

            if (reader[string.Format("{0}modifytime", prefix)] != DBNull.Value)
            {
                configuration.ModifyTime = (DateTime)reader[string.Format("{0}modifytime", prefix)];
            }
        }
Beispiel #2
0
        public async Task <CacheRemoteConfiguration> QueryByName(string name)
        {
            CacheRemoteConfiguration configuration = null;

            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _cacheConnectionFactory.CreateReadForCache(), async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran,
                    CommandText = string.Format(@"select {0} from CacheRemoteConfiguration where [name]=@name", StoreHelper.GetCacheRemoteConfigurationSelectFields(string.Empty))
                })
                {
                    var parameter = new SqlParameter("@name", SqlDbType.NVarChar, 150)
                    {
                        Value = name
                    };
                    commond.Parameters.Add(parameter);

                    await commond.PrepareAsync();


                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        if (await reader.ReadAsync())
                        {
                            configuration = new CacheRemoteConfiguration();
                            StoreHelper.SetCacheRemoteConfigurationSelectFields(configuration, reader, string.Empty);
                        }

                        reader.Close();
                    }
                }
            });

            return(configuration);
        }
Beispiel #3
0
        public async Task Update(CacheRemoteConfiguration configuration)
        {
            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _cacheConnectionFactory.CreateAllForCache(), async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran,
                    CommandText = @"update SQueue set [name]=@name,[remoteaddresses]=@remoteaddresses,[modifytime]=getutcdate()
                                    where [id]=@id"
                })
                {
                    var parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                    {
                        Value = configuration.ID
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@name", SqlDbType.NVarChar, 150)
                    {
                        Value = configuration.Name
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@remoteaddresses", SqlDbType.NVarChar, 1000)
                    {
                        Value = configuration.RemoteAddresses
                    };
                    commond.Parameters.Add(parameter);


                    await commond.PrepareAsync();

                    await commond.ExecuteNonQueryAsync();
                }
            });
        }
Beispiel #4
0
        public async Task Add(CacheRemoteConfiguration configuration)
        {
            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, false, false, _cacheConnectionFactory.CreateAllForCache(), async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran
                })
                {
                    if (configuration.ID == Guid.Empty)
                    {
                        commond.CommandText = @"insert into CacheRemoteConfiguration([id],[name],[remoteaddresses],[createtime],[modifytime])
                                    values(default,@name,@remoteaddresses,getutcdate(),getutcdate());
                                    select @newid=[id] from CacheRemoteConfiguration where [sequence]=SCOPE_IDENTITY()";
                    }
                    else
                    {
                        commond.CommandText = @"insert into CacheRemoteConfiguration([id],[name],[remoteaddresses],[createtime],[modifytime])
                                    values(@id,@name,@code,@remoteaddresses,getutcdate(),getutcdate())";
                    }

                    SqlParameter parameter;
                    if (configuration.ID != Guid.Empty)
                    {
                        parameter = new SqlParameter("@id", SqlDbType.UniqueIdentifier)
                        {
                            Value = configuration.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, 100)
                    {
                        Value = configuration.Name
                    };
                    commond.Parameters.Add(parameter);

                    parameter = new SqlParameter("@remoteaddresses", SqlDbType.NChar, 1000)
                    {
                        Value = configuration.RemoteAddresses
                    };
                    commond.Parameters.Add(parameter);

                    await commond.PrepareAsync();

                    await commond.ExecuteNonQueryAsync();



                    if (configuration.ID == Guid.Empty)
                    {
                        configuration.ID = (Guid)commond.Parameters["@newid"].Value;
                    }
                }
            });
        }
Beispiel #5
0
        public async Task <QueryResult <CacheRemoteConfiguration> > QueryByName(string name, int page, int pageSize)
        {
            QueryResult <CacheRemoteConfiguration> result = new QueryResult <CacheRemoteConfiguration>();

            await DBTransactionHelper.SqlTransactionWorkAsync(DBTypes.SqlServer, true, false, _cacheConnectionFactory.CreateReadForCache(), async (conn, transaction) =>
            {
                SqlTransaction sqlTran = null;
                if (transaction != null)
                {
                    sqlTran = (SqlTransaction)transaction;
                }

                await using (SqlCommand commond = new SqlCommand()
                {
                    Connection = (SqlConnection)conn,
                    CommandType = CommandType.Text,
                    Transaction = sqlTran,
                    CommandText = string.Format(@"set @currentpage=@page
		                           select @count= count(*) from CacheRemoteConfiguration 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 CacheRemoteConfiguration where [name] like @name
                                    order by [sequence]
		                            offset (@pagesize * (@currentpage - 1)) rows 
		                            fetch next @pagesize rows only;"        , StoreHelper.GetCacheRemoteConfigurationSelectFields(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);

                    await commond.PrepareAsync();

                    SqlDataReader reader = null;

                    await using (reader = await commond.ExecuteReaderAsync())
                    {
                        while (await reader.ReadAsync())
                        {
                            var configuration = new CacheRemoteConfiguration();
                            StoreHelper.SetCacheRemoteConfigurationSelectFields(configuration, reader, string.Empty);
                            result.Results.Add(configuration);
                        }

                        await reader.CloseAsync();

                        result.TotalCount  = (int)commond.Parameters["@count"].Value;
                        result.CurrentPage = (int)commond.Parameters["@currentpage"].Value;
                    }
                }
            });

            return(result);
        }