一个数据库操作类

拿它做了两个项目了,感觉还是很实用,拿出来大家一起学习,都是做项目的时候想到了就写上,肯定有很多地方需要改进,大家多指教。

 

using System;

using System.Data.SqlClient;

using System.Data;

using System.Configuration;

namespace web.base_class

{

     /// <summary>

     /// db_class 的摘要说明。注意用到executereader后一定要调用析构函数

     /// </summary>

     public class db_class

     {

          private SqlConnection conn=new SqlConnection();

         /// <summary>

         /// 私有成员

         /// </summary>

          private string _sql;

         /// <summary>

         /// 属性:数据库查询语句

         /// </summary>

          private string _er;

         public string er

         {

              get{return _er;}

             

         }

         public string sql

         {

              get{return _sql;}

              set{_sql=value;}

         }

         /// <summary>

         /// 构造函数

         /// </summary>

         public db_class()

         {

              //

              // TODO: 在此处添加构造函数逻辑

              //

              conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();

         }

         ~ db_class()//析构函数

         {

              if (conn.State==ConnectionState.Open)

              conn.Close();

         }

 

         /// <summary>

         /// 打开数据库连接

         /// </summary>

         public void db_open()

         {

              if (conn.State==ConnectionState.Closed)

              {

                   conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();

                   conn.Open();

              }

         }

         /// <summary>

         /// 关闭数据库连接

         /// </summary>

         public void db_close()

         {

              if (conn.State==ConnectionState.Open){

                   conn.Close();

                   conn.Dispose();

              }

         }

 

         /// <summary>

         /// 执行非返回型查询语句

         /// </summary>

         /// <returns></returns>

         public bool executesql()

         {

              SqlCommand cmd=new SqlCommand(_sql,conn);

              this.db_open();

              try

              {

                   cmd.ExecuteNonQuery();

                   this.db_close();

                   return true;

              }

              catch

              {

                   this.db_close();

                   return false;

              }

         }

         /// <summary>

         /// 执行返回datareader的数据库查询

         /// </summary>

         /// <returns></returns>

         public SqlDataReader executereader()

         {

              SqlCommand cmd=new SqlCommand(_sql,conn);

              this.db_open();

              SqlDataReader dr;

              try

              {

                   dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);//参数表示关闭datagrid对象就会关闭connection对象

                   if (dr.HasRows==true)

                   {

                       return dr;

                   }

                   else

                   {

                       _er="数据为空";

                       return null;

                   }

              }

              catch

              {

                   _er="语句错误";

                   return null;

              }

         }

 

/// <summary>

///

/// </summary>

/// <returns></returns>

         public DataView executedv()

         {

              System.Data.SqlClient.SqlDataAdapter da=new SqlDataAdapter(_sql,conn);

              DataSet ds=new DataSet();

              this.db_open();

              try

              {

                   da.Fill(ds,"tb");

                   DataView dv=new DataView(ds.Tables["tb"]);

                   if (dv.Count>0)

                   {

                        this.db_close();

                       return dv;

                   }

                   else

                   {

                        this.db_close();

                       _er="数据出错";

                       return null;

                   }

              }

              catch

              {

                   _er="数据出错";

                   return null;

              }

              finally

              {

                   this.db_close();

              }

         }

         public string sql2str(string def)

         {

              string rr="";

              SqlCommand cmd=new SqlCommand(_sql,conn);

              this.db_open();

              SqlDataReader dr;

              try

              {

                   dr=cmd.ExecuteReader(CommandBehavior.CloseConnection);

                   if(dr.HasRows)

                   {

                        dr.Read();

                        rr=dr[0].ToString();

                        dr.Close();

                        this.db_close();

                   }

              }

              catch

              {

                   rr="";

              }

              if(rr=="")

                   rr=def;

              return rr;

         }

 

         /// <summary>

         /// 执行返回BOOL的存储过程

         /// </summary>

         /// <returns></returns>

         public bool exepro()

         {

              this.db_open();

              SqlCommand cmd=new SqlCommand(_sql,conn);

              cmd.CommandType=CommandType.StoredProcedure;

              try

              {

                   cmd.ExecuteNonQuery();

                   this.db_close();

                   return true;

              }

              catch

              {

                   this.db_close();

                   return false;

              }

 

         }

     }

}

文章出自

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值