一个通用的数据库类

复制代码
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;


/// <summary>
/// DataBase 的摘要说明
/// </summary>
public class DataBase
{
    
private  SqlConnection conn;
    
private SqlCommand sc;
    
private SqlDataAdapter sa;

    
//返回当前的连接,事务时使用
    public SqlConnection Conn
    {
        
get { return conn; }
    }

    
public DataBase()
    {
        
//当对象一但被创建则建立连接
        this.conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings.Get("conStr"));
        
this.conn.Open();
    }

    
//关闭数据库连接
    public   void dis_connect()
    {
        
if (conn != null)
            conn.Close();
    
    }

    
//查询数据库
    public  DataSet  query(string cmd)
    {
        sc 
= new SqlCommand(cmd, conn);     //用当前对象的连接建立数据命令
        sa = new SqlDataAdapter(sc);
        DataSet myds 
= new DataSet();
        myds  
= new DataSet();
        sa.Fill(myds);   
//执行查询命令,并将查询结果保存在DataSet中.
        return myds;
    }

    
//新增记录
    public int update(string cmd) 
    {
        sc 
= new SqlCommand(cmd, conn);
        
int re;
        
try
        {
            re 
= sc.ExecuteNonQuery();            //执行其它命令(insert,update,delete)
        }
        
catch (Exception e)
        {
            Console.Write(e.Message);           
            re 
= -5;                                 //数据库操作有异常
        }
        
return re;
            
    }

    
/*调用查询类的存储过程
     * prceName: 存储过程名
     * parames[]:参数数组
     
*/
    
public DataSet call_que_proce(string prceName,params SqlParameter[] parames)
    {
        sa 
= new SqlDataAdapter();
        sc 
= new SqlCommand();
        DataSet ds 
= new DataSet();

        sc.Connection 
= conn;
        sc.CommandText 
= prceName;
        sc.CommandType 
= CommandType.StoredProcedure;
        sa.SelectCommand 
= sc;

        
foreach (SqlParameter sp in parames)
        {
            sa.SelectCommand.Parameters.Add(sp);
        }
        sa.Fill(ds);
        sc.Parameters.Clear();
        sa.Dispose();
        
return ds;
    }

    
/*调用非查询类的存储过程
     * prceName
     
*/
    
public int call_noqeury_proce(string prceName, params SqlParameter[] parames)
    {
     
        sc 
= new SqlCommand();
        
int result = 0;
        sc.Connection 
= conn;
        sc.CommandText 
= prceName;
        sc.CommandType 
= CommandType.StoredProcedure;
        sa.SelectCommand 
= sc;

        
foreach (SqlParameter sp in parames)
        {
            sa.SelectCommand.Parameters.Add(sp);
        }

        result 
= sc.ExecuteNonQuery();
        sc.Parameters.Clear();
        
return result;
    }
}
复制代码

posted on   心笑峰  阅读(421)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述

导航

< 2009年6月 >
31 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 1 2 3 4
5 6 7 8 9 10 11
点击右上角即可分享
微信分享提示