StringBuilder strBuild = new StringBuilder(_SelectStatEffect);
if (orgID != "")
{
strBuild.AppendFormat(" and C1.OrgFullCode like '{0}%' {1}", orgID, strFilter);
}
else
{
strBuild.AppendFormat(" {0}", strFilter);
}
strBuild.AppendFormat(" group by ORGFULLCODE,ORGNAME,paytype,DATEEFFECTED,currentstep)");
public IList<UserInfo> GetAllContent(int pageSize, int pageIndex, string logName)
{
try
{
IQueryable<UserInfo> query = DbContext.UserInfo;
if (!string.IsNullOrEmpty(logName))
{
query = query.Where(i => i.LoginName.Contains(logName));
}
var conList = query.AsEnumerable().Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList<UserInfo>();
return conList;
}
catch (Exception e)
{
return null;
}
}
#8
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_PageQuery]
@pageSize int=20,
@SortFile varchar(50)='',
@SortType varchar(20)='asc',
@QuerySQL varchar(8000),
@PageIndex int=1,
@Total int output
AS
BEGIN
declare @SQL nvarchar(4000),@SQLTotal nvarchar(4000)
set @SQLTotal='select @Total=count(*) from ('+@QuerySQL +') as totaltable'
exec sp_executesql @SQLTotal,N'@Total int OUTPUT',@Total OUTPUT
set @SQL='SELECT TOP '+CAST(@pageSize as varchar)+' * '
+'FROM '
+'( '
+' SELECT ROW_NUMBER() OVER ('
if @SortFile<>''
set @SQL=@SQL+' ORDER BY '+@SortFile+' '+@SortType
set @SQL=@SQL +' ) as RowNO,*'
+' FROM ( '+@QuerySQL+') as DataTable'
+' ) as PageTable '
+'WHERE ROWNO>'+CAST( (@PageIndex-1)*@pageSize as nvarchar)
exec (@SQL)
END
#9
多表查询分页情况如何处理 ??? 表数量 都是不固定的
#10
SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO CREATE PROCEDURE [dbo].[sp_PageQuery] @pageSize int=20, @SortFile varchar(50)='', @SortType varchar(20)='asc', @QuerySQL varchar(8000), @PageIndex int=1, @Total int outputASBEGIN declare @SQL nvarchar(4000),@SQLTotal nvarchar(4000) set @SQLTotal='select @Total=count(*) from ('+@QuerySQL +') as totaltable' exec sp_executesql @SQLTotal,N'@Total int OUTPUT',@Total OUTPUT set @SQL='SELECT TOP '+CAST(@pageSize as varchar)+' * ' +'FROM ' +'( ' +' SELECT ROW_NUMBER() OVER (' if @SortFile<>'' set @SQL=@SQL+' ORDER BY '+@SortFile+' '+@SortType set @SQL=@SQL +' ) as RowNO,*' +' FROM ( '+@QuerySQL+') as DataTable' +' ) as PageTable ' +'WHERE ROWNO>'+CAST( (@PageIndex-1)*@pageSize as nvarchar) exec (@SQL) END
不使用存储过程
#11
多表用连接查询啊
IQueryable<GroupInfo> query = (from p in DbContext.MicroLetterGroup
join n in DbContext.UserInfo on p.Founder equals n.UserId
into a
from b in a.DefaultIfEmpty()
select new GroupInfo
{
GroupId = p.GroupId,
GroupName = p.GroupName,
Founder = p.Founder,
CreateTime = p.CreateTime,
IsDelete = p.IsDelete,
FounderName = b.UserName
});
多表用连接查询啊
IQueryable<GroupInfo> query = (from p in DbContext.MicroLetterGroup
join n in DbContext.UserInfo on p.Founder equals n.UserId
into a
from b in a.DefaultIfEmpty()
select new GroupInfo
{
GroupId = p.GroupId,
GroupName = p.GroupName,
Founder = p.Founder,
CreateTime = p.CreateTime,
IsDelete = p.IsDelete,
FounderName = b.UserName
});
不同的查询逻辑不一样,SQL也不一样,这个无法通用???
#20
public DataSet GetinfoList(string table, int pageindex, int pagesize, string orderFid, string Conditions, string[] strparam, object[] strval)
{
string query = string.Empty;
if (ConfigurationManager.ConnectionStrings["DbType"].ToString() == "Access")
{
if (pageindex <= 1)
{
query = "Select top {0} * from {1} {2} Order by {3}";
query = string.Format(query, pagesize, table, Conditions, orderFid, (pageindex - 1) * pagesize);
}
else
{
query = "select a.* from ( select top {0} * from {1} {2} Order by {3} ) a left join ( select top {4} * from {1} {2} Order by {3} ) b on a.id=b.id where iif(b.id,'0','1')='1'";
query = string.Format(query, pageindex * pagesize, table, Conditions, orderFid, (pageindex - 1) * pagesize);
}
}
else if (ConfigurationManager.ConnectionStrings["DbType"].ToString() == "SQL2000")
{
query = "select a.* from (select top {0} * from {1} {2} Order by {3}) a left join ( select top {4} * from {1} {2} Order by {3}) b on a.id=b.id where b.id is null";
query = string.Format(query, pageindex * pagesize, table, Conditions, orderFid, (pageindex - 1) * pagesize);
}
else
{
query = "Select top {0} * From "
+ "(Select *,ROW_NUMBER() OVER(ORDER BY {2}) as RowNum From {4} {3}) as newTable "
+ "Where (RowNum > {1})";
query = string.Format(query, pagesize, (pageindex - 1) * pagesize, orderFid, Conditions, table);
}
return db.GetDataSet(query, strparam, strval);
}
public int GetRowCount(string table, string cond, string[] strparam, object[] strval)
{
}
一般是这样的
private void BindData()
{
string _cond = " Where isCheck=1 and sid=@sid ";
string[] strparam={"@sid"};
object[] objvalue={sid};
rp_news.DataSource = GetinfoList("gl_NewsSystem", AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, "fbdate DESC,ID DESC", _cond, strparam, objvalue);
rp_news.DataBind();
AspNetPager1.RecordCount = GetRowCount("gl_NewsSystem", _cond, strparam, objvalue);
}
当遇到多表关联查询时,我要先把多表关联做成一个视图,然后把视图带进去就可以了
CREATE VIEW [dbo].[v_gl_k_market]
AS
Select qs.*,
sp.f2 as sp_f2,sp.f3 as sp_f3,sp.f5 as sp_f5 ,
ot.classID,ot.PicUrl,ot.PicThumb,ot.Weight,ot.EMS,ot.Explain,ot.Intro,ot.sdateTime,ot.edateTIme,ot.price3,ot.price3UserID,ot.price3Date,ot.Times,ot.hits,
case isnull(ot.Status,0)
when 99 then
case datediff(ss,sdateTime,getdate())/abs(datediff(ss,sdateTime,getdate()))
when -1 then
'未开始'
else
case datediff(ss,edateTIme,getdate())/abs(datediff(ss,edateTIme,getdate()))
when 1 then
'已结束'
else
'进行中'
end
end
when 0 then '未设置'
when 3 then '已开标'
when 4 then '已结算'
end as [Status],
case isnull(ot.ItemID,0)
when 0 then '线下用户'
when -1 then '流标标的'
else
ot.WinUserID
end as WinUserID
, ot.price4
,ot.OrderID
From k_qs_bh qs
left outer join k_sp_list sp on qs.f2=sp.f4
left outer join k_Auction ot on qs.id=ot.qs_bh_ID
public IList<UserInfo> GetAllContent(int pageSize, int pageIndex, string logName)
{
try
{
IQueryable<UserInfo> query = DbContext.UserInfo;
if (!string.IsNullOrEmpty(logName))
{
query = query.Where(i => i.LoginName.Contains(logName));
}
var conList = query.AsEnumerable().Skip(pageSize * (pageIndex - 1)).Take(pageSize).ToList<UserInfo>();
return conList;
}
catch (Exception e)
{
return null;
}
}
#8
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[sp_PageQuery]
@pageSize int=20,
@SortFile varchar(50)='',
@SortType varchar(20)='asc',
@QuerySQL varchar(8000),
@PageIndex int=1,
@Total int output
AS
BEGIN
declare @SQL nvarchar(4000),@SQLTotal nvarchar(4000)
set @SQLTotal='select @Total=count(*) from ('+@QuerySQL +') as totaltable'
exec sp_executesql @SQLTotal,N'@Total int OUTPUT',@Total OUTPUT
set @SQL='SELECT TOP '+CAST(@pageSize as varchar)+' * '
+'FROM '
+'( '
+' SELECT ROW_NUMBER() OVER ('
if @SortFile<>''
set @SQL=@SQL+' ORDER BY '+@SortFile+' '+@SortType
set @SQL=@SQL +' ) as RowNO,*'
+' FROM ( '+@QuerySQL+') as DataTable'
+' ) as PageTable '
+'WHERE ROWNO>'+CAST( (@PageIndex-1)*@pageSize as nvarchar)
exec (@SQL)
END
SET ANSI_NULLS ONGO SET QUOTED_IDENTIFIER ONGO CREATE PROCEDURE [dbo].[sp_PageQuery] @pageSize int=20, @SortFile varchar(50)='', @SortType varchar(20)='asc', @QuerySQL varchar(8000), @PageIndex int=1, @Total int outputASBEGIN declare @SQL nvarchar(4000),@SQLTotal nvarchar(4000) set @SQLTotal='select @Total=count(*) from ('+@QuerySQL +') as totaltable' exec sp_executesql @SQLTotal,N'@Total int OUTPUT',@Total OUTPUT set @SQL='SELECT TOP '+CAST(@pageSize as varchar)+' * ' +'FROM ' +'( ' +' SELECT ROW_NUMBER() OVER (' if @SortFile<>'' set @SQL=@SQL+' ORDER BY '+@SortFile+' '+@SortType set @SQL=@SQL +' ) as RowNO,*' +' FROM ( '+@QuerySQL+') as DataTable' +' ) as PageTable ' +'WHERE ROWNO>'+CAST( (@PageIndex-1)*@pageSize as nvarchar) exec (@SQL) END
多表用连接查询啊
IQueryable<GroupInfo> query = (from p in DbContext.MicroLetterGroup
join n in DbContext.UserInfo on p.Founder equals n.UserId
into a
from b in a.DefaultIfEmpty()
select new GroupInfo
{
GroupId = p.GroupId,
GroupName = p.GroupName,
Founder = p.Founder,
CreateTime = p.CreateTime,
IsDelete = p.IsDelete,
FounderName = b.UserName
});
多表用连接查询啊
IQueryable<GroupInfo> query = (from p in DbContext.MicroLetterGroup
join n in DbContext.UserInfo on p.Founder equals n.UserId
into a
from b in a.DefaultIfEmpty()
select new GroupInfo
{
GroupId = p.GroupId,
GroupName = p.GroupName,
Founder = p.Founder,
CreateTime = p.CreateTime,
IsDelete = p.IsDelete,
FounderName = b.UserName
});
不同的查询逻辑不一样,SQL也不一样,这个无法通用???
#20
public DataSet GetinfoList(string table, int pageindex, int pagesize, string orderFid, string Conditions, string[] strparam, object[] strval)
{
string query = string.Empty;
if (ConfigurationManager.ConnectionStrings["DbType"].ToString() == "Access")
{
if (pageindex <= 1)
{
query = "Select top {0} * from {1} {2} Order by {3}";
query = string.Format(query, pagesize, table, Conditions, orderFid, (pageindex - 1) * pagesize);
}
else
{
query = "select a.* from ( select top {0} * from {1} {2} Order by {3} ) a left join ( select top {4} * from {1} {2} Order by {3} ) b on a.id=b.id where iif(b.id,'0','1')='1'";
query = string.Format(query, pageindex * pagesize, table, Conditions, orderFid, (pageindex - 1) * pagesize);
}
}
else if (ConfigurationManager.ConnectionStrings["DbType"].ToString() == "SQL2000")
{
query = "select a.* from (select top {0} * from {1} {2} Order by {3}) a left join ( select top {4} * from {1} {2} Order by {3}) b on a.id=b.id where b.id is null";
query = string.Format(query, pageindex * pagesize, table, Conditions, orderFid, (pageindex - 1) * pagesize);
}
else
{
query = "Select top {0} * From "
+ "(Select *,ROW_NUMBER() OVER(ORDER BY {2}) as RowNum From {4} {3}) as newTable "
+ "Where (RowNum > {1})";
query = string.Format(query, pagesize, (pageindex - 1) * pagesize, orderFid, Conditions, table);
}
return db.GetDataSet(query, strparam, strval);
}
public int GetRowCount(string table, string cond, string[] strparam, object[] strval)
{
}
一般是这样的
private void BindData()
{
string _cond = " Where isCheck=1 and sid=@sid ";
string[] strparam={"@sid"};
object[] objvalue={sid};
rp_news.DataSource = GetinfoList("gl_NewsSystem", AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, "fbdate DESC,ID DESC", _cond, strparam, objvalue);
rp_news.DataBind();
AspNetPager1.RecordCount = GetRowCount("gl_NewsSystem", _cond, strparam, objvalue);
}
当遇到多表关联查询时,我要先把多表关联做成一个视图,然后把视图带进去就可以了
CREATE VIEW [dbo].[v_gl_k_market]
AS
Select qs.*,
sp.f2 as sp_f2,sp.f3 as sp_f3,sp.f5 as sp_f5 ,
ot.classID,ot.PicUrl,ot.PicThumb,ot.Weight,ot.EMS,ot.Explain,ot.Intro,ot.sdateTime,ot.edateTIme,ot.price3,ot.price3UserID,ot.price3Date,ot.Times,ot.hits,
case isnull(ot.Status,0)
when 99 then
case datediff(ss,sdateTime,getdate())/abs(datediff(ss,sdateTime,getdate()))
when -1 then
'未开始'
else
case datediff(ss,edateTIme,getdate())/abs(datediff(ss,edateTIme,getdate()))
when 1 then
'已结束'
else
'进行中'
end
end
when 0 then '未设置'
when 3 then '已开标'
when 4 then '已结算'
end as [Status],
case isnull(ot.ItemID,0)
when 0 then '线下用户'
when -1 then '流标标的'
else
ot.WinUserID
end as WinUserID
, ot.price4
,ot.OrderID
From k_qs_bh qs
left outer join k_sp_list sp on qs.f2=sp.f4
left outer join k_Auction ot on qs.id=ot.qs_bh_ID