大家好,有人可以帮我解决这个问题吗?我花了将近 2 天的时间试图解决这个问题。
我在c#中有一个this参数查询
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 放在那里甚至认为我已经专门分配了 '' 所以它返回一个警告。
The parameterized query '(@UserID int,@LoginName nvarchar(6), @Name nvarchar(9),@Surname n' expects the parameter '@Telphone', which was not supplied.
但我确实给它分配了一个 '' new SqlParameter("@Telphone", "''");
我错过了什么?我需要另一双眼睛,请帮忙。
表定义:
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()();
}
最佳答案
关于c# - Sql参数和c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29581131/