public void InitBindData( Control c, Wuqi.Webdiyer. AspNetPager aspnet, string tbname, string tbcolumn, stringsqlwhere,string sfields)
{
aspnet.RecordCount = GetRows(tbname, sqlwhere);
if (aspnet.RecordCount > 0)
{
string contype = c.GetType().ToString();
DataSet ds = GetList(tbname, tbcolumn,sfields,aspnet.PageSize, aspnet.CurrentPageIndex, sqlwhere);
if (ds.Tables[0].Rows.Count > 0)
{
if (contype.IndexOf("GridView" ) != -1)
{
GridView gv = (GridView )c;
gv.DataSource = ds;
gv.DataBind();
}
else if (contype.IndexOf( "Repeater") != -1)
{
Repeater rep = (Repeater )c;
rep.DataSource = ds;
rep.DataBind();
}
else if (contype.IndexOf( "DataList") != -1)
{
DataList dl = (DataList )c;
dl.DataSource = ds;
dl.DataBind();
}
c.Visible = true ;
}
}
else
{
c.Visible = false ;
}
}
private int GetRows( string tablename, string strwhere)
{
string strSql = "select count(1) from " + tablename + " with(nolock) where " + strwhere + "" ;
return int .Parse( DbHelperSQL.GetSingle(strSql).ToString());
}
/// <summary>
/// 分页获取数据列表
/// </summary>
/// <param name="PageSize"></param>
/// <param name="PageIndex"></param>
/// <param name="strWhere"></param>
/// <returns></returns>
public DataSet GetList( string tablename, string keyname, string sfields, int PageSize, int PageIndex, stringstrWhere)
{
SqlParameter [] parameters = {
new SqlParameter ( "@tblName", SqlDbType .VarChar, 255),
new SqlParameter ( "@fldName", SqlDbType .VarChar, 255),
new SqlParameter ( "@sFields", SqlDbType .VarChar,1000),
new SqlParameter ( "@PageSize", SqlDbType .Int),
new SqlParameter ( "@PageIndex", SqlDbType .Int),
new SqlParameter ( "@IsReCount", SqlDbType .Bit),
new SqlParameter ( "@OrderType", SqlDbType .Bit),
new SqlParameter ( "@strWhere", SqlDbType .VarChar,1000),
};
parameters[0].Value = tablename;
parameters[1].Value = keyname;
parameters[2].Value = sfields;
parameters[3].Value = PageSize;
parameters[4].Value = PageIndex;
parameters[5].Value = 0;
parameters[6].Value = 1;
parameters[7].Value = strWhere;
return DbHelperSQL .RunProcedure( "Up_GetRecordByPage", parameters, "ds" );
}
这是在DbHelperSQL 中的方法:
/// <summary>
/// 执行存储过程返回dataset
/// </summary>
/// <param name="storedProcName"> 存储过程名 </param>
/// <param name="parameters"> 存储过程参数 </param>
/// <param name="tableName"> DataSet结果中的表名 </param>
/// <returns> DataSet</returns>
public static DataSet RunProcedure( string storedProcName, IDataParameter [] parameters, string tableName)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
DataSet dataSet = new DataSet();
connection.Open();
SqlDataAdapter sqlDA = new SqlDataAdapter();
sqlDA.SelectCommand = BuildQueryCommand(connection, storedProcName, parameters);
sqlDA.Fill(dataSet, tableName);
connection.Close();
return dataSet;
}
}
/// <summary>
/// 执行一条计算查询结果语句,返回查询结果(object)。
/// </summary>
/// <param name="SQLString"> 计算查询结果语句 </param>
/// <returns> 查询结果(object) </returns>
public static object GetSingle( string SQLString)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand(SQLString, connection))
{
try
{
connection.Open();
object obj = cmd.ExecuteScalar();
if ((Object .Equals(obj, null)) || ( Object .Equals(obj, System.DBNull .Value)))
{
return null ;
}
else
{
return obj;
}
}
catch (System.Data.SqlClient.SqlException e)
{
connection.Close();
connection.Dispose();
throw new Exception(e.Message);
}
}
}
}