拿它做了两个项目了,感觉还是很实用,拿出来大家一起学习,都是做项目的时候想到了就写上,肯定有很多地方需要改进,大家多指教。
using
System;
using
System.Web;
using
System.Data.SqlClient;
using
System.Data;
using
System.Configuration;
![](https://i-blog.csdnimg.cn/blog_migrate/1bd0399593c97bff900c2dd52c66a441.gif)
namespace
op_db
![](https://i-blog.csdnimg.cn/blog_migrate/548a8571eb85c68861366eceb9c9c6c1.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 专门用来处理与数据库的操作
/// </summary>
public class db_class
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/d6b3c7fedac732ddac75fefbbb9f8edb.gif)
成员#region 成员
private SqlConnection conn=new SqlConnection();
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 私有成员
/// </summary>
private string _sql;
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 属性:数据库查询语句
/// </summary>
private string _er;
public string er
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
get
{return _er;}
}
public string sql
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
get
{return _sql;}
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
set
{_sql=value;}
}
#endregion
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/d6b3c7fedac732ddac75fefbbb9f8edb.gif)
函数#region 函数
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 构造函数
/// </summary>
public db_class()
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
//
// TODO: 在此处添加构造函数逻辑
//
conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
}
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 析构函数
/// </summary>
~ db_class()
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
if (conn.State==ConnectionState.Open)
conn.Close();
}
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 打开数据库连接
/// </summary>
public void db_open()
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
try
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
if (conn.State==ConnectionState.Closed)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
conn.Open();
}
}
catch(Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
//System.Web.HttpContext.Current.Response.Redirect(System.Configuration.ConfigurationSettings.AppSettings["conn_admin"].ToString());
System.Web.HttpContext.Current.Response.Write(ex.Message);
System.Web.HttpContext.Current.Response.End();
}
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 关闭数据库连接
/// </summary>
public void db_close()
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
if (conn.State==ConnectionState.Open)
{
conn.Close();
conn.Dispose();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 执行非返回型查询语句
/// </summary>
/// <returns>是否执行成功</returns>
public bool executesql()
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
SqlCommand cmd=new SqlCommand(_sql,conn);
this.db_open();
try
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
cmd.ExecuteNonQuery();
this.db_close();
return true;
}
catch(Exception ex)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
this.db_close();
System.Web.HttpContext.Current.Response.Write(ex.Message);
System.Web.HttpContext.Current.Response.End();
return false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
}
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 执行返回datareader的数据库查询
/// </summary>
/// <returns>datareader对象</returns>
public SqlDataReader executereader()
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
SqlCommand cmd=new SqlCommand(_sql,conn);
this.db_open();
SqlDataReader dr;
try
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);//参数表示关闭datagrid对象就会关闭connection对象
if (dr.HasRows==true)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
return dr;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
_er="数据为空";
return null;
}
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
_er="语句错误";
return null;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 将sql语句返回为dataview
/// </summary>
/// <returns>dataview对象</returns>
public DataView executedv()
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
System.Data.SqlClient.SqlDataAdapter da=new SqlDataAdapter(_sql,conn);
DataSet ds=new DataSet();
this.db_open();
try
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
da.Fill(ds,"tb");
DataView dv=new DataView(ds.Tables["tb"]);
if (dv.Count>0)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
this.db_close();
return dv;
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
this.db_close();
_er="数据出错";
return null;
}
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
_er="数据出错";
return null;
}
finally
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
this.db_close();
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 将只返回一条记录的sql语句执行并且返回结果
/// </summary>
/// <param name="def">如果没有记录的话的默认值</param>
/// <returns>返回结果为string</returns>
public string sql2str(string def)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
string rr="";
SqlCommand cmd=new SqlCommand(_sql,conn);
System.Data.SqlClient.SqlDataReader dr;
this.db_open();
try
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.HasRows)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
dr.Read();
rr=dr[0].ToString();
dr.Close();
this.db_close();
}
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
rr="";
}
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
finally
{this.db_close();}
if(rr=="")
rr=def;
return rr;
}
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
/**//// <summary>
/// 执行返回BOOL的存储过程
/// </summary>
/// <returns>是否执行成功</returns>
public bool exepro()
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
this.db_open();
SqlCommand cmd=new SqlCommand(_sql,conn);
cmd.CommandType=CommandType.StoredProcedure;
try
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
cmd.ExecuteNonQuery();
this.db_close();
return true;
}
catch
![](https://i-blog.csdnimg.cn/blog_migrate/e15ff881d60e00168aecc00c9fd570e9.gif)
{
this.db_close();
return false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/b63304648824f73dde4716ef3707fd51.gif)
}
#endregion
}
}
http://guanvee.cnblogs.com/archive/2006/06/16/427510.html