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

时间:2021-05-28

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;
}
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章