SqlServer擷取預存程序傳回值的執行個體

來源:互聯網
上載者:User

1.OUPUT參數傳回值

複製代碼 代碼如下:CREATE PROCEDURE [dbo].[nb_order_insert](
@o_buyerid int ,
@o_id bigint OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
BEGIN
INSERT INTO [Order](o_buyerid )
VALUES (@o_buyerid )
SET @o_id = @@IDENTITY
END
END

預存程序中獲得方法:

複製代碼 代碼如下:DECLARE @o_buyerid int
DECLARE @o_id bigint
EXEC [nb_order_insert] @o_buyerid,@o_id output

2.RETURN過程傳回值

複製代碼 代碼如下:CREATE PROCEDURE [dbo].[nb_order_insert](
@o_buyerid int ,
@o_id bigint OUTPUT
)
AS
BEGIN
SET NOCOUNT ON;
IF(EXISTS(SELECT * FROM [Shop] WHERE [s_id] = @o_buyerid ))
BEGIN
INSERT INTO [Order](o_buyerid ) VALUES (@o_buyerid )
SET @o_id = @@IDENTITY
RETURN 1 — 插入成功返回1
END
ELSE
RETURN 0 — 插入失敗返回0 END

預存程序中的擷取方法

複製代碼 代碼如下:DECLARE @o_buyerid int
DECLARE @o_id bigint
DECLARE @result bit
EXEC @result = [nb_order_insert] @o_buyerid ,o_id output

3.SELECT 資料集傳回值

複製代碼 代碼如下:CREATE PROCEDURE [dbo].[nb_order_select](
@o_id int
)
AS
BEGIN
SET NOCOUNT ON;
SELECT o_id,o_buyerid FROM [Order]
WHERE o_id = @o_id
GO

預存程序中的擷取方法

(1)、使用暫存資料表的方法

複製代碼 代碼如下:CREATE TABLE [dbo].[Temp](
[o_id] [bigint] IDENTITY(1,1) NOT FOR REPLICATION NOT NULL,
[o_buyerid] [int] NOT NULL
)
INSERT [Temp] EXEC [nb_order_select] @o_id
– 這時 Temp 就是EXEC執行SELECT 後的結果集
SELECT * FROM [Temp]
DROP [Temp] — 刪除暫存資料表

1.擷取Return傳回值

複製代碼 代碼如下:SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ToString());
conn.Open();
SqlCommand MyCommand = new SqlCommand("nb_order", conn); //預存程序名字
MyCommand.CommandType = CommandType.StoredProcedure;   //指定類型為預存程序
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 10;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@return", SqlDbType.Int));
MyCommand.Parameters["@return"].Direction = ParameterDirection.ReturnValue;
MyCommand.ExecuteNonQuery(); //執行預存程序
Response.Write(MyCommand.Parameters["@return"].Value.ToString()); //取得return的傳回值

2.擷取Output輸出參數值

複製代碼 代碼如下:SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnStr"].ToString());
conn.Open();
SqlCommand MyCommand = new SqlCommand("nb_order", conn);
MyCommand.CommandType = CommandType.StoredProcedure;
MyCommand.Parameters.Add(new SqlParameter("@a", SqlDbType.Int));
MyCommand.Parameters["@a"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@b", SqlDbType.Int));
MyCommand.Parameters["@b"].Value = 20;
MyCommand.Parameters.Add(new SqlParameter("@c", SqlDbType.Int));
MyCommand.Parameters["@c"].Direction = ParameterDirection.Output;
MyCommand.ExecuteNonQuery();
Response.Write(MyCommand.Parameters["@c"].Value.ToString()); //指定取得預存程序的傳回值

C#接收預存程序傳回值:

複製代碼 代碼如下:public static int User_Add(User us)
{
int iRet;
SqlConnection conn = new SqlConnection(Conn_Str);
SqlCommand cmd = new SqlCommand("User_Add", conn);
cmd.CommandType = CommandType.StoredProcedure; //指定預存程序 AddWithValue可以指定名稱和值,而Add需要指定名稱,類型,再給value
cmd.Parameters.AddWithValue("@UName", us.UName);
cmd.Parameters.AddWithValue("@UPass", us.UPass);
cmd.Parameters.AddWithValue("@PassQuestion", us.PassQuestion);
cmd.Parameters.AddWithValue("@PassKey", us.PassKey);
cmd.Parameters.AddWithValue("@Email", us.Email);
cmd.Parameters.AddWithValue("@RName", us.RName);
cmd.Parameters.AddWithValue("@Area", us.Area);
cmd.Parameters.AddWithValue("@Address", us.Address);
cmd.Parameters.AddWithValue("@ZipCodes", us.ZipCodes);
cmd.Parameters.AddWithValue("@Phone", us.Phone);
cmd.Parameters.AddWithValue("@QQ", us.QQ);
cmd.Parameters.Add("@RETURN_VALUE", "").Direction = ParameterDirection.ReturnValue; //指定輸出參數是傳回值
try
{
conn.Open();
cmd.ExecuteNonQuery();                   //執行預存程序
iRet = (int)cmd.Parameters["@RETURN_VALUE"].Value; //取得return的值
}
catch (SqlException ex)
{
throw ex;
}
finally
{
conn.Close();
}
return iRet;
}

C#接收預存程序的輸出參數:

複製代碼 代碼如下:public static decimal Cart_UserAmount(int UID)
{
decimal iRet;
SqlConnection conn = new SqlConnection(Conn_Str);
SqlCommand cmd = new SqlCommand("Cart_UserAmount", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@UID", UID);
cmd.Parameters.Add("@Amount", SqlDbType.Decimal).Direction=ParameterDirection.Output; //利用Add方法為其添加名稱,類型和輸出參數
try
{
conn.Open();
cmd.ExecuteNonQuery();
iRet = (decimal)cmd.Parameters["@Amount"].Value; //取得預存程序中的輸出參數
}
catch (SqlException ex)
{
throw ex;
}
finally
{
conn.Close();
}
return iRet;
}

C#取得結果集:

複製代碼 代碼如下:string sqlw = string.Format("exec sp_UserInfo {0}", uid);
DataTable dsuser = SqlConn.GetDataSet(sqlw).Tables[0];

public static DataSet GetDataSet(string sql)
{
string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter da = new SqlDataAdapter(cmd); //直接用SqlDataAdapter將結果集取出來放入dataset中
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
cmd.Dispose();
return ds;
}

相關文章

聯繫我們

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