多条件组合查询+分页
作者:admin 日期:2008-06-04
比较麻烦,把他记录下来。
前台UI:
实现的类代码:
///
/// 多条件组合查询
///
/// 供应商代码
/// 物料代码
/// 合同有效日期搜索开始日期
/// 合同有效日期搜索结束日期
/// 厂别
/// 状态
/// POCT
/// 排序字段
///
public IList MultiSearch(string suppliercode, string materialcode, string beginDate, string endDate, string companyType, string formState, string POCT, string orderField, int pagesize, int pageindex)
{
SqlParameter[] Parms =
{
new SqlParameter("@tblName",SqlDbType.VarChar,255),
new SqlParameter("@strGetFields",SqlDbType.VarChar,1000),
new SqlParameter("@fldName",SqlDbType.VarChar,255),
new SqlParameter("@PageSize",SqlDbType.Int,4),
new SqlParameter("@PageIndex",SqlDbType.Int,4),
new SqlParameter("@doCount",SqlDbType.Int,4),
new SqlParameter("@OrderType",SqlDbType.Int,4),
new SqlParameter("@strWhere",SqlDbType.VarChar,1500),
};
Parms[0].Value = "UBS_CAS_PriceForm";
Parms[1].Value = "*";
Parms[2].Value = "FormID";
Parms[3].Value = pagesize;
Parms[4].Value = pageindex;
Parms[5].Value = 0;
Parms[6].Value = 1;
string sql_select="1=1";
if(!string.IsNullOrEmpty(suppliercode))
{
sql_select+="and suppliercode='"+suppliercode.ToString()+"'";
}
if(!string.IsNullOrEmpty(materialcode))
{
sql_select+=" and materialcode='"+materialcode.ToString()+"'";
}
if (!string.IsNullOrEmpty(companyType.ToString()))
{
sql_select += " and companytype=" + companyType.ToString();
}
if(!string.IsNullOrEmpty(POCT))
{
sql_select+=" and POCT='"+POCT.ToString() +"'";
}
if(!string.IsNullOrEmpty(formState.ToString()))
{
sql_select+=" and formState="+formState.ToString() ;
}
if(!string.IsNullOrEmpty(beginDate.ToString()) && !string.IsNullOrEmpty(endDate.ToString()))
{
sql_select+= " And [EffectiveDate] >='" + beginDate.ToString() + "' And [EffectiveDate]<= '" + endDate.ToString() + " '";
}
Parms[7].Value = sql_select;
//Parms[7].Value = "1=1 and suppliercode is null or suppliercode='" + suppliercode.ToString().Trim() + "' and materialcode is null or materialcode='" + materialcode.ToString() + "' AND companytype is null or CompanyType=" + companyType.ToString() + " AND POCT is null or POCT='" + POCT.ToString() + "' AND EffectiveDate between '" + beginDate.ToString() + "' AND '" + endDate.ToString() + "'";
IList FormList = new List();
using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringProfile, CommandType.StoredProcedure, "UBS_System_Page", Parms))
{
while (dr.Read())
{
Entity.CAS.PriceFormInfo priceForm = new Entity.CAS.PriceFormInfo(Convert.ToInt32(dr["formID"]), dr["materialCode"].ToString().Trim(), dr["materialDesc"].ToString().Trim(), dr["supplierCode"].ToString().Trim(), dr["supplierName"].ToString().Trim(), dr["payment"].ToString().Trim(), Convert.ToDouble(dr["tax"]), dr["currencyType"].ToString().Trim(), Convert.ToDateTime(dr["effectiveDate"]), dr["processMarDealWay"].ToString().Trim(), dr["productMarDealWay"].ToString().Trim(), dr["guaranDealWay"].ToString().Trim(), dr["idelDealWay"].ToString().Trim(), Convert.ToInt32(dr["companyType"]), dr["purchaser"].ToString().Trim(), Convert.ToInt32(dr["purchaserUID"]), dr["auditor"].ToString().Trim(), Convert.ToInt32(dr["auditorUID"]), dr["description"].ToString().Trim(), Convert.ToDateTime(dr["pubDate"]), Convert.ToInt32(dr["formState"]), dr["rejectReason"].ToString().Trim(), Convert.ToInt32(dr["itemCostModelID"]), Convert.ToDouble(dr["price"]), Convert.ToDateTime(dr["checkedDate"]), Convert.ToDouble(dr["profits"]), Convert.ToDouble(dr["totalTaxPercent"]), Convert.ToDouble(dr["processMarTax"]), Convert.ToDouble(dr["productMarTax"]), Convert.ToDouble(dr["guaranTax"]), Convert.ToInt32(dr["IsCurrentPrice"]), Convert.ToInt32(dr["FormType"]), dr["POCT"].ToString().Trim(), Convert.ToInt32(dr["MaterialClass"]), dr["Checker"].ToString().Trim(), Convert.ToInt32(dr["CheckerUID"]), Convert.ToInt32(dr["IsBatch"]), dr["AttributePath"].ToString().Trim(), dr["BatchNo"].ToString().Trim());
FormList.Add(priceForm);
}
}
return FormList;
}
存储过程实现代码:(这个可以通用,在SQL或Access都可以用的)
-- =============================================
-- Author:Dezai www.Dezai.cn
-- Create date:2008-06-04 11:19:52
-- Description:查询分页集合(通用存储过程)
-- =============================================
Create PROCEDURE UP_GetCollectionPage
(
@tblName VARCHAR(255), -- 表名
@strGetFields VARCHAR(1000) = '*', -- 需要返回的列
@fldName VARCHAR(255)='', -- 排序的字段名
@PageSize INT = 10, -- 页尺寸
@PageIndex INT = 1, -- 页码
@doCount BIT = 0, -- 返回记录总数, 非 0 值则返回
@OrderType BIT = 0, -- 设置排序类型, 非 0 值则降序
@strWhere VARCHAR(1500) = '' -- 查询条件 (注意: 不要加 Where)
)
AS
SET NOCOUNT ON
DECLARE @strSQL VARCHAR(5000) -- 主语句
DECLARE @strTmp VARCHAR(110) -- 临时变量
DECLARE @strOrder VARCHAR(400) -- 排序类型
IF(@doCount != 0)
BEGIN
IF @strWhere !=''
SET @strSQL = 'Select COUNT(1) AS Total FROM [' + @tblName + '] Where '+@strWhere
ELSE
SET @strSQL = 'Select COUNT(1) AS Total FROM [' + @tblName + ']'
END
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
ELSE
BEGIN
IF @OrderType != 0
BEGIN
SET @strTmp = '<(Select MIN'
SET @strOrder = ' orDER BY [' + @fldName +'] DESC'
--如果@OrderType不是0,就执行降序,这句很重要
END
ELSE
BEGIN
SET @strTmp = '>(Select MAX'
SET @strOrder = ' orDER BY [' + @fldName +'] ASC'
END
IF @PageIndex = 1
BEGIN
IF @strWhere != ''
SET @strSQL = 'Select TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM [' + @tblName + '] Where ' + @strWhere + ' ' + @strOrder
ELSE
SET @strSQL = 'Select TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM ['+ @tblName + '] '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
END
ELSE
BEGIN
--以下代码赋予了@strSQL以真正执行的SQL代码
SET @strSQL = 'Select TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM ['
+ @tblName + '] Where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) FROM (Select TOP ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] FROM [' + @tblName + ']' + @strOrder + ') AS tblTmp)'+ @strOrder
IF @strWhere != ''
SET @strSQL = 'Select TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM ['
+ @tblName + '] Where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) FROM (Select TOP ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] FROM [' + @tblName + '] Where ' + @strWhere + ' '
+ @strOrder + ') AS tblTmp) AND ' + @strWhere + ' ' + @strOrder
END
END
EXEC (@strSQL)
SET NOCOUNT OFF
GO
记录一下,与大家分享,欢迎纠错。[em02]
前台UI:
实现的类代码:
复制内容到剪贴板 程序代码
///
/// 多条件组合查询
///
/// 供应商代码
/// 物料代码
/// 合同有效日期搜索开始日期
/// 合同有效日期搜索结束日期
/// 厂别
/// 状态
/// POCT
/// 排序字段
///
public IList
{
SqlParameter[] Parms =
{
new SqlParameter("@tblName",SqlDbType.VarChar,255),
new SqlParameter("@strGetFields",SqlDbType.VarChar,1000),
new SqlParameter("@fldName",SqlDbType.VarChar,255),
new SqlParameter("@PageSize",SqlDbType.Int,4),
new SqlParameter("@PageIndex",SqlDbType.Int,4),
new SqlParameter("@doCount",SqlDbType.Int,4),
new SqlParameter("@OrderType",SqlDbType.Int,4),
new SqlParameter("@strWhere",SqlDbType.VarChar,1500),
};
Parms[0].Value = "UBS_CAS_PriceForm";
Parms[1].Value = "*";
Parms[2].Value = "FormID";
Parms[3].Value = pagesize;
Parms[4].Value = pageindex;
Parms[5].Value = 0;
Parms[6].Value = 1;
string sql_select="1=1";
if(!string.IsNullOrEmpty(suppliercode))
{
sql_select+="and suppliercode='"+suppliercode.ToString()+"'";
}
if(!string.IsNullOrEmpty(materialcode))
{
sql_select+=" and materialcode='"+materialcode.ToString()+"'";
}
if (!string.IsNullOrEmpty(companyType.ToString()))
{
sql_select += " and companytype=" + companyType.ToString();
}
if(!string.IsNullOrEmpty(POCT))
{
sql_select+=" and POCT='"+POCT.ToString() +"'";
}
if(!string.IsNullOrEmpty(formState.ToString()))
{
sql_select+=" and formState="+formState.ToString() ;
}
if(!string.IsNullOrEmpty(beginDate.ToString()) && !string.IsNullOrEmpty(endDate.ToString()))
{
sql_select+= " And [EffectiveDate] >='" + beginDate.ToString() + "' And [EffectiveDate]<= '" + endDate.ToString() + " '";
}
Parms[7].Value = sql_select;
//Parms[7].Value = "1=1 and suppliercode is null or suppliercode='" + suppliercode.ToString().Trim() + "' and materialcode is null or materialcode='" + materialcode.ToString() + "' AND companytype is null or CompanyType=" + companyType.ToString() + " AND POCT is null or POCT='" + POCT.ToString() + "' AND EffectiveDate between '" + beginDate.ToString() + "' AND '" + endDate.ToString() + "'";
IList
using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringProfile, CommandType.StoredProcedure, "UBS_System_Page", Parms))
{
while (dr.Read())
{
Entity.CAS.PriceFormInfo priceForm = new Entity.CAS.PriceFormInfo(Convert.ToInt32(dr["formID"]), dr["materialCode"].ToString().Trim(), dr["materialDesc"].ToString().Trim(), dr["supplierCode"].ToString().Trim(), dr["supplierName"].ToString().Trim(), dr["payment"].ToString().Trim(), Convert.ToDouble(dr["tax"]), dr["currencyType"].ToString().Trim(), Convert.ToDateTime(dr["effectiveDate"]), dr["processMarDealWay"].ToString().Trim(), dr["productMarDealWay"].ToString().Trim(), dr["guaranDealWay"].ToString().Trim(), dr["idelDealWay"].ToString().Trim(), Convert.ToInt32(dr["companyType"]), dr["purchaser"].ToString().Trim(), Convert.ToInt32(dr["purchaserUID"]), dr["auditor"].ToString().Trim(), Convert.ToInt32(dr["auditorUID"]), dr["description"].ToString().Trim(), Convert.ToDateTime(dr["pubDate"]), Convert.ToInt32(dr["formState"]), dr["rejectReason"].ToString().Trim(), Convert.ToInt32(dr["itemCostModelID"]), Convert.ToDouble(dr["price"]), Convert.ToDateTime(dr["checkedDate"]), Convert.ToDouble(dr["profits"]), Convert.ToDouble(dr["totalTaxPercent"]), Convert.ToDouble(dr["processMarTax"]), Convert.ToDouble(dr["productMarTax"]), Convert.ToDouble(dr["guaranTax"]), Convert.ToInt32(dr["IsCurrentPrice"]), Convert.ToInt32(dr["FormType"]), dr["POCT"].ToString().Trim(), Convert.ToInt32(dr["MaterialClass"]), dr["Checker"].ToString().Trim(), Convert.ToInt32(dr["CheckerUID"]), Convert.ToInt32(dr["IsBatch"]), dr["AttributePath"].ToString().Trim(), dr["BatchNo"].ToString().Trim());
FormList.Add(priceForm);
}
}
return FormList;
}
存储过程实现代码:(这个可以通用,在SQL或Access都可以用的)
复制内容到剪贴板 程序代码
-- =============================================
-- Author:Dezai www.Dezai.cn
-- Create date:2008-06-04 11:19:52
-- Description:查询分页集合(通用存储过程)
-- =============================================
Create PROCEDURE UP_GetCollectionPage
(
@tblName VARCHAR(255), -- 表名
@strGetFields VARCHAR(1000) = '*', -- 需要返回的列
@fldName VARCHAR(255)='', -- 排序的字段名
@PageSize INT = 10, -- 页尺寸
@PageIndex INT = 1, -- 页码
@doCount BIT = 0, -- 返回记录总数, 非 0 值则返回
@OrderType BIT = 0, -- 设置排序类型, 非 0 值则降序
@strWhere VARCHAR(1500) = '' -- 查询条件 (注意: 不要加 Where)
)
AS
SET NOCOUNT ON
DECLARE @strSQL VARCHAR(5000) -- 主语句
DECLARE @strTmp VARCHAR(110) -- 临时变量
DECLARE @strOrder VARCHAR(400) -- 排序类型
IF(@doCount != 0)
BEGIN
IF @strWhere !=''
SET @strSQL = 'Select COUNT(1) AS Total FROM [' + @tblName + '] Where '+@strWhere
ELSE
SET @strSQL = 'Select COUNT(1) AS Total FROM [' + @tblName + ']'
END
--以上代码的意思是如果@doCount传递过来的不是0,就执行总数统计。以下的所有代码都是@doCount为0的情况
ELSE
BEGIN
IF @OrderType != 0
BEGIN
SET @strTmp = '<(Select MIN'
SET @strOrder = ' orDER BY [' + @fldName +'] DESC'
--如果@OrderType不是0,就执行降序,这句很重要
END
ELSE
BEGIN
SET @strTmp = '>(Select MAX'
SET @strOrder = ' orDER BY [' + @fldName +'] ASC'
END
IF @PageIndex = 1
BEGIN
IF @strWhere != ''
SET @strSQL = 'Select TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM [' + @tblName + '] Where ' + @strWhere + ' ' + @strOrder
ELSE
SET @strSQL = 'Select TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM ['+ @tblName + '] '+ @strOrder
--如果是第一页就执行以上代码,这样会加快执行速度
END
ELSE
BEGIN
--以下代码赋予了@strSQL以真正执行的SQL代码
SET @strSQL = 'Select TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM ['
+ @tblName + '] Where [' + @fldName + ']' + @strTmp + '(['+ @fldName + ']) FROM (Select TOP ' + str((@PageIndex-1)*@PageSize) + ' ['+ @fldName + '] FROM [' + @tblName + ']' + @strOrder + ') AS tblTmp)'+ @strOrder
IF @strWhere != ''
SET @strSQL = 'Select TOP ' + str(@PageSize) +' '+@strGetFields+ ' FROM ['
+ @tblName + '] Where [' + @fldName + ']' + @strTmp + '(['
+ @fldName + ']) FROM (Select TOP ' + str((@PageIndex-1)*@PageSize) + ' ['
+ @fldName + '] FROM [' + @tblName + '] Where ' + @strWhere + ' '
+ @strOrder + ') AS tblTmp) AND ' + @strWhere + ' ' + @strOrder
END
END
EXEC (@strSQL)
SET NOCOUNT OFF
GO
记录一下,与大家分享,欢迎纠错。[em02]
评论: 0 | 引用: 0 | 查看次数: 6112
发表评论