C#(.NET)数据访问连接、查询、插入等操作的封装类
程序员文章站
2022-09-16 14:54:14
using system; using system.data; using system.data.sqlclient; using&nbs...
using system;
using system.data;
using system.data.sqlclient;
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;
/// <summary>
/// public 的摘要说明
/// </summary>
public class publicclass
{ //定义一个公用成员
public sqlconnection conn;
public publicclass()
{
//
// todo: 在此处添加构造函数逻辑
//
}
#region 建立数据库连接
public void openconn()
{
string strconn = system.configuration.configurationmanager.appsettings["sqlconn"].tostring();
conn = new sqlconnection(strconn);
if (conn.state.tostring().tolower() == "open")
{
//连接为打开时
}
else
{
//连接为关闭时
conn.open();
}
}
#endregion
#region 关闭并释放连接
public void closeconn()
{
if (conn.state.tostring().tolower() == "open")
{
//连接为打开时
conn.close();
conn.dispose();
}
}
#endregion
#region 返回datareader,用于读取数据
public sqldatareader dataread(string sql)
{
openconn();
sqlcommand cmd = new sqlcommand(sql, conn);
sqldatareader dr = cmd.executereader();
return dr;
}
#endregion
#region 返回一个数据集
public dataset mysqldataset(string sql, string tablename)
{
openconn();
sqldataadapter da;
dataset ds = new dataset();
da = new sqldataadapter(sql, conn);
da.fill(ds, tablename);
closeconn();
return ds;
}
#endregion
//返回一个数据集
public dataview mysqldatasource(string sql)
{
openconn();
sqldataadapter da;
dataset ds = new dataset();
da = new sqldataadapter(sql, conn);
da.fill(ds, "temp");
closeconn();
return ds.tables[0].defaultview;
}
#region 执行一个sql操作:添加、删除、更新操作
//执行一个sql操作:添加、删除、更新操作
public void mysqlexcute(string sql)
{
openconn();
sqlcommand cmd;
cmd = new sqlcommand(sql, conn);
cmd.executenonquery();
cmd.dispose();
closeconn();
}
#endregion
#region 执行一个sql操作:添加、删除、更新操作,返回受影响的行
//执行一个sql操作:添加、删除、更新操作,返回受影响的行
public int mysqlexecutenonquery(string sql)
{
openconn();
sqlcommand cmd;
cmd = new sqlcommand(sql, conn);
int flag = cmd.executenonquery();
return flag;
}
#endregion
public object mysqlexecutescalar(string sql)
{
openconn();
sqlcommand cmd;
cmd = new sqlcommand(sql, conn);
object obj = cmd.executescalar();
cmd.dispose();
closeconn();
return obj;
}
/// <summary>
/// 返回datatable对象
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns></returns>
public datatable mysqldatatable(string sql)
{
openconn();
dataset ds = new dataset();
sqldataadapter da = new sqldataadapter(sql, conn);
da.fill(ds, "table");
closeconn();
return ds.tables["table"];
}
/// <summary>
/// 返回一个数据集的记录数
/// </summary>
/// <param name="sql">传递的sql语句必须为一个统计查询</param>
/// <returns></returns>
public int mysqlrecordcount(string sql)
{
//注:sql 语句必须是一个统计查询
openconn();
sqlcommand cmd = new sqlcommand();
cmd.commandtext = sql;
cmd.connection = conn;
sqldatareader dr;
dr = cmd.executereader();
int recordcount = -1;
while (dr.read())
{
recordcount = int.parse(dr[0].tostring());
}
closeconn();
return recordcount;
}
/// <summary>
/// 自定义的功能警告
/// </summary>
/// <param name="str">弹出信息框内容</param>
public void setalert(string str)
{
httpcontext.current.response.write("<script language='javascript' type='text/javascript'>alert('" + str + "');</script>");
}
//返回上一页
public void adderro(string message)
{
httpcontext.current.response.write("<script>alert('" + message + "');history.back(-1);</script>");
}
//关闭窗口
public void setclosewindow()
{
httpcontext.current.response.write("<script language='javascript' type='text/javascript'>window.close();</script>");
}
/// <summary>
/// 地址跳转
/// </summary>
/// <param name="str">跳转地址</param>
public void setlocation(string str)
{
httpcontext.current.response.write("<script language='javascript' type='text/javascript'>location='" + str + "';</script>");
}
public string ajaxsetalert(string str)
{
return "<script language='javascript' type='text/javascript'>alert('" + str + "');</script>";
}
//过滤非法字符
public string filterstr(string str)
{
str = str.trim();
str = str.replace("*", "");
str = str.replace("=", "");
str = str.replace("/", "");
str = str.replace("$", "");
str = str.replace("#", "");
str = str.replace("@", "");
str = str.replace("&", "");
return str;
}
//md5加密算法
public string md5(string str)
{
return system.web.security.formsauthentication.hashpasswordforstoringinconfigfile(str, "md5").tolower().substring(0, 12);
}
public string rndnum(int vcodenum)
{
string vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,w,x";
string[] vcarray = vchar.split(new char[] { ',' }); //将字符串生成数组
string vnum = "";
int temp = -1;
random rand = new random();
for (int i = 1; i < vcodenum + 1; i++)
{
if (temp != -1)
{
rand = new random(i * temp * unchecked((int)datetime.now.ticks));
}
int t = rand.next(31); //数组一般从0开始读取,所以这里为31*rnd
if (temp != -1 && temp == t)
{
return rndnum(vcodenum);
}
temp = t;
vnum += vcarray[t];
}
return vnum;
}
}
using system.data;
using system.data.sqlclient;
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;
/// <summary>
/// public 的摘要说明
/// </summary>
public class publicclass
{ //定义一个公用成员
public sqlconnection conn;
public publicclass()
{
//
// todo: 在此处添加构造函数逻辑
//
}
#region 建立数据库连接
public void openconn()
{
string strconn = system.configuration.configurationmanager.appsettings["sqlconn"].tostring();
conn = new sqlconnection(strconn);
if (conn.state.tostring().tolower() == "open")
{
//连接为打开时
}
else
{
//连接为关闭时
conn.open();
}
}
#endregion
#region 关闭并释放连接
public void closeconn()
{
if (conn.state.tostring().tolower() == "open")
{
//连接为打开时
conn.close();
conn.dispose();
}
}
#endregion
#region 返回datareader,用于读取数据
public sqldatareader dataread(string sql)
{
openconn();
sqlcommand cmd = new sqlcommand(sql, conn);
sqldatareader dr = cmd.executereader();
return dr;
}
#endregion
#region 返回一个数据集
public dataset mysqldataset(string sql, string tablename)
{
openconn();
sqldataadapter da;
dataset ds = new dataset();
da = new sqldataadapter(sql, conn);
da.fill(ds, tablename);
closeconn();
return ds;
}
#endregion
//返回一个数据集
public dataview mysqldatasource(string sql)
{
openconn();
sqldataadapter da;
dataset ds = new dataset();
da = new sqldataadapter(sql, conn);
da.fill(ds, "temp");
closeconn();
return ds.tables[0].defaultview;
}
#region 执行一个sql操作:添加、删除、更新操作
//执行一个sql操作:添加、删除、更新操作
public void mysqlexcute(string sql)
{
openconn();
sqlcommand cmd;
cmd = new sqlcommand(sql, conn);
cmd.executenonquery();
cmd.dispose();
closeconn();
}
#endregion
#region 执行一个sql操作:添加、删除、更新操作,返回受影响的行
//执行一个sql操作:添加、删除、更新操作,返回受影响的行
public int mysqlexecutenonquery(string sql)
{
openconn();
sqlcommand cmd;
cmd = new sqlcommand(sql, conn);
int flag = cmd.executenonquery();
return flag;
}
#endregion
public object mysqlexecutescalar(string sql)
{
openconn();
sqlcommand cmd;
cmd = new sqlcommand(sql, conn);
object obj = cmd.executescalar();
cmd.dispose();
closeconn();
return obj;
}
/// <summary>
/// 返回datatable对象
/// </summary>
/// <param name="sql">sql语句</param>
/// <returns></returns>
public datatable mysqldatatable(string sql)
{
openconn();
dataset ds = new dataset();
sqldataadapter da = new sqldataadapter(sql, conn);
da.fill(ds, "table");
closeconn();
return ds.tables["table"];
}
/// <summary>
/// 返回一个数据集的记录数
/// </summary>
/// <param name="sql">传递的sql语句必须为一个统计查询</param>
/// <returns></returns>
public int mysqlrecordcount(string sql)
{
//注:sql 语句必须是一个统计查询
openconn();
sqlcommand cmd = new sqlcommand();
cmd.commandtext = sql;
cmd.connection = conn;
sqldatareader dr;
dr = cmd.executereader();
int recordcount = -1;
while (dr.read())
{
recordcount = int.parse(dr[0].tostring());
}
closeconn();
return recordcount;
}
/// <summary>
/// 自定义的功能警告
/// </summary>
/// <param name="str">弹出信息框内容</param>
public void setalert(string str)
{
httpcontext.current.response.write("<script language='javascript' type='text/javascript'>alert('" + str + "');</script>");
}
//返回上一页
public void adderro(string message)
{
httpcontext.current.response.write("<script>alert('" + message + "');history.back(-1);</script>");
}
//关闭窗口
public void setclosewindow()
{
httpcontext.current.response.write("<script language='javascript' type='text/javascript'>window.close();</script>");
}
/// <summary>
/// 地址跳转
/// </summary>
/// <param name="str">跳转地址</param>
public void setlocation(string str)
{
httpcontext.current.response.write("<script language='javascript' type='text/javascript'>location='" + str + "';</script>");
}
public string ajaxsetalert(string str)
{
return "<script language='javascript' type='text/javascript'>alert('" + str + "');</script>";
}
//过滤非法字符
public string filterstr(string str)
{
str = str.trim();
str = str.replace("*", "");
str = str.replace("=", "");
str = str.replace("/", "");
str = str.replace("$", "");
str = str.replace("#", "");
str = str.replace("@", "");
str = str.replace("&", "");
return str;
}
//md5加密算法
public string md5(string str)
{
return system.web.security.formsauthentication.hashpasswordforstoringinconfigfile(str, "md5").tolower().substring(0, 12);
}
public string rndnum(int vcodenum)
{
string vchar = "0,1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,i,j,k,l,m,n,p,q,r,s,t,u,w,x";
string[] vcarray = vchar.split(new char[] { ',' }); //将字符串生成数组
string vnum = "";
int temp = -1;
random rand = new random();
for (int i = 1; i < vcodenum + 1; i++)
{
if (temp != -1)
{
rand = new random(i * temp * unchecked((int)datetime.now.ticks));
}
int t = rand.next(31); //数组一般从0开始读取,所以这里为31*rnd
if (temp != -1 && temp == t)
{
return rndnum(vcodenum);
}
temp = t;
vnum += vcarray[t];
}
return vnum;
}
}