当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.Net 数据操作类(附通用数据基类)

Asp.Net 数据操作类(附通用数据基类)

2018年04月19日  | 移动技术网IT编程  | 我要评论

海南工地塌方事故,新生儿鼻屎,鸿泰影城影讯

using system;
using system.data;
using system.configuration;
using system.web;
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
namespace ec
{
/// <summary>
/// eb通用与数据交互操作基类
/// </summary>
public class ebcommonobj:idisposable
{
private bool _alreadydispose = false;
private dboperate dbo;
private string sql = null;
private system.data.dataset ds;
#region 构造与析构函数
public ebcommonobj()
{
dbo = new dboperate();
}
~ebcommonobj()
{
dbo.dispose();
dispose();
}
protected virtual void dispose(bool isdisposing)
{
if (_alreadydispose) return;
if (isdisposing)
{
dbo.dispose();
}
_alreadydispose = true;
}
#endregion
#region idisposable 成员
public void dispose()
{
dispose(true);
gc.suppressfinalize(this);
}
#endregion
#region 通用删除数据库中的某条记录
/// <summary>
/// 通用删除数据库中的某条记录
/// </summary>
/// <param name="tbl">数据表名</param>
/// <param name="fld">字段名</param>
/// <param name="isint">是否是int型</param>
/// <param name="kev">关键词值</param>
public void commdelbyid(string tbl, string fld, bool isint, string key)
{
sql = "delete from {0} where {1}=";
if (isint)
{
sql += "{3}";
}
else
{
sql += "'{3}'";
}
dbo.executenonquery(string.format(sql, tbl, fld, isint, key));
}
#endregion
#region 通用读取数据库中的某条记录
/// <summary>
/// 通用读取数据库中的某条记录
/// </summary>
/// <param name="tbl"></param>
/// <param name="fld"></param>
/// <param name="isint"></param>
/// <param name="key"></param>
/// <returns></returns>
public dataset commreadbyid(string tbl,string fld,bool isint,string key)
{
sql = "select * from {0} where {1}=";
if (isint)
{
sql += "{3}";
}
else
{
sql += "'{3}'";
}
ds = dbo.getdataset(string.format(sql, tbl, fld, isint, key));
return ds;
}
#endregion
#region 修改数据库中的某条记录为true 或flase
/// <summary>
/// 修改数据库中的某条记录为true 或flase
/// </summary>
/// <param name="tbl">表格式</param>
/// <param name="fld">主键标识</param>
/// <param name="isint">是否整形</param>
/// <param name="key">主键</param>
/// <param name="flgfld">flase键</param>
/// <param name="flgkey">key值</param>
public void commupdatebyid(string tbl,string fld,bool isint,string key,string flgfld,int flgkey)
{
sql = "update {0} set {4}={5} where {1}=";
if (isint)
{
sql += "{3}";
}
else
{
sql += "'{3}'";
}
dbo.executenonquery(string.format(sql, tbl, fld, isint, key, flgfld, flgkey));
}
#endregion
#region 绑定dropdown 列表
/// <summary>
/// 绑定dropdown 列表
/// </summary>
/// <param name="tbl">表名</param>
/// <param name="selvalue">下拉框值</param>
/// <param name="seltext">下拉框显示内容</param>
/// <param name="strwhere">where 条件语句 不用加where 没有条件则为空</param>
/// <param name="dr">dropdownlist控件名称</param>
public void dropbind(string tbl, string selvalue, string seltext, string strwhere,system.web.ui.webcontrols.dropdownlist dr)
{
ds = getdrop(tbl, selvalue, seltext, strwhere);
dr.datasource = ds;
dr.datatextfield = seltext;
dr.datavaluefield = selvalue;
dr.databind();
ds.clear();
ds.dispose();
}
/// <summary>
/// 读取表中数据
/// </summary>
/// <param name="tbl"></param>
/// <param name="selvalue"></param>
/// <param name="seltext"></param>
/// <param name="strwhere">条件</param>
/// <returns></returns>
public dataset getdrop(string tbl,string selvalue,string seltext,string strwhere)
{
sql = "select {1},{2} from {0} where 1=1 and {3}";
ds = dbo.getdataset(string.format(sql, tbl, selvalue, seltext, strwhere));
return ds;
}
#endregion
#region 判断是否有数据
/// <summary>
/// 判断是否有数据:存在数据时返回true,否则返回flash
/// </summary>
/// <param name="tbl">数据表名</param>
/// <param name="fld">字段名</param>
/// <param name="key">关键词</param>
/// <param name="iskeyint">是否是数字类型:是:true;否:false</param>
/// <returns>true或false</returns>
public bool ishavedate(string tbl,string fld,string key,bool iskeyint)
{
bool rev = false;
if (iskeyint)
{
sql = "select * from {0} where {1}={2}";
}
else
{
sql = "select * from {0} where {1}='{2}'";
}
ds = dbo.getdataset(string.format(sql, tbl, fld, key));
if (ds.tables[0].rows.count > 0)
{
rev = true;
}
return rev;
}
#endregion
}
}

