【发布时间】:2014-02-19 04:39:23
【问题描述】:
谁能告诉我这个函数发生了什么?
在下面的代码 sn-p 中,user.Id = 0、id.Value = 0 和 id.SqlDbType = Int.. 符合预期,因为 user.Id 是一个 int 字段。
但是,error.Value = null 和 error.SqlDbType = BigInt。是什么赋予了?如果我使用非零,它会检测到一个 int 和正确的值。
注意:声明参数方向前后的Value属性是一样的。
public static long InsertUpdate(User user) {
SqlParameter id = new SqlParameter("@id", user.Id);
id.Direction = ParameterDirection.InputOutput;
cmd.Parameters.Add(id);
SqlParameter error = new SqlParameter("@error_code", 0);
error.Direction = ParameterDirection.Output;
cmd.Parameters.Add(error);
.... other stuff
}
同样,如果存储过程中的@SET @error_Code = 0,则在过程运行后error.Value = NULL 和error.SqlDbType = NVarChar。如果我将它设置为一个整数,我会得到一个 Int 类型。
更新: 指定 SqlDbType.Int 后,参数现在在命令前后都有正确的 SqlDbType ......但是,当我实际上将其设置为 0 时,存储过程仍在设置 @error_code = null。
更新: 当 sproc 执行 SELECT 语句时,@error_code 参数始终返回为 null,无论它何时被设置...这仅在有 select 语句时发生...
这里是重现的过程:
ALTER PROCEDURE [dbo].[usp_user_insert_v1]
@username VARCHAR(255),
@password VARCHAR(255),
@gender CHAR(1),
@birthday DATETIME,
@error_code INT OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @default_dt DATETIME
EXEC @default_dt = uf_get_default_date
DECLARE @dt DATETIME = GETUTCDATE()
INSERT INTO users(username, password, gender, birthday, create_dt, last_login_dt, update_dt, deleted)
VALUES(@username, @password, @gender, @birthday, @dt, @default_dt, @default_dt, 0)
SELECT * FROM users WHERE id = SCOPE_IDENTITY()
SET @error_code = 3
RETURN
END
解决方案?
在 ASP 论坛上找到此链接...显然,在您从 SqlDataReader 读取所有结果之前,您无法读取输出参数...对我来说非常不幸,因为我决定是否要阅读结果基于输出参数...
【问题讨论】:
标签: sql .net stored-procedures sqlparameter