zoukankan      html  css  js  c++  java
  • 我积累的数据库操作类(ASP.NET)

    http://guanvee.cnblogs.com/archive/2006/06/16/427510.html
    http://guanvee.cnblogs.com/archive/2006/06/16/427510.html

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

     

    using System;
    using System.Web;
    using System.Data.SqlClient;
    using System.Data;
    using System.Configuration;

    namespace op_db
    {
    /// <summary>
    /// 专门用来处理与数据库的操作
    /// </summary>

        public class db_class
        
    {
            
    #region 成员
            
    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;}
            }

            
    #endregion



            
    #region 函数

            
    /// <summary>
            
    /// 构造函数
            
    /// </summary>

            public db_class()
            
    {
                
    //
                
    // TODO: 在此处添加构造函数逻辑
                
    //
                conn.ConnectionString=System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
            }

            
    /// <summary>
            
    /// 析构函数
            
    /// </summary>

            ~ db_class()
            
    {
                
    if (conn.State==ConnectionState.Open)
                conn.Close();
            }


            
    /// <summary>
            
    /// 打开数据库连接
            
    /// </summary>

            public void db_open()
            
    {
                
    try
                
    {
                    
    if (conn.State==ConnectionState.Closed)
                    
    {
                        conn.ConnectionString
    =System.Configuration.ConfigurationSettings.AppSettings["constr"].ToString();
                        conn.Open();
                    }

                }

                
    catch(Exception ex)
                
    {
                    
    //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();
                }


            }


            
    /// <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(Exception ex)
                
    {
                    
    this.db_close();
                    System.Web.HttpContext.Current.Response.Write(ex.Message);
                    System.Web.HttpContext.Current.Response.End();
                    
    return false;
                }


            }


            
    /// <summary>
            
    /// 执行返回datareader的数据库查询
            
    /// </summary>
            
    /// <returns>datareader对象</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>
            
    /// 将sql语句返回为dataview
            
    /// </summary>
            
    /// <returns>dataview对象</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();
                }

            }


            
    /// <summary>
            
    /// 将只返回一条记录的sql语句执行并且返回结果
            
    /// </summary>
            
    /// <param name="def">如果没有记录的话的默认值</param>
            
    /// <returns>返回结果为string</returns>

            
            
    public string sql2str(string def)
            
    {
                
    string rr="";
                SqlCommand cmd
    =new SqlCommand(_sql,conn);
                System.Data.SqlClient.SqlDataReader dr;
                
    this.db_open();
                
    try
                
    {
                    dr
    =cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    
    if(dr.HasRows)
                    
    {
                        dr.Read();
                        rr
    =dr[0].ToString();
                        dr.Close();
                        
    this.db_close();
                    }

                }

                
    catch
                
    {
                    rr
    ="";
                }

                
    finally{this.db_close();}
                
    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;
                }


            }

            
    #endregion

        }

    }


  • 相关阅读:
    从程序员到项目经理(5):程序员加油站 不是人人都懂的学习要点
    从程序员到项目经理(14):项目经理必须懂一点“章法”
    从程序员到项目经理(17):你不是一个人在战斗思维一换天地宽
    从程序员到项目经理(19):想改变任何人都是徒劳的
    tp5时间戳转换日期格式
    win10让人愤怒的磁盘占用100%问题
    uniapp将时间日期格式化的组件unidateformat的用法
    Asp.Net大型项目实践系列第二季(一)哥欲善其事,必先利其器...
    Asp.Net大型项目实践(9)ExtJs实现系统框架页(非iframe,附源码,在线demo)
    Asp.Net大型项目实践(10)基于MVC Action粒度的权限管理(在线demo,全部源码)
  • 原文地址:https://www.cnblogs.com/smallfa/p/889759.html
Copyright © 2011-2022 走看看