当前位置:编程学习 > C#/ASP.NET >>

求asp.net的通用类(数据操作,分页操作,数据库操作.....)!!!

 我自己的弄的太简单了,希望有好用的学习学习!
   邮箱1103208228@qq.com !!!谢谢大侠们
答案:/// <summary> 
/// 执行存储过程(返回记录集)
/// </summary>
/// <param name="procName">过程名</param>
/// <param name="hashtable">传入的参数表</param>
/// <returns>返回记录集</returns>
public DataSet ExecProcedure(string procName, System.Collections.Hashtable hashtable)
{
System.Data.DataSet ds = new DataSet();
com.CommandText = procName;
com.CommandType = CommandType.StoredProcedure;

System.Collections.IDictionaryEnumerator ide = hashtable.GetEnumerator();

while (ide.MoveNext())
{
System.Data.SqlClient.SqlParameter p = new System.Data.SqlClient.SqlParameter(ide.Key.ToString(), ide.Value);
com.Parameters.Add(p);
}
try
{
System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(com);
da.Fill(ds);
da.Dispose();
}
catch (Exception ee)
{
throw new Exception(ee.Message.ToString());
}
finally
{
com.Cancel();
}
return ds;
}
#endregion

#region 数据操作
/// <summary>
/// 统计某表记录总数
/// </summary>
/// <param name="KeyField">主键/索引键</param>
/// <param name="TableName">数据库.用户名.表名</param>
/// <param name="Condition">查询条件</param>
/// <returns>返回记录总数</returns>
public int GetRecordCount(string keyField, string tableName, string condition)
{
int RecordCount = 0;
string sql = "select count(" + keyField + ") as count from " + tableName + " " + condition;
System.Data.DataSet ds = GetDataSet(sql);
if (ds.Tables[0].Rows.Count > 0)
{
RecordCount =Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}
ds.Clear();
ds.Dispose();
return RecordCount;
}
/// <summary>
/// 统计某表记录总数
/// </summary>
/// <param name="Field">可重复的字段</param>
/// <param name="tableName">数据库.用户名.表名</param>
/// <param name="condition">查询条件</param>
/// <param name="flag">字段是否主键</param>
/// <returns>返回记录总数</returns>
public int GetRecordCount(string Field, string tableName, string condition, bool flag)
{
int RecordCount = 0;
if (flag)
{
RecordCount = GetRecordCount(Field, tableName, condition);
}
else
{
string sql = "select count(distinct(" + Field + ")) as count from " + tableName + " " + condition;
System.Data.DataSet ds = GetDataSet(sql);
if (ds.Tables[0].Rows.Count > 0)
{
RecordCount = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}
ds.Clear();
ds.Dispose();
}
return RecordCount;
}
/// <summary>
/// 统计某表分页总数
/// </summary>
/// <param name="keyField">主键/索引键</param>
/// <param name="tableName">表名</param>
/// <param name="condition">查询条件</param>
/// <param name="pageSize">页宽</param>
/// <param name="RecordCount">记录总数</param>
/// <returns>返回分页总数</returns>
public int GetPageCount(string keyField, string tableName, string condition, int pageSize, int RecordCount)
{
int PageCount = 0;
PageCount = (RecordCount % pageSize) > 0 ? (RecordCount / pageSize) + 1 : RecordCount / pageSize;
if (PageCount < 1) PageCount = 1;
return PageCount;
}
/// <summary>
/// 统计某表分页总数
/// </summary>
/// <param name="keyField">主键/索引键</param>
/// <param name="tableName">表名</param>
/// <param name="condition">查询条件</param>
/// <param name="pageSize">页宽</param>
/// <returns>返回页面总数</returns>
public int GetPageCount(string keyField, string tableName, string condition, int pageSize, ref int RecordCount)
{
RecordCount = GetRecordCount(keyField, tableName, condition);
return GetPageCount(keyField, tableName, condition, pageSize, RecordCount);
}
/// <summary>
/// 统计某表分页总数
/// </summary>
/// <param name="Field">可重复的字段</param>
/// <param name="tableName">表名</param>
/// <param name="condition">查询条件</param>
/// <param name="pageSize">页宽</param>
/// <param name="flag">是否主键</param>
/// <returns>返回页页总数</returns>
public int GetPageCount(string Field, string tableName, string condition, ref int RecordCount, int pageSize, bool flag)
{
RecordCount = GetRecordCount(Field, tableName, condition, flag);
return GetPageCount(Field, tableName, condition, pageSize, ref RecordCount);
}
#endregion

#region 分页函数
/// <summary>
/// 构造分页查询SQL语句
/// </summary>
/// <param name="KeyField">主键</param>
/// <param name="FieldStr">所有需要查询的字段(field1,field2...)</param>
/// <param name="TableName">库名.拥有者.表名</param>
/// <param name="Condition">查询条件1(where ...)</param>
/// <param name="Condition2">查询条件2(order by ...)</param>
/// <param name="CurrentPage">当前页号</param>
/// <param name="PageSize">页宽</param>
/// <returns>SQL语句</returns>
public static string JoinPageSQL(string KeyField, string FieldStr, string TableName, string Condition, string Condition2, int CurrentPage, int PageSize)
{
string sql = null;
if (CurrentPage == 1)
{
sql = "select top " + CurrentPage * PageSize + " " + FieldStr + " from " + TableName + " " + Condition + " " + Condition2 + " ";
}
else
{
sql = "select * from (";
sql += "select top " + CurrentPage * PageSize + " " + FieldStr + " from " + TableName + " " + Condition + " " + Condition2 + ") a ";
sql += "where " + KeyField + " not in (";
sql += "select top " + (CurrentPage - 1) * PageSize + " " + KeyField + " from " + TableName + " " + Condition + " " + Condition2 + ")";
}
return sql;
}
/// <summary>
/// 构造分页查询SQL语句
/// </summary>
/// <param name="Field">字段名(非主键)</param>
/// <param name="TableName">库名.拥有者.表名</param>
/// <param name="Condition">查询条件1(where ...)</param>
/// <param name="Condition2">查询条件2(order by ...)</param>
/// <param name="CurrentPage">当前页号</param>
/// <param name="PageSize">页宽</param>
/// <returns>SQL语句</returns>
public static string JoinPageSQL(string Field, string TableName, string Condition, string Condition2, int CurrentPage, int PageSize)
{
string sql = null;
if (CurrentPage == 1)
{
sql = "select top " + CurrentPage * PageSize + " " + Field + " from " + TableName + " " + Condition + " " + Condition2 + " group by " + Field;
}
else
{
sql = "select * from (";
sql += "select top " + CurrentPage * PageSize + " " + Field + " from " + TableName + " " + Condition + " " + Condition2 + " group by " + Field + " ) a ";
sql += "where " + Field + " not in (";
sql += "select top " + (CurrentPage - 1) * PageSize + " " + Field + " from " + TableName + " " + Condition + " " + Condition2 + " group by " + Field + ")";
}
return sql;
}
/// <summary>
/// 页面分页显示功能
/// </summary>
/// <param name="Parameters">参数串(a=1&b=2...)</param>
/// <param name="RecordCount">记录总数</param>
/// <param name="PageSize">页宽</param>
/// <param name="CurrentPage">当前页号</param>
/// <param name="ShowJump">是否显示跳转输入框及按钮</param>
/// <param name="Style">样式(1

上一个:关于.net的学习问题
下一个:我买了本学习asp.net的书,里面配的光盘有案例,当我用visual studio 2005运行时,总是报错。怎么解决?

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,