将整数列表作为sql参数传递

时间:2018-09-12 13:04:52

标签: c# sql sql-server

知道此问题可能是重复的,但仍然没有答案说明此代码导致异常的原因

const string query = @"
            SELECT hi.[ID]    
                   ,hi.[UniqueName]
                   ,hi.[DisplayName]
                   ,hi.[Value]
                   ,hi.[Unit]
                   ,hi.[Status]
                   ,hi.[OriginalStatus]
                   ,hi.[Message]
                   ,hi.[IsDeleted]
                   ,hi.[IsDisabled]
                   ,h.[NetObjectID]
           FROM [dbo].[HWH_HardwareItem] hi
           INNER JOIN [dbo].[HWH_HardwareInfo] h ON hi.HardwareInfoID = h.ID
           WHERE h.[NetObjectType] = @NetObjectType AND h.NetObjectID IN (@NetObjectIDs) AND hi.[IsDisabled] = 'TRUE'";

        using (var command = SqlHelper.GetTextCommand(query))
        {
            var idParameterList = new List<string>();
            var index = 0;
            foreach (var id in netObjectIds)
            {
                var paramName = "@idParam" + index;
                command.Parameters.AddWithValue(paramName, id);
                idParameterList.Add(paramName);
                index++;
            }
            command.Parameters.AddWithValue("NetObjectType", netObjectType);
            command.Parameters.AddWithValue("NetObjectIDs", string.Join(",", idParameterList));

            using (var reader = SqlHelper.ExecuteReader(command))
            {
                while (reader.Read())
                {
                    disabledItems.Add(GetItemFromReader(reader));
                }
            }
        }

异常:'将nvarchar值'@ idParam0,@ idParam1'转换为数据类型int时转换失败

0 个答案:

没有答案