sqlserver 存储过程分页代码
更新时间:2010年06月09日 18:20:59 作者:
sqlserver 存储过程分页代码并说明了用vc#调用上面的存储过程
复制代码 代码如下:
private void fyDB()
{
DataTable dt = new DataTable();
string con = System.Configuration.ConfigurationSettings.AppSettings["connectionstring"];
SqlConnection conn = new SqlConnection(con);
SqlCommand comm = new SqlCommand("P_viewPage_A", conn); //建立SqlCommand对象
comm.CommandType = CommandType.StoredProcedure; //设置SqlCommand对象执行类型为存储过程
comm.Parameters.Add("@TableName", SqlDbType.VarChar ,200); //向Parameters参数列表添加参数
comm.Parameters.Add("@FieldList", SqlDbType.VarChar, 2000);
comm.Parameters.Add("@PrimaryKey", SqlDbType.VarChar, 100);
comm.Parameters.Add("@where", SqlDbType.VarChar, 2000);
comm.Parameters.Add("@Order", SqlDbType.VarChar, 1000);
comm.Parameters.Add("@SortType", SqlDbType.Int);
comm.Parameters.Add("@RecorderCount", SqlDbType.Int);
comm.Parameters.Add("@PageSize", SqlDbType.Int);
comm.Parameters.Add("@PageIndex", SqlDbType.Int);
comm.Parameters.Add("@TotalCount", SqlDbType.Int);
comm.Parameters.Add("@TotalPageCount", SqlDbType.Int);
comm.Parameters["@TotalCount"].Direction = ParameterDirection.Output; //设置参数的输出类型
comm.Parameters["@TotalPageCount"].Direction = ParameterDirection.Output; //设置参数的输出类型
comm.Parameters["@TableName"].Value = "type1";//表名
comm.Parameters["@FieldList"].Value = "*";//显示列名,如果是全部字段则为*
comm.Parameters["@PrimaryKey"].Value = "id";//单一主键或唯一值键
comm.Parameters["@where"].Value = "";//查询条件 不含'where'字符,如id>10 and len(userid)>9
comm.Parameters["@Order"].Value = "id asc";//排序 不含'order by'字符,如id asc,userid desc,必须指定asc或desc
comm.Parameters["@SortType"].Value = 1;//排序规则 1:正序asc 2:倒序desc 3:多列排序方法
comm.Parameters["@RecorderCount"].Value = 0;//记录总数 0:会返回总记录
comm.Parameters["@PageSize"].Value = 2;//每页输出的记录数
int id1;
if (Request.QueryString["id"] == null)
id1 = 1;
else
id1 = Convert.ToInt32(Request.QueryString["id"]);
comm.Parameters["@PageIndex"].Value = id1;//当前页数
conn.Open();
// SqlDataReader asr = comm.ExecuteReader();
//int dtr = (int)comm.ExecuteScalar();
SqlDataReader sdr = comm.ExecuteReader();
int i=0;
//while (sdr.Read())
//{
// dt.Rows[i][0] = sdr.GetValue(0);
// dt.Rows[i][1] = sdr.GetValue(1);
//}
dt = ConvertDataReaderToDataTable(sdr);
GridView1.DataSource = dt;
GridView1.DataBind();
// dt.Rows[0][0];
sdr.Close();
// Response.Write(dtr);
Response.Write(comm.Parameters["@TotalCount"].Value + "<br>");
Response.Write(comm.Parameters["@TotalPageCount"].Value + "<br>");
// if((Int32)comm.Parameters["RETURN_VALUE"].Value==0)
conn.Close();
//string conStr = System.Configuration.ConfigurationSettings.AppSettings["connectionstring"];
//SqlConnection connection = new SqlConnection(conStr);
//DataSet dataSet = new DataSet();
//connection.Open();
//SqlDataAdapter sqlDA = new SqlDataAdapter();
//sqlDA.SelectCommand = BuildQueryCommand(connection, "P_viewPage_A", parameters);
//sqlDA.Fill(dataSet, tableName);
//connection.Close();
//return dataSet;
}
#region 将DataReader 转为 DataTable
/// <summary>
/// 将DataReader 转为 DataTable
/// </summary>
/// <param name="DataReader">DataReader</param>
public static DataTable ConvertDataReaderToDataTable(SqlDataReader reader)
{
try
{
DataTable objDataTable = new DataTable();
int intFieldCount = reader.FieldCount;
for (int intCounter = 0; intCounter < intFieldCount; ++intCounter)
{
objDataTable.Columns.Add(reader.GetName(intCounter), reader.GetFieldType(intCounter));
}
objDataTable.BeginLoadData();
object[] objValues = new object[intFieldCount];
while (reader.Read())
{
reader.GetValues(objValues);
objDataTable.LoadDataRow(objValues, true);
}
reader.Close();
objDataTable.EndLoadData();
return objDataTable;
}
catch (Exception ex)
{
throw new Exception("转换DataReader为DataTable出错!", ex);
}
}
#endregion
最新评论