/############################################
版权声明:
文章内容为本站编辑,创作.你可以任意转载、发布、使用但请务必标明文章原始出处及本声明
http://www.opent.cn 作者:浪淘沙
############################################/
/**********************************************************************************
*
* 功能说明:数据操作基类,可以执行内联sql语句和存储过程
* 作者: 刘功勋;
* 版本:v0.1(c#2.0);时间:2006-4-28
*
* *******************************************************************************/
using system.web.security;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.webcontrols.webparts;
using system.web.ui.htmlcontrols;
namespace ec
{
/// <summary>
/// 数据库连接及操作对象类
/// </summary>
public class dbbase
{
private bool _alreadydispose = false;
private system.data.sqlclient.sqlconnection conn;
private system.data.sqlclient.sqlcommand com;
#region 构造与柝构
public dbbase()
{
try
{
conn=new system.data.sqlclient.sqlconnection(configurationmanager.appsettings["connectionstring"]);
conn.open();
com = new system.data.sqlclient.sqlcommand();
com.connection = conn;
}
catch (exception ee)
{
throw new exception("连接数据库出错");
}
}
~dbbase()
{
dispose();
}
protected virtual void dispose(bool isdisposing)
{
if (_alreadydispose) return;
if (isdisposing)
{
// todo: 此处释放受控资源
if (com != null)
{
com.cancel();
com.dispose();
}
if (conn != null)
{
try
{
conn.close();
conn.dispose();
}
catch (exception ee)
{
}
finally
{
conn = null;
}
}
}
// todo: 此处释放非受控资源。设置被处理过标记
_alreadydispose = true;
}
#endregion
#region idisposable 成员
public void dispose()
{
dispose(true);
gc.suppressfinalize(this);
}
#endregion
#region 数据基本操作
/// <summary>
/// executenonquery
/// </summary>
/// <param name="sqlstring">sql语句</param>
/// <returns>返回影响行数</returns>
public int executenonquery(string sqlstring)
{
int ret = 0;
com.commandtext = sqlstring;
com.commandtype = commandtype.text;
try
{
ret = com.executenonquery();
}
catch (exception ee)
{
throw new exception("sql:" + sqlstring + "<br />" + ee.message.tostring());
}
finally
{
com.cancel();
}
return ret;
}
/// <summary>
/// 执行插入语句返回identity
/// </summary>
/// <param name="sqlstring">sql语句</param>
/// <returns>@@identity</returns>
public int execinsert(string sqlstring)
{
int identity = 0;
//仅能执行insert into 语句
if (!sqlstring.tolower().contains("insert into"))
{
return -1;
}
sqlstring += " select @@identity";
system.data.dataset ds = new dataset();
try
{
system.data.sqlclient.sqldataadapter da = new system.data.sqlclient.sqldataadapter(sqlstring, conn);
da.fill(ds);
da.dispose();
}
catch (exception ee)
{
throw new exception("sql:" + sqlstring + "<br />" + ee.message.tostring());
}
if (ds.tables[0].rows.count > 0)
{
identity =convert.toint32(ds.tables[0].rows[0][0]);
}
ds.clear();
ds.dispose();
return identity;
}
/// <summary>
/// 执行sql语句返回记录集
/// </summary>
/// <param name="sqlstring">sql语句</param>
/// <returns>dataset</returns>
public dataset getdataset(string sqlstring)
{
system.data.dataset ds = new dataset();
try
{
system.data.sqlclient.sqldataadapter da = new system.data.sqlclient.sqldataadapter(sqlstring, conn);
da.fill(ds);
da.dispose();
}
catch (exception ee)
{
throw new exception("sql:" + sqlstring + "<br />" + ee.message.tostring());
}
return ds;
}
/// <summary>
/// 执行存储过程(返回n种参数)
/// </summary>
/// <param name="procname">过程名</param>
/// <param name="hashtable">传入的参数表</param>
/// <param name="hashtable1">传出的参数表</param>
/// <returns>返回参数表</returns>
public system.collections.hashtable execprocedure(string procname, system.collections.hashtable hashtable, system.collections.hashtable hashtable1)
{
system.collections.hashtable hashtable2 = new system.collections.hashtable();
system.collections.idictionaryenumerator ide = hashtable.getenumerator();
system.collections.idictionaryenumerator ide1 = hashtable1.getenumerator();
com.commandtype = commandtype.storedprocedure;
com.commandtext = procname;
while (ide.movenext())
{
system.data.sqlclient.sqlparameter p = new system.data.sqlclient.sqlparameter(ide.key.tostring(), ide.value);
com.parameters.add(p);
}
while (ide1.movenext())
{
system.data.sqlclient.sqlparameter p = new system.data.sqlclient.sqlparameter(ide1.key.tostring(), ide.value);
com.parameters.add(p);
}
try
{
com.executenonquery();
ide1 = hashtable1.getenumerator();
while (ide1.movenext())
{
string k = ide1.key.tostring();
hashtable2.add(k, com.parameters[k].value);
}
}
catch (exception ee)
{
throw new exception(ee.message.tostring());
}
finally
{
com.cancel();
}
return hashtable2;
}
/// <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:上页下页...,2:1234...)</param>
/// <returns></returns>
public static string paging(string parameters, int recordcount, int pagecount, int pagesize, int currentpage, bool showjump, int style)
{
string str;
if (recordcount <= pagesize) return "";
if (parameters != "") parameters += "&";
if (currentpage < 1) currentpage = 1;
if (currentpage > pagecount) currentpage = pagecount;
str = "<table align='center' width=\"100%\"><tr><td align=\"center\">";
str += "共 " + recordcount + " 条记录 页次:" + currentpage + "/" + pagecount + "页 ";
str += pagesize + "条/页 ";
if (style == 1)
{
if (currentpage == 1)
str += "<font color=\"#999999\">首页 上页</font> ";
else
{
str += "<a href='?" + parameters + "page=1' class=\"link\">首页</a> ";
str += "<a href='?" + parameters + "page=" + (currentpage - 1) + "' class=\"link\">上页</a> "; ;
}
if (currentpage == pagecount )
{
str += "<font color=\"#999999\">下页 尾页</font> ";
}
else
{
str += "<a href='?" + parameters + "page=" + (currentpage + 1) + "' class=\"link\">下页</a> ";
str += "<a href='?" + parameters + "page=" + pagecount + "' class=\"link\">尾页</a> ";
}
}
else if (style == 2)
{
int numbersize = 10;
int pagenumber = (currentpage - 1) / numbersize;
if (pagenumber * numbersize > 0)
str += "<a href='?" + parameters + "page=" + pagenumber * numbersize + "' title=上十页 >[<<]</a> ";
int i;
for (i = pagenumber * numbersize + 1; i <= (pagenumber + 1) * numbersize; i++)
{
if (i == currentpage)
str += "<strong><font color=#ff0000>[" + i + "]</font></strong> ";
else
str += "<a href='?" + parameters + "page=" + i + "'>[" + i + "]</a> ";
if (i == pagecount) break;
}
if (i < recordcount) str += "<a href='?" + parameters + "page=" + i + "' title=下十页>[>>]</a> ";
}
if (showjump)
{
str += "";
}
str += "</td></tr></table>";
return str;
}
#endregion
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网