最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#調用SQL?Server中有參數(shù)的存儲過程

 更新時間:2022年03月04日 08:51:05   作者:.NET開發(fā)菜鳥  
這篇文章介紹了C#調用SQL?Server中有參數(shù)存儲過程的方法,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

一、使用SqlParameter的方式

代碼:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Collections.ObjectModel;
using System.Reflection;

namespace ExecuteProcBySQLServer
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_LoadData_Click(object sender, EventArgs e)
        {
            // 存儲過程名稱
            string strProcName = "usp_yngr_getInfectionCard_test";

            //定義存儲過程的參數(shù)數(shù)組
            SqlParameter[] paraValues = {
                                        new SqlParameter("@BeginTime",SqlDbType.VarChar),
                                        new SqlParameter("@EndTime",SqlDbType.VarChar),
                                        new SqlParameter("@DateType",SqlDbType.Int),
                                        new SqlParameter("@PtName",SqlDbType.VarChar),
                                        new SqlParameter("@PtChartNo",SqlDbType.VarChar),
                                        new SqlParameter("@DeptCode",SqlDbType.VarChar),
                                        new SqlParameter("@CheckedStatus",SqlDbType.Int)
                                        };
            // 給存儲過程參數(shù)數(shù)組賦值
            paraValues[0].Value = "2017-06-01";
            paraValues[1].Value = "2017-07-01";
            paraValues[2].Value = 1;
            paraValues[3].Value = "";
            paraValues[4].Value = "";
            paraValues[5].Value = "";
            paraValues[6].Value = 1;

            this.dgv_Demo.DataSource = LoadData(strProcName, paraValues);

        }

        /// <summary>
        /// 通過存儲過程獲取數(shù)據(jù)
        /// </summary>
        /// <param name="strProcName">存儲過程名稱</param>
        /// <param name="paraValues">可變的參數(shù)數(shù)組 數(shù)組的個數(shù)可以為0,也可以為多個</param>
        /// <returns></returns>
        private DataTable LoadData(string strProcName, params object[] paraValues)
        {
            DataTable dt = new DataTable();
            string strConn = ConfigurationManager.ConnectionStrings["HealthHospInfection"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = strProcName;
                    // 設置CommandType的類型
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = conn;
                    conn.Open();

                    if (paraValues != null)
                    {
                        //添加參數(shù)
                        cmd.Parameters.AddRange(paraValues);
                    }

                    // 取數(shù)據(jù)
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                        adapter.Fill(dt);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("錯誤:" + ex.Message + "/r/n跟蹤:" + ex.StackTrace);
                }
                finally
                {
                    conn.Close();
                }
            }
            return dt;
        }
    }
}

二、使用SqlCommandBuilder

在上面的例子中,得到一個SqlCommand之后要一個一個地去設置參數(shù),這樣很麻煩,幸好SqlCommandBuilder有一個靜態(tài)的方法:

public static void DeriveParameters(SqlCommand command);

使用這個方法有兩個局限性:

  • 1、參數(shù)必須是SqlCommand。
  • 2、該方法只能在調用存儲過程的時候使用。

同時還要注意到:在使用的時候,數(shù)據(jù)庫連接必須是打開的。
下面的例子演示如何使用這個方法設置存儲過程的參數(shù):

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace ExecuteProcBySQLServer
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void btn_LoadData_Click(object sender, EventArgs e)
        {
            // 存儲過程名稱
            string strProcName = "usp_yngr_getInfectionCard_test";
            // 定義參數(shù)類
            object objParams = new
            {
                BeginTime = "2017-06-01",
                EndTime = "2017-07-01",
                DateType = 1,
                PtName = "",
                PtChartNo = "",
                DeptCode = "",
                CheckedStatus = 1
            };

            this.dgv_Demo.DataSource = LoadData(strProcName,objParams);
        }

        private DataTable LoadData(string strProcName,object objParams)
        {
            DataTable dtInit = new DataTable();
            string strConn = ConfigurationManager.ConnectionStrings["HealthHospInfection"].ConnectionString;
            using (SqlConnection conn = new SqlConnection(strConn))
            {
                try
                {
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = strProcName;
                    // 設置CommandType的類型
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Connection = conn;
                    conn.Open();

                    // 添加參數(shù)
                    foreach (var item in GetParameters(cmd, objParams))
                    {
                        cmd.Parameters.Add(item);
                    }

                    // 取數(shù)據(jù)
                    using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
                    {
                        adapter.Fill(dtInit);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("錯誤:" + ex.Message + "/r/n跟蹤:" + ex.StackTrace);
                }
                finally
                {
                    conn.Close();
                }
            }
            return dtInit;
        }

        private Collection<SqlParameter> GetParameters(SqlCommand command, object objParam)
        {
            Collection<SqlParameter> collection = new Collection<SqlParameter>();
            if (objParam != null)
            {
                // 使用反射獲取屬性
                PropertyInfo[] properties = objParam.GetType().GetProperties();
                SqlCommandBuilder.DeriveParameters(command);
                //int index = 0;
                foreach (SqlParameter parameter in command.Parameters)
                {
                    foreach (PropertyInfo property in properties)
                    {
                        if (("@" + property.Name.ToLower()).Equals(parameter.ParameterName.ToLower()))
                        {
                            parameter.Value = property.GetValue(objParam, null);
                            collection.Add(parameter);
                        }
                    }
                }

                // 清空所有參數(shù)對象
                command.Parameters.Clear();
            }

            return collection;
        }
    }
}

