"Conversion failed when converting the nvarchar value '113332,113347' to data type int."
"Conversion failed when converting the nvarchar value '113332,113347' to data type int."
我收到以下代码的错误。我将本地 ID 传递为“113332,113347”
cn.Open();
SqlCommand cmd = new SqlCommand("SelectUser", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@in_UserId", SqlDbType.Int).Value = Convert.ToInt32(userId);
cmd.Parameters.Add("@in_StartDate", SqlDbType.DateTime).Value = fromDate;
cmd.Parameters.Add("@in_EndDate", SqlDbType.DateTime).Value = toDate;
cmd.Parameters.Add("@in_LocalIds", SqlDbType.NVarChar, 100).Value = localids.ToString();
cmd.ExecuteNonQuery();
"Conversion failed when converting the nvarchar value '113332,113347' to data type int."
在数据库中,本地id是int数据类型。
存储过程代码如下
CREATE PROCEDURE [dbo].[User_Update] @in_UserId INT
,@in_StartDate DATETIME
,@in_EndDate DATETIME
,@in_LocalIds NVARCHAR(100)
AS
BEGIN
SELECT * FROM TABLE1 WHERE LocalId in (@in_LocalIds) AND UserId = @in_UserId
END
go
在 @in_LocalIds
参数中你有一个由 ',' 连接的 int,所以拆分值(在 SQL 中它应该是某种 while 循环),将值转换为 int 和将它们插入临时 table.
然后在您的 where 子句中使用 table。
编辑:
CREATE PROCEDURE [dbo].[User_Update] @in_UserId INT
,@in_StartDate DATETIME
,@in_EndDate DATETIME
,@in_LocalIds NVARCHAR(100)
AS
BEGIN
declare @tempLocalIds nvarchar(100)
declare @tempStrId nvarchar(100)
declare @tempId int
declare @idx int
declare @Ids TABLE ( ID int )
set @tempLocalIds = @in_LocalIds
while( len(@tempLocalIds) > 0)
begin
-- find index of first ',' char
set @idx = charindex(',', @tempLocalIds)
-- get substring 0 to previously found index
set @tempStrId = substring(@tempLocalIds, 0, @idx-1)
-- convert the value
set @tempId = convert(@tempStrId as int)
-- remove the first number from string
set @tempLocalIds = substring(@tempLocalIds, @idx, len(@tempLocalIds) - @idx -1)
-- insert into the temp table
insert into @Ids(ID)
values (@tempId)
end
SELECT * FROM TABLE1 WHERE LocalId in (select ID from @Ids) AND UserId = @in_UserId
END
go
我收到以下代码的错误。我将本地 ID 传递为“113332,113347”
cn.Open();
SqlCommand cmd = new SqlCommand("SelectUser", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@in_UserId", SqlDbType.Int).Value = Convert.ToInt32(userId);
cmd.Parameters.Add("@in_StartDate", SqlDbType.DateTime).Value = fromDate;
cmd.Parameters.Add("@in_EndDate", SqlDbType.DateTime).Value = toDate;
cmd.Parameters.Add("@in_LocalIds", SqlDbType.NVarChar, 100).Value = localids.ToString();
cmd.ExecuteNonQuery();
"Conversion failed when converting the nvarchar value '113332,113347' to data type int."
在数据库中,本地id是int数据类型。
存储过程代码如下
CREATE PROCEDURE [dbo].[User_Update] @in_UserId INT
,@in_StartDate DATETIME
,@in_EndDate DATETIME
,@in_LocalIds NVARCHAR(100)
AS
BEGIN
SELECT * FROM TABLE1 WHERE LocalId in (@in_LocalIds) AND UserId = @in_UserId
END
go
在 @in_LocalIds
参数中你有一个由 ',' 连接的 int,所以拆分值(在 SQL 中它应该是某种 while 循环),将值转换为 int 和将它们插入临时 table.
然后在您的 where 子句中使用 table。
编辑:
CREATE PROCEDURE [dbo].[User_Update] @in_UserId INT
,@in_StartDate DATETIME
,@in_EndDate DATETIME
,@in_LocalIds NVARCHAR(100)
AS
BEGIN
declare @tempLocalIds nvarchar(100)
declare @tempStrId nvarchar(100)
declare @tempId int
declare @idx int
declare @Ids TABLE ( ID int )
set @tempLocalIds = @in_LocalIds
while( len(@tempLocalIds) > 0)
begin
-- find index of first ',' char
set @idx = charindex(',', @tempLocalIds)
-- get substring 0 to previously found index
set @tempStrId = substring(@tempLocalIds, 0, @idx-1)
-- convert the value
set @tempId = convert(@tempStrId as int)
-- remove the first number from string
set @tempLocalIds = substring(@tempLocalIds, @idx, len(@tempLocalIds) - @idx -1)
-- insert into the temp table
insert into @Ids(ID)
values (@tempId)
end
SELECT * FROM TABLE1 WHERE LocalId in (select ID from @Ids) AND UserId = @in_UserId
END
go