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;
}