示例代碼下載地址:點此下載

到此這篇關于C#調用SQL Server中有參數(shù)存儲過程的文章就介紹到這了。希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • C#中的委托和事件詳解

    C#中的委托和事件詳解

    本文詳細講解了C#中的委托和事件,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • Unity PC版Log的具體位置介紹

    Unity PC版Log的具體位置介紹

    這篇文章主要介紹了Unity PC版Log的具體位置介紹,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-04-04
  • C#?winform跨線程操作控件的實現(xiàn)

    C#?winform跨線程操作控件的實現(xiàn)

    本文主要介紹了C#?winform跨線程操作控件的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-06-06
  • C#中匿名方法與委托的關系介紹

    C#中匿名方法與委托的關系介紹

    這篇文章介紹了C#中匿名方法與委托的關系,文中通過示例代碼介紹的非常詳細。對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-08-08
  • WCF實現(xiàn)的計算器功能實例

    WCF實現(xiàn)的計算器功能實例

    這篇文章主要介紹了WCF實現(xiàn)的計算器功能,結合具體實例形式較為詳細的分析了WCF實現(xiàn)計算器功能的具體步驟與相關操作技巧,需要的朋友可以參考下
    2017-06-06
  • C#數(shù)據(jù)結構之隊列(Quene)實例詳解

    C#數(shù)據(jù)結構之隊列(Quene)實例詳解

    這篇文章主要介紹了C#數(shù)據(jù)結構之隊列(Quene),結合實例形式較為詳細的講述了隊列的功能、原理與C#實現(xiàn)隊列的相關技巧,需要的朋友可以參考下
    2015-11-11
  • C#禁止textbox復制、粘貼、剪切及鼠標右鍵的方法

    C#禁止textbox復制、粘貼、剪切及鼠標右鍵的方法

    這篇文章主要介紹了C#禁止textbox復制、粘貼、剪切及鼠標右鍵的方法,涉及C#針對窗口消息的處理技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • SQLite之C#版 System.Data.SQLite使用方法

    SQLite之C#版 System.Data.SQLite使用方法

    這篇文章主要介紹了SQLite之C#版 System.Data.SQLite使用方法,需要的朋友可以參考下
    2020-10-10
  • C#微信公眾平臺開發(fā)之access_token的獲取存儲與更新

    C#微信公眾平臺開發(fā)之access_token的獲取存儲與更新

    這篇文章主要介紹了C#微信公眾平臺開發(fā)之access_token的獲取存儲與更新的相關資料,需要的朋友可以參考下
    2016-03-03
  • C# 利用AForge實現(xiàn)攝像頭信息采集

    C# 利用AForge實現(xiàn)攝像頭信息采集

    這篇文章主要介紹了C# 如何利用AForge實現(xiàn)攝像頭信息采集,文中示例代碼非常詳細,幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-07-07

最新評論

普陀区| 普兰县| 南宁市| 凤翔县| 丰台区| 岳普湖县| 沧州市| 宁河县| 类乌齐县| 芒康县| 京山县| 临安市| 门头沟区| 贞丰县| 淮阳县| 阿拉善盟| 奇台县| 奉新县| 长治市| 涟水县| 义马市| 沽源县| 开平市| 辽阳县| 沅江市| 探索| 九江县| 舟曲县| 即墨市| 玉龙| 永康市| 天全县| 印江| 晋江市| 旺苍县| 南涧| 郑州市| 那坡县| 东乡族自治县| 水城县| 麦盖提县|