使用C#代碼獲取存儲(chǔ)過(guò)程返回值
更新時(shí)間:2015年11月18日 11:59:22 作者:柔城
這篇文章主要介紹了使用C#代碼獲取存儲(chǔ)過(guò)程返回值,需要的朋友可以參考下
廢話不多說(shuō),直接給大家貼C#代碼了。
/// <summary> /// 執(zhí)行存儲(chǔ)過(guò)程,返回" 返回值" /// </summary> /// <param name="storedProcName">存儲(chǔ)過(guò)程名</param> /// <param name="parameters">存儲(chǔ)過(guò)程參數(shù)</param> /// <returns>執(zhí)行存儲(chǔ)過(guò)程的返回值</returns> public static int RunProcedureWithReturn(string storedProcName, IDataParameter[] parameters) { using (SqlConnection connection = new SqlConnection(connectionString)) { int result; connection.Open(); SqlCommand command = BuildIntCommand(connection, storedProcName, parameters); command.ExecuteNonQuery(); result = (int)command.Parameters["ReturnValue"].Value; //Connection.Close(); return result; } } /// <summary> /// 創(chuàng)建 SqlCommand 對(duì)象實(shí)例(用來(lái)返回一個(gè)整數(shù)值) /// </summary> /// <param name="storedProcName">存儲(chǔ)過(guò)程名</param> /// <param name="parameters">存儲(chǔ)過(guò)程參數(shù)</param> /// <returns>SqlCommand 對(duì)象實(shí)例</returns> private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters) { SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters); command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null)); return command; }
ps:在C#中調(diào)用存儲(chǔ)過(guò)程中的兩種返回值
//存儲(chǔ)過(guò)程 //create proc authors_count @outrus int output //as //declare @authors int //select @authors=count(*) from authors //set @outrus=@authors //return @authors System.Data.SqlClient.SqlConnection sqlcon=new System.Data.SqlClient.SqlConnection("server=(local);database=pubs;uid=sa;pwd=;"); System.Data.SqlClient.SqlCommand sqlcmd=new System.Data.SqlClient.SqlCommand("authors_count",sqlcon); sqlcmd.CommandType=System.Data.CommandType.StoredProcedure; // sqlcmd.CommandText="authors_count"; // sqlcmd.Connection=sqlcon; sqlcmd.Parameters.Add("@rus",System.Data.SqlDbType.Int); sqlcmd.Parameters.Add("@outrus",System.Data.SqlDbType.Int); sqlcmd.Parameters[0].Direction=System.Data.ParameterDirection.ReturnValue; sqlcmd.Parameters[1].Direction=System.Data.ParameterDirection.Output; sqlcon.Open(); //int res=(int)sqlcmd.ExecuteNonQuery();//此時(shí)返回的不是存儲(chǔ)過(guò)程的返回值,以上只是返回delete,update,insert所影響的行數(shù) sqlcmd.ExecuteNonQuery(); string res=sqlcmd.Parameters[0].Value.ToString();//這樣就可以得到存儲(chǔ)過(guò)程的返回值 sqlcon.Close(); this.label1.Text="存儲(chǔ)過(guò)程的返回值是:"+res.ToString();//由return 返回 this.label2.Text="存儲(chǔ)過(guò)程中返回的output值:"+sqlcmd.Parameters[1].Value.ToString();//由output返回
您可能感興趣的文章:
- C#調(diào)用SQL?Server中有參數(shù)的存儲(chǔ)過(guò)程
- SQL Server存儲(chǔ)過(guò)程在C#中調(diào)用的簡(jiǎn)單實(shí)現(xiàn)方法
- C# Ado.net實(shí)現(xiàn)讀取SQLServer數(shù)據(jù)庫(kù)存儲(chǔ)過(guò)程列表及參數(shù)信息示例
- C#執(zhí)行存儲(chǔ)過(guò)程并將結(jié)果填充到GridView的方法
- c#獲取存儲(chǔ)過(guò)程返回值示例分享
- C#獲取存儲(chǔ)過(guò)程返回值和輸出參數(shù)值的方法
- C#中如何執(zhí)行存儲(chǔ)過(guò)程方法
- C#中常用的分頁(yè)存儲(chǔ)過(guò)程小結(jié)
- C#開(kāi)發(fā)Winform程序調(diào)用存儲(chǔ)過(guò)程
相關(guān)文章
WPF實(shí)現(xiàn)動(dòng)畫效果的入門教程
WPF是一種用于創(chuàng)建Windows客戶端應(yīng)用程序的UI框架,它讓我們能夠創(chuàng)建豐富的圖形界面,包括各種各樣的動(dòng)畫效果,下面我們就來(lái)看看如何利用wpf實(shí)現(xiàn)簡(jiǎn)單的動(dòng)畫效果吧2023-09-09C#實(shí)現(xiàn)簡(jiǎn)單獲取及設(shè)置Session類
這篇文章主要介紹了C#實(shí)現(xiàn)簡(jiǎn)單獲取及設(shè)置Session類,涉及C#針對(duì)session的設(shè)置及獲取的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03Unity實(shí)現(xiàn)簡(jiǎn)單場(chǎng)景分層移動(dòng)
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)簡(jiǎn)單場(chǎng)景分層移動(dòng),分為前景、場(chǎng)景、背景等,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09C#針對(duì)xml文件轉(zhuǎn)化Dictionary的方法
這篇文章主要介紹了C#針對(duì)xml文件轉(zhuǎn)化Dictionary的方法,是C#操作XML文件的典型應(yīng)用,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-01-01