CREATE proc getdataset
@TableList Varchar(200)='*',--搜索表的字段,比如:’id,datatime,job‘,用逗号隔开
@TableName Varchar(30), --搜索的表名
@SelectWhere Varchar(500)='',--搜索条件,这里不用写where,比如:job=’teacher‘and class='2'
@SelectOrderId Varchar(20),--表主键字段名。比如:id
@SelectOrder Varchar(200)='', --排序,可以使用多字段排序但主键字段必需在最前面.也可以不写,比如:order by class asc
@intPageNo int=1, --页号
@intPageSize int=10 ,--每页显示数
@RecordCount int OUTPUT --总记录数(存储过程输出参数)
as
declare @TmpSelect NVarchar(600)
declare @Tmp NVarchar(600) set nocount on--关闭计数set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhereexecute sp_executesql
@TmpSelect, --执行上面的sql语句
N'@RecordCount int OUTPUT' , --执行输出数据的sql语句,output出总记录数
@RecordCount OUTPUT if (@RecordCount = 0) --如果没有贴子,则返回零
return 0
/*判断页数是否正确*/
if (@intPageNo - 1) * @intPageSize > @RecordCount --页号大于总页数,返回错误
return (-1)
set nocount off--打开计数
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)
GO
查询条件是不是错了 @SelectWhere Varchar(500)='',
该怎么给条件参数
public void bind()
{
int id = Convert.ToInt32(Request.QueryString["id"]);
int sumPage;
int pageNo = 1;
int pageSize = 10;
if (Request.QueryString["CurrentPage"] == null)
{
pageNo = 1;
}
else
{
pageNo = Int32.Parse(Request.QueryString["CurrentPage"]);
}
//SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=ajax;");
string cnnString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection conn = new SqlConnection(cnnString);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "getdataset";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@TableList", SqlDbType.VarChar, 200).Value = "id,title,datatime,centex,did";
da.SelectCommand.Parameters.Add("@TableName", SqlDbType.VarChar, 30).Value = "[title]";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where d=1";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where id=" + id + "";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.Int, 4).Value = " where did='" + id + "'";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.Int, 4).Value = " where did='" + id.ToString() + "'";
da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where did='" + id + "'";
da.SelectCommand.Parameters.Add("@SelectOrderId", SqlDbType.VarChar, 20).Value = "id";
da.SelectCommand.Parameters.Add("@SelectOrder", SqlDbType.VarChar, 200).Value = "order by id asc";
da.SelectCommand.Parameters.Add("@intPageNo", SqlDbType.Int).Value = pageNo;
da.SelectCommand.Parameters.Add("@intPageSize", SqlDbType.Int).Value = pageSize;
da.SelectCommand.Parameters.Add("@RecordCount", SqlDbType.Int).Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add("RowCount", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
DataSet ds = new DataSet();
da.Fill(ds, "title");
DataList1.DataSource = ds;
DataList1.DataBind();
Int32 RecordCount = (Int32)da.SelectCommand.Parameters["@RecordCount"].Value; //求出总记录数,该值是output出来的值
Int32 RowCount = (Int32)da.SelectCommand.Parameters["RowCount"].Value; //求出当前页中的记录数,在最后一页不等于pagesize,
lbRecord.Text = RecordCount.ToString();
lbRow.Text = pageNo.ToString();
sumPage = (Int32)RecordCount / pageSize;
if (RecordCount % pageSize > 0)
{
sumPage = sumPage + 1;
}
lbpage.Text = sumPage.ToString();
if (pageNo > 1)
{
hylfirst.NavigateUrl = "list.aspx?CurrentPage=1";
hylprev.NavigateUrl = string.Concat("list.aspx?CurrentPage=", "", pageNo - 1);
}
else
{
hylprev.NavigateUrl = "";
hylfirst.NavigateUrl = "";
hylfirst.Visible = false;
hylprev.Visible = false;
}
if (pageNo < sumPage)
{
hylend.NavigateUrl = string.Concat("list.aspx?CurrentPage=", "", sumPage);
hylnext.NavigateUrl = string.Concat("list.aspx?CurrentPage=", "", pageNo + 1);
}
else
{
hylnext.NavigateUrl = "";
hylend.NavigateUrl = "";
hylend.Visible = false;
hylnext.Visible = false;
} }这段该怎么改啊//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where d=1";
@TableList Varchar(200)='*',--搜索表的字段,比如:’id,datatime,job‘,用逗号隔开
@TableName Varchar(30), --搜索的表名
@SelectWhere Varchar(500)='',--搜索条件,这里不用写where,比如:job=’teacher‘and class='2'
@SelectOrderId Varchar(20),--表主键字段名。比如:id
@SelectOrder Varchar(200)='', --排序,可以使用多字段排序但主键字段必需在最前面.也可以不写,比如:order by class asc
@intPageNo int=1, --页号
@intPageSize int=10 ,--每页显示数
@RecordCount int OUTPUT --总记录数(存储过程输出参数)
as
declare @TmpSelect NVarchar(600)
declare @Tmp NVarchar(600) set nocount on--关闭计数set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhereexecute sp_executesql
@TmpSelect, --执行上面的sql语句
N'@RecordCount int OUTPUT' , --执行输出数据的sql语句,output出总记录数
@RecordCount OUTPUT if (@RecordCount = 0) --如果没有贴子,则返回零
return 0
/*判断页数是否正确*/
if (@intPageNo - 1) * @intPageSize > @RecordCount --页号大于总页数,返回错误
return (-1)
set nocount off--打开计数
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)
GO
查询条件是不是错了 @SelectWhere Varchar(500)='',
该怎么给条件参数
public void bind()
{
int id = Convert.ToInt32(Request.QueryString["id"]);
int sumPage;
int pageNo = 1;
int pageSize = 10;
if (Request.QueryString["CurrentPage"] == null)
{
pageNo = 1;
}
else
{
pageNo = Int32.Parse(Request.QueryString["CurrentPage"]);
}
//SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=ajax;");
string cnnString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection conn = new SqlConnection(cnnString);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "getdataset";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@TableList", SqlDbType.VarChar, 200).Value = "id,title,datatime,centex,did";
da.SelectCommand.Parameters.Add("@TableName", SqlDbType.VarChar, 30).Value = "[title]";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where d=1";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where id=" + id + "";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.Int, 4).Value = " where did='" + id + "'";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.Int, 4).Value = " where did='" + id.ToString() + "'";
da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where did='" + id + "'";
da.SelectCommand.Parameters.Add("@SelectOrderId", SqlDbType.VarChar, 20).Value = "id";
da.SelectCommand.Parameters.Add("@SelectOrder", SqlDbType.VarChar, 200).Value = "order by id asc";
da.SelectCommand.Parameters.Add("@intPageNo", SqlDbType.Int).Value = pageNo;
da.SelectCommand.Parameters.Add("@intPageSize", SqlDbType.Int).Value = pageSize;
da.SelectCommand.Parameters.Add("@RecordCount", SqlDbType.Int).Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add("RowCount", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
DataSet ds = new DataSet();
da.Fill(ds, "title");
DataList1.DataSource = ds;
DataList1.DataBind();
Int32 RecordCount = (Int32)da.SelectCommand.Parameters["@RecordCount"].Value; //求出总记录数,该值是output出来的值
Int32 RowCount = (Int32)da.SelectCommand.Parameters["RowCount"].Value; //求出当前页中的记录数,在最后一页不等于pagesize,
lbRecord.Text = RecordCount.ToString();
lbRow.Text = pageNo.ToString();
sumPage = (Int32)RecordCount / pageSize;
if (RecordCount % pageSize > 0)
{
sumPage = sumPage + 1;
}
lbpage.Text = sumPage.ToString();
if (pageNo > 1)
{
hylfirst.NavigateUrl = "list.aspx?CurrentPage=1";
hylprev.NavigateUrl = string.Concat("list.aspx?CurrentPage=", "", pageNo - 1);
}
else
{
hylprev.NavigateUrl = "";
hylfirst.NavigateUrl = "";
hylfirst.Visible = false;
hylprev.Visible = false;
}
if (pageNo < sumPage)
{
hylend.NavigateUrl = string.Concat("list.aspx?CurrentPage=", "", sumPage);
hylnext.NavigateUrl = string.Concat("list.aspx?CurrentPage=", "", pageNo + 1);
}
else
{
hylnext.NavigateUrl = "";
hylend.NavigateUrl = "";
hylend.Visible = false;
hylnext.Visible = false;
} }这段该怎么改啊//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where d=1";
你是在这里使用@SelectWhere的,那么你在前台应该用da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = " did=" + id.ToString(); //去掉"where"
exec getdataset
'第一字段,二字段,',
'table name',
'第一字段=x',
'第一字段',
'第一字段asc',
1,
10,
0实验成功
上面有源码,谁帮我试试
用了楼上的提示'=' 附近有语法错误。
望解答
存储过程能这么赋值么
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[getdataset]
@TableList Varchar(200)='*',
@TableName Varchar(30),
@SelectWhere Varchar(500)='',
@SelectOrderId Varchar(20),
@SelectOrder Varchar(200)='',
@intPageNo int=1,
@intPageSize int=10 ,
@RecordCount int OUTPUT
as
declare @TmpSelect NVarchar(600)
declare @Tmp NVarchar(600) set nocount onset @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' '+@SelectWhereexecute sp_executesql
@TmpSelect,
N'@RecordCount int OUTPUT' ,
@RecordCount OUTPUT if (@RecordCount = 0)
return 0
if (@intPageNo - 1) * @intPageSize > @RecordCount
return (-1)
set nocount off
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' '+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)cs代码using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;namespace Web
{
public partial class list : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.bind();
}
protected void link_Click(object sender, EventArgs e)
{
int page = Convert.ToInt32(txtlink.Text);
Response.Redirect("list.aspx?CurrentPage=" + page + "");
}
public void bind()
{
int id = Convert.ToInt32(Request.QueryString["id"]);
//string id = Request.QueryString["id"];
int sumPage;
int pageNo = 1;
int pageSize = 10;
if (Request.QueryString["CurrentPage"] == null)
{
pageNo = 1;
}
else
{
pageNo = Int32.Parse(Request.QueryString["CurrentPage"]);
}
//SqlConnection conn = new SqlConnection("server=.;uid=sa;pwd=123456;database=ajax;");
string cnnString = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
SqlConnection conn = new SqlConnection(cnnString);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand();
da.SelectCommand.Connection = conn;
da.SelectCommand.CommandText = "getdataset";
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.SelectCommand.Parameters.Add("@TableList", SqlDbType.VarChar, 200).Value = "id,title,datatime,centex,did";
da.SelectCommand.Parameters.Add("@TableName", SqlDbType.VarChar, 30).Value = "[title]";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where d=1";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where id=" + id + "";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.Int, 4).Value = " where did='" + id + "'";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.Int, 4).Value = " where did='" + id.ToString() + "'";
// da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where did='" + id + "'";
//da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "where did=" + id.ToString();
da.SelectCommand.Parameters.Add("@SelectWhere", SqlDbType.VarChar, 500).Value = "did=" + id.ToString();
da.SelectCommand.Parameters.Add("@SelectOrderId", SqlDbType.VarChar, 20).Value = "id";
da.SelectCommand.Parameters.Add("@SelectOrder", SqlDbType.VarChar, 200).Value = "order by id asc";
da.SelectCommand.Parameters.Add("@intPageNo", SqlDbType.Int).Value = pageNo;
da.SelectCommand.Parameters.Add("@intPageSize", SqlDbType.Int).Value = pageSize;
da.SelectCommand.Parameters.Add("@RecordCount", SqlDbType.Int).Direction = ParameterDirection.Output;
da.SelectCommand.Parameters.Add("RowCount", SqlDbType.Int).Direction = ParameterDirection.ReturnValue;
DataSet ds = new DataSet();
da.Fill(ds, "title");
DataList1.DataSource = ds;
DataList1.DataBind();
Int32 RecordCount = (Int32)da.SelectCommand.Parameters["@RecordCount"].Value; //求出总记录数,该值是output出来的值
Int32 RowCount = (Int32)da.SelectCommand.Parameters["RowCount"].Value; //求出当前页中的记录数,在最后一页不等于pagesize,
lbRecord.Text = RecordCount.ToString();
lbRow.Text = pageNo.ToString();
sumPage = (Int32)RecordCount / pageSize;
if (RecordCount % pageSize > 0)
{
sumPage = sumPage + 1;
}
lbpage.Text = sumPage.ToString();
if (pageNo > 1)
{
hylfirst.NavigateUrl = "list.aspx?CurrentPage=1";
hylprev.NavigateUrl = string.Concat("list.aspx?CurrentPage=", "", pageNo - 1);
}
else
{
hylprev.NavigateUrl = "";
hylfirst.NavigateUrl = "";
hylfirst.Visible = false;
hylprev.Visible = false;
}
if (pageNo < sumPage)
{
hylend.NavigateUrl = string.Concat("list.aspx?CurrentPage=", "", sumPage);
hylnext.NavigateUrl = string.Concat("list.aspx?CurrentPage=", "", pageNo + 1);
}
else
{
hylnext.NavigateUrl = "";
hylend.NavigateUrl = "";
hylend.Visible = false;
hylnext.Visible = false;
} }
}
}
set QUOTED_IDENTIFIER ON
GO
ALTER proc [dbo].[getdataset]
@TableList Varchar(200)='*',
@TableName Varchar(30),
@SelectWhere Varchar(500)='',
@SelectOrderId Varchar(20),
@SelectOrder Varchar(200)='',
@intPageNo int=1,
@intPageSize int=10 ,
@RecordCount int OUTPUT
as
declare @TmpSelect NVarchar(600)
declare @Tmp NVarchar(600) set nocount on set @TmpSelect = 'select @RecordCount = count(*) from '+@TableName+' where '+@SelectWhere execute sp_executesql
@TmpSelect,
N'@RecordCount int OUTPUT' ,
@RecordCount OUTPUT if (@RecordCount = 0)
return 0
if (@intPageNo - 1) * @intPageSize > @RecordCount
return (-1)
set nocount off
if @SelectWhere != ''
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' where '+@SelectWhere +' '+@SelectOrder+') and '+@SelectWhere +' '+@SelectOrder
end
else
begin
set @TmpSelect = 'select top '+str(@intPageSize)+' '+@TableList+' from '+@TableName+' where '+@SelectOrderId+' not in(select top '+str((@intPageNo-1)*@intPageSize)+' '+@SelectOrderId+' from '+@TableName+' where '+@SelectOrder+') '+@SelectOrder
end
execute sp_executesql @TmpSelect
return(@@rowcount)