C#調用預存程序的通用類

來源:互聯網
上載者:User
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
using System.Data;

// 摘要:資料訪問助手。
// 作者:ZhiQiao
// 日期:2008/07/02

namespace ZhiQiao.DataAccessHelper
{
    // 預存程序調用助手。
    public class StoreProcedure
    {
        // 預存程序名稱。
        private string _name;
        // 資料庫連接字串。
        private string _conStr;

        // 建構函式
         // sprocName: 預存程序名稱;
         // conStr: 資料庫連接字串。
        public StoreProcedure(string sprocName, string conStr) {
            _conStr = conStr;
            _name = sprocName;
        }
        
        //  執行預存程序,不傳回值。
         //  paraValues: 參數值列表。
         //  return: void
        public void ExecuteNoQuery(params object[] paraValues) {
            using (SqlConnection con = new SqlConnection(_conStr)) {
                SqlCommand comm = new SqlCommand(_name, con);
                comm.CommandType = CommandType.StoredProcedure;

                AddInParaValues(comm, paraValues);

                con.Open();
                comm.ExecuteNonQuery();
                con.Close();
            }
        }
        
        // 執行預存程序返回一個表。
         // paraValues: 參數值列表。
         // return: DataTable
        public DataTable ExecuteDataTable(params object[] paraValues) {
            SqlCommand comm = new SqlCommand(_name, new SqlConnection(_conStr));
            comm.CommandType = CommandType.StoredProcedure;
            AddInParaValues(comm, paraValues);

            SqlDataAdapter sda = new SqlDataAdapter(comm);
            DataTable dt = new DataTable();
            sda.Fill(dt);

            return dt;
        }
       
        // 執行預存程序,返回SqlDataReader對象,
         // 在SqlDataReader對象關閉的同時,資料庫連接自動關閉。
         // paraValues: 要傳遞給給預存程序的參數值類表。
         // return: SqlDataReader
        public SqlDataReader ExecuteDataReader(params object[] paraValues) {
            SqlConnection con = new SqlConnection(_conStr);
            SqlCommand comm = new SqlCommand(_name, con);
            comm.CommandType = CommandType.StoredProcedure;
            AddInParaValues(comm, paraValues);
            con.Open();
            return comm.ExecuteReader(CommandBehavior.CloseConnection);
        }

        // 擷取預存程序的參數列表。
        private ArrayList GetParas() {
            SqlCommand comm = new SqlCommand("dbo.sp_sproc_columns_90", 
                       new SqlConnection(_conStr));
            comm.CommandType = CommandType.StoredProcedure;
            comm.Parameters.AddWithValue("@procedure_name", (object)_name);
            SqlDataAdapter sda = new SqlDataAdapter(comm);

            DataTable dt = new DataTable();
            sda.Fill(dt);

            ArrayList al = new ArrayList();
            for (int i = 0; i < dt.Rows.Count; i++) {
                al.Add(dt.Rows[i][3].ToString());
            }
            return al;
        }

        // 為 SqlCommand 添加參數及賦值。
        private void AddInParaValues(SqlCommand comm, params object[] paraValues) {
            comm.Parameters.Add(new SqlParameter("@RETURN_VALUE", SqlDbType.Int));
            comm.Parameters["@RETURN_VALUE"].Direction = 
                           ParameterDirection.ReturnValue;
            if (paraValues != null) {
                ArrayList al = GetParas();
                for (int i = 0; i < paraValues.Length; i++) {
                    comm.Parameters.AddWithValue(al[i + 1].ToString(), 
                         paraValues[i]);
                }
            }
        }
    }
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.