SqlServer中Output参数在asp.net中怎么接收?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SqlServer中Output参数在asp.net中怎么接收?相关的知识,希望对你有一定的参考价值。

一个添加用户的操作,添加时检查用户名是否重复,在存储过程中有一个Output参数,在asp.net中用什么接收?

示例:
存储过程为
CREATE Procedure CustomerAdd
(
@FullName nvarchar(50),
@Email nvarchar(50),
@Password nvarchar(50),
@CustomerID int OUTPUT
)
AS

INSERT INTO Customers
(
FullName,
EMailAddress,
Password
)

VALUES
(
@FullName,
@Email,
@Password
)

SELECT
@CustomerID = @@Identity

GO

C#代码为:
using System;
using System.Data;
using System.Data.SqlClient;

public class AddCustomer

public static void Main()

string connectionString = "Data Source=.;" +
"Initial Catalog=store;Integrated Security=SSPI";
string procedure = "CustomerAdd";

// Create ADO.NET objects.
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(procedure, con);

// Configure command and add input parameters.
cmd.CommandType = CommandType.StoredProcedure;
SqlParameter param;

param = cmd.Parameters.Add("@FullName", SqlDbType.NVarChar, 50);
param.Value = "John Smith";

param = cmd.Parameters.Add("@Email", SqlDbType.NVarChar, 50);
param.Value = "john@mydomain.com";

param = cmd.Parameters.Add("@Password", SqlDbType.NVarChar, 50);
param.Value = "opensesame";

// Add the output parameter.
param = cmd.Parameters.Add("@CustomerID", SqlDbType.Int);
param.Direction = ParameterDirection.Output;//指示是返回参数

// Execute the command.
con.Open();
cmd.ExecuteNonQuery();
con.Close();
//获取返回参数的值
Console.WriteLine("New customer has ID of " + param.Value);


参考技术A //-------------------------------------------------------------------------------
//测试获得存储过程传出的参数
//SqlClient方式,测试通过
//-------------------------------------------------------------------------------
//连接数据库
SqlConnection sqlconn=new SqlConnection();
sqlconn.C;
//server=192.168.3.80;uid=sa;Password=rain;database=MyStudy";
sqlconn.Open();
SqlCommand com=new SqlCommand();
com.Connection=sqlconn;

com.CommandText="ShowCmdTest";
com.CommandType=CommandType.StoredProcedure;
SqlParameter[] p_htno=new SqlParameter[2];
p_htno[0] = new SqlParameter("@inA",SqlDbType.VarChar,80);
p_htno[1] = new SqlParameter("@outB",SqlDbType.VarChar,80);
p_htno[0].Value ="测试输入";
p_htno[1].Direction=ParameterDirection.Output;
//p_htno[1].Value = this.drop_character.Items[this.drop_character.SelectedIndex].Value;

for ( int i =0 ;i< p_htno.Length;i++)

com.Parameters.Add(p_htno[i]);

com.ExecuteNonQuery();
Response.Write(p_htno[1].Value.ToString()+"<br>测试输出成功!");

以上是关于SqlServer中Output参数在asp.net中怎么接收?的主要内容,如果未能解决你的问题,请参考以下文章

pymssql调用sqlserver存储过程带output 参数

sqlserver的function有output参数吗?

qt调用sql server存储过程并获取output参数

sqlserver怎么创建存储过程

带参数的ms sql server的扩展存储过程,怎么传递参数

如何使sqlserver存储过程返回varchar