一個通用的資料庫類

來源:互聯網
上載者:User
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;
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.