大家好,有人可以帮我这个忙。我花了将近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()();
}
最佳答案
您不需要在参数中使用单引号。就像这样使用它们;
Telphone = @Telphone
对于单引号,sql将其视为string literal,而不是参数。
关于c# - SqlParameter和C#,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29581131/