C#(.NET)数据访问连接、查询、插入等操作的封装类

秋天,是收获的季节,它因收获而变得美丽。苹果成熟了,成熟的苹果染红了大树,葡萄成熟了,成熟的葡萄在向我们微笑,桃子也成熟了,桃子像小女孩粉红的脸蛋。
usingSystem;
usingSystem.Data;
usingSystem.Data.SqlClient;
usingSystem.Configuration;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls; ///<summary>
///Public的摘要说明
///</summary>
publicclassPublicClass
{//定义一个公用成员
publicSqlConnectionconn; publicPublicClass()
{
//
//TODO:在此处添加构造函数逻辑
//
}
#region建立数据库连接
publicvoidOpenConn()
{
Stringstrconn=System.Configuration.ConfigurationManager.AppSettings["sqlconn"].ToString();
conn=newSqlConnection(strconn);
if(conn.State.ToString().ToLower()=="open")
{
//连接为打开时
}
else
{
//连接为关闭时
conn.Open();
}
}
#endregion
#region关闭并释放连接
publicvoidCloseConn()
{
if(conn.State.ToString().ToLower()=="open")
{
//连接为打开时
conn.Close();
conn.Dispose();
}
}
#endregion
#region返回DataReader,用于读取数据
publicSqlDataReaderDataRead(stringsql)
{
OpenConn();
SqlCommandcmd=newSqlCommand(sql,conn);
SqlDataReaderdr=cmd.ExecuteReader();
returndr;
}
#endregion
#region返回一个数据集
publicDataSetMySqlDataSet(stringSql,stringtableName)
{
OpenConn();
SqlDataAdapterda;
DataSetds=newDataSet();
da=newSqlDataAdapter(Sql,conn);
da.Fill(ds,tableName);
CloseConn();
returnds;
}
#endregion
//返回一个数据集
publicDataViewMySqlDataSource(stringSql)
{
OpenConn();
SqlDataAdapterda;
DataSetds=newDataSet();
da=newSqlDataAdapter(Sql,conn);
da.Fill(ds,"temp");
CloseConn();
returnds.Tables[0].DefaultView;
}
#region执行一个SQL操作:添加、删除、更新操作 //执行一个SQL操作:添加、删除、更新操作
publicvoidMySqlExcute(stringsql)
{
OpenConn();
SqlCommandcmd;
cmd=newSqlCommand(sql,conn);
cmd.ExecuteNonQuery();
cmd.Dispose();
CloseConn();
}
#endregion
#region执行一个SQL操作:添加、删除、更新操作,返回受影响的行
//执行一个SQL操作:添加、删除、更新操作,返回受影响的行
publicintMySqlExecuteNonQuery(stringsql)
{
OpenConn();
SqlCommandcmd;
cmd=newSqlCommand(sql,conn);
intflag=cmd.ExecuteNonQuery();
returnflag;
}
#endregion publicobjectMySqlExecuteScalar(stringsql)
{
OpenConn();
SqlCommandcmd;
cmd=newSqlCommand(sql,conn);
objectobj=cmd.ExecuteScalar();
cmd.Dispose();
CloseConn();
returnobj;
} ///<summary>
///返回DataTable对象
///</summary>
///<paramname="sql">sql语句</param>
///<returns></returns>
publicDataTableMySqlDataTable(stringsql)
{
OpenConn();
DataSetds=newDataSet();
SqlDataAdapterda=newSqlDataAdapter(sql,conn);
da.Fill(ds,"table");
CloseConn();
returnds.Tables["table"];
} ///<summary>
///返回一个数据集的记录数
///</summary>
///<paramname="sql">传递的sql语句必须为一个统计查询</param>
///<returns></returns>
publicintMySqlRecordCount(stringsql)
{
//注:Sql语句必须是一个统计查询
OpenConn();
SqlCommandcmd=newSqlCommand();
cmd.CommandText=sql;
cmd.Connection=conn;
SqlDataReaderdr;
dr=cmd.ExecuteReader();
intRecordCount=-1;
while(dr.Read())
{
RecordCount=int.Parse(dr[0].ToString());
}
CloseConn();
returnRecordCount;
} ///<summary>
///自定义的功能警告
///</summary>
///<paramname="str">弹出信息框内容</param>
publicvoidSetAlert(stringstr)
{
HttpContext.Current.Response.Write("<scriptlanguage='JavaScript'type='text/JavaScript'>alert('"+str+"');</script>"); }
//返回上一页
publicvoidAddErro(stringmessage)
{
HttpContext.Current.Response.Write("<script>alert('"+message+"');history.back(-1);</script>");
}
//关闭窗口
publicvoidSetCloseWindow()
{
HttpContext.Current.Response.Write("<scriptlanguage='JavaScript'type='text/JavaScript'>window.close();</script>");
} ///<summary>
///地址跳转
///</summary>
///<paramname="str">跳转地址</param>
publicvoidSetLocation(stringstr)
{
HttpContext.Current.Response.Write("<scriptlanguage='JavaScript'type='text/JavaScript'>location='"+str+"';</script>");
}
publicstringAjaxSetAlert(stringstr)
{
return"<scriptlanguage='JavaScript'type='text/JavaScript'>alert('"+str+"');</script>";
} //过滤非法字符
publicstringFilterStr(stringStr)
{
Str=Str.Trim();
Str=Str.Replace("*","");
Str=Str.Replace("=","");
Str=Str.Replace("/","");
Str=Str.Replace("$","");
Str=Str.Replace("#","");
Str=Str.Replace("@","");
Str=Str.Replace("&","");
returnStr;
} //Md5加密算法
publicstringmd5(stringstr)
{
returnSystem.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str,"md5").ToLower().Substring(0,12);
}
publicstringRndNum(intVcodeNum)
{
stringVchar="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(newChar[]{','});//将字符串生成数组
stringVNum="";
inttemp=-1; Randomrand=newRandom(); for(inti=1;i<VcodeNum+1;i++)
{
if(temp!=-1)
{
rand=newRandom(i*temp*unchecked((int)DateTime.Now.Ticks));
} intt=rand.Next(31);//数组一般从0开始读取,所以这里为31*Rnd
if(temp!=-1&&temp==t)
{
returnRndNum(VcodeNum);
}
temp=t;
VNum+=VcArray[t];
}
returnVNum;
}
}

本文C#(.NET)数据访问连接、查询、插入等操作的封装类到此结束。从风雨中寻找愉悦,在挫折中持续坚韧。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
ASP.NET中Response.BufferOutput属性的使用技巧

ASP.NET轻量级MVC框架Nancy的基本用法

ASP.NET Core中的对象池介绍

.NET集成ORM框架HiSql

asp.net中MVC的处理流程详解