我已经编写了存储过程来获取具有给定输入的客户ID。 手动执行时,它返回值,但不返回任何值 C#代码。
create procedure getCustomerID
@firstname CHAR(25),
@lastname CHAR(25),
@middlename as CHAR(25)=null,
@DOB datetime,
@CustomerState as CHAR(25)=null,
@CustomerNumber INTEGER,
@ID nvarchar(25) output
as
begin
....
...
set @ID='something'
end
USE [TestDB]
GO
declare @ID nvarchar(25)
EXECute [dbo].[getCustomerID]
'A', 'B','C','1963-09-06','', 12345, @ID out
print 'ID:'+@ID
GO
输出
ID:CN0075
C#代码:
try
{
using (SqlConnection conn = new SqlConnection("Connectionstring"))
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
using (var command = new SqlCommand("getCustomerID", conn)
{
CommandType = CommandType.StoredProcedure
})
{
command.Parameters.Add("@firstname", SqlDbType.Char, 25).Value = "A";
command.Parameters.Add("@lastname", SqlDbType.Char, 25).Value = "B";
command.Parameters.Add("@middlename", SqlDbType.Char, 25).Value = "C";
command.Parameters.Add("@CustomerState", SqlDbType.Char, 25).Value = "";
command.Parameters.Add("@DOB", SqlDbType.DateTime).Value = "1963-09-06";
command.Parameters.Add("@CustomerNumber", SqlDbType.Int).Value = "12345";
command.Parameters.Add("@ID", SqlDbType.NVarChar, 25).Direction =
ParameterDirection.Output;
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
string retunID = command.Parameters["@ID"].Value.ToString();
}
}
}
}
}
catch (Exception e)
{
Logger.Error(e.Message);
}
它不会引发异常。它执行存储过程并执行
不要进入While(reader.read())
循环并读取数据。
答案 0 :(得分:0)
在您的sp中使用 select 状态为 print
create procedure getCustomerID
@firstname CHAR(25),
@lastname CHAR(25),
@middlename as CHAR(25)=null,
@DOB datetime,
@CustomerState as CHAR(25)=null,
@CustomerNumber INTEGER,
@ID nvarchar(25) output
as
begin
....
...
set @ID='something'
select @ID
end
USE [TestDB]
GO
declare @ID nvarchar(25)
EXECute [dbo].[getCustomerID]
'A', 'B','C','1963-09-06','', 12345, @ID out
select 'ID:'+@ID
GO