SqlServer中Output参数在asp.net中怎么接收?
发布网友
发布时间:2022-04-13 06:57
我来回答
共3个回答
热心网友
时间:2022-04-13 08:26
示例:
存储过程为
CREATE Procere 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 procere = "CustomerAdd";
// Create ADO.NET objects.
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(procere, con);
// Configure command and add input parameters.
cmd.CommandType = CommandType.StoredProcere;
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);
}
}
热心网友
时间:2022-04-13 09:44
//-------------------------------------------------------------------------------
//测试获得存储过程传出的参数
//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.StoredProcere;
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>测试输出成功!");