SqlParameter和c#

时间:2015-04-11 17:54:56

标签: c# sql

大家好,有人可以帮我解决这个问题。我花了将近2天时间试图弄明白这一点。

我在c#

中有一个这个参数查询
UPDATE [User] 
SET LoginName = '@LoginName', Name = '@Name',
    Surname = '@Surname', Telphone = '@Telphone', 
    Cellphone = '@Cellphone', Fax = '@Fax', Email = '@Email', 
    Password = '@Password', PasswordCreationDate = @PasswordCreationDate, 
    IsActive = @IsActive, ManagerID = @ManagerID, 
    SuperVisorID = @SuperVisorID, TeamLeaderID = @TeamLeaderID, 
    PermissionID = @PermissionID 
WHERE UserID = @UserID

我已将此值分配给@Telephone参数,但它失败了,所以我打开了SQL Server Profiler,我看到它给了我这个

USE Collector
exec sp_executesql N'UPDATE [User] SET LoginName=''@LoginName'', Name=''@Name'', Surname=''@Surname'', 
Telphone=''@Telphone'',
 Cellphone=''@Cellphone'', Fax=''@Fax'', 
Email=''@Email'', Password=''@Password'', PasswordCreationDate = @PasswordCreationDate, 
IsActive = @IsActive, ManagerID = @ManagerID, SuperVisorID = @SuperVisorID, 
TeamLeaderID = @TeamLeaderID, PermissionID = @PermissionID WHERE UserID = @UserID',
N'@UserID int,@LoginName nvarchar(6),@Name nvarchar(9),@Surname nvarchar(8),
@Telphone nvarchar(4000)
,@Cellphone nvarchar(4),@Fax nvarchar(3),@Email nvarchar(3),
@Password nvarchar(32),@PasswordCreationDate datetime,@IsActive bit,@ManagerID int,
@SuperVisorID int,@TeamLeaderID int,@PermissionID int',@UserID=29,@LoginName=N'daniel',
@Name=N'Daniel123',@Surname=N'asdfasdf',
@Telphone=DEFAULT,
@Cellphone=N'3453',@Fax=N'345',
@Email=N'adf',@Password=N'5F4DCC3B5AA765D61D8327DEB882CF99',
@PasswordCreationDate='2015-04-03 21:55:57',@IsActive=1,@ManagerID=7,@SuperVisorID=7,
@TeamLeaderID=7,@PermissionID=1

不看这个查询我看到它已经在那里放置了单词DEFAULT,甚至认为我已经专门指定''所以它会带回警告。

  

参数化查询'(@ UserID int,@ LoginName nvarchar(6),@ Name nvarchar(9),@ Surname n'需要参数'@Telphone',这是未提供的。

但我确实给它分配了'new SqlParameter("@Telphone", "''");

我错过了什么?我需要另一组眼睛请帮忙。

表格def:

USE [Collector]
GO

/****** Object:  Table [dbo].[User]    Script Date: 2015/04/11 08:19:09 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[User](
    [UserID] [int] IDENTITY(1,1) NOT NULL,
    [LoginName] [nvarchar](255) NOT NULL,
    [Name] [nvarchar](255) NOT NULL,
    [Surname] [nvarchar](255) NOT NULL,
    [Telphone] [nvarchar](50) NULL,
    [Cellphone] [nvarchar](50) NULL,
    [Fax] [nvarchar](50) NULL,
    [Email] [nvarchar](50) NULL,
    [Password] [nvarchar](255) NOT NULL,
    [PasswordCreationDate] [datetime] NOT NULL,
    [IsActive] [bit] NOT NULL CONSTRAINT [DF_User_IsActive]  DEFAULT ((1)),
    [ManagerID] [int] NULL,
    [SuperVisorID] [int] NULL,
    [TeamLeaderID] [int] NULL,
    [PermissionID] [int] NULL,
 CONSTRAINT [PK_User] PRIMARY KEY CLUSTERED 
(
    [UserID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

感谢所有的目光。我设法跟踪问题

罪魁祸首..我必须修改反射部分以检查它是否是一个字符串而不使用默认值为null。

public static object GetDefaultValue(this Type type)
    {
        if (type == null) throw new ArgumentNullException("type");

        Expression<Func<object>> e = Expression.Lambda<Func<object>>(
            Expression.Convert(
            Expression.Default(type), typeof(object)
            )
        );

        // Compile and return the value.
        return e.Compile()();
    }

3 个答案:

答案 0 :(得分:3)

您不需要使用带引号的单引号。就像使用它们一样;

Telphone = @Telphone

使用单引号,sql将其视为string literal,而不是参数。

答案 1 :(得分:0)

你不应该在参数周围有撇号,这使得它们成为文字字符串值而不是参数。例如,它会在表格中保存'@LoginName'而不是'daniel'.。查询应为:

UPDATE [User] SET LoginName=@LoginName, Name=@Name,
Surname=@Surname, Telphone=@Telphone, 
Cellphone=@Cellphone, Fax=@Fax, Email=@Email, 
Password=@Password, PasswordCreationDate = @PasswordCreationDate, 
IsActive = @IsActive, ManagerID = @ManagerID, 
SuperVisorID = @SuperVisorID, TeamLeaderID = @TeamLeaderID, 
PermissionID = @PermissionID WHERE UserID = @UserID

关于@Telphone参数,在执行命令之前,您似乎确实没有将其添加到命令中。检查拼写,并检查将其添加到命令的位置和方式。

答案 2 :(得分:0)

试试这个:

new SqlParameter("@Telphone", DBNull.Value);