Asp.net Sql参数丢失

时间:2013-08-30 09:44:21

标签: asp.net sql sql-server stored-procedures

我试图用一些参数调用存储过程但是得到

的错误
  

“过程或函数”sp_User_Create'期望参数   '@adminUser',未提供。“

当我发送值为0

但是当我使用大于0的值调用存储过程时,它可以正常工作吗?

这是我的代码......

SqlConnection con;
SqlCommand cmd = new SqlCommand();
con = new SqlConnection("server=XXX.XXX.XXX.XXX; database=xxxxxxx; uid=xxxxxxx; pwd=xxxxx");
cmd = new SqlCommand("sp_User_Create", con);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.Add(new SqlParameter("fullName", txtFirstName.Text + " " + txtLastName.Text));
cmd.Parameters.Add(new SqlParameter("emailAddress", txtEmailAddress.Text));
cmd.Parameters.Add(new SqlParameter("password", txtPassword.Text));
cmd.Parameters.Add(new SqlParameter("phoneNumber", txtPhoneNumber.Text));
cmd.Parameters.Add(new SqlParameter("jobTitle", txtJobTitle.Text));
cmd.Parameters.Add(new SqlParameter("createDate", DateTime.Now));
cmd.Parameters.Add(new SqlParameter("companyId", 116));
cmd.Parameters.Add(new SqlParameter("adminUser", 0));
cmd.Parameters.Add(new SqlParameter("userExpireDate", DateTime.Now.AddDays(7)));
cmd.Parameters.Add(new SqlParameter("userAccessLeaderboardClips", 99));
cmd.Parameters.Add(new SqlParameter("userAccessLeaderboardUsers", 99));
cmd.Parameters.Add(new SqlParameter("userAccessLeaderboardVideos", 99));
cmd.Parameters.Add(new SqlParameter("userAccessShared", 99));
cmd.Parameters.Add(new SqlParameter("userAccessEmbed", 99));
cmd.Parameters.Add(new SqlParameter("userAccessTinyUrl", 1));
cmd.Parameters.Add(new SqlParameter("userAccessAllowCreatedVideos", 1));
cmd.Parameters.Add(new SqlParameter("userAccessDownload", 99));
cmd.Parameters.Add(new SqlParameter("companyName", "My Company Name"));
cmd.Parameters.Add(new SqlParameter("logoName", "the logo name"));
cmd.Parameters.Add(new SqlParameter("postcode", "XXXX XXX"));
cmd.Parameters.Add(new SqlParameter("optIn", true));
cmd.Parameters.Add(new SqlParameter("accountType", ""));
cmd.Parameters.Add(new SqlParameter("transactionDesc", ""));
cmd.Parameters.Add(new SqlParameter("transactionDate", ""));
cmd.Parameters.Add(new SqlParameter("transactionAmount", ""));

con.Open();
cmd.ExecuteNonQuery();
con.Close();

我检查了存储过程,并且期待:

@adminUser as int

这是存储过程:

USE [vCreateDev]
GO
/****** Object:  StoredProcedure [dbo].[sp_User_Create]    Script Date: 29/08/2013 10:47:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:      Trevor Daniel
-- Create date: 28/8/2013
-- Description: Create a user
-- =============================================
ALTER PROCEDURE [dbo].[sp_User_Create]

-- UserLogin Fields
@fullName as varchar(100),
@emailAddress as varchar(100),
@password as varchar(20),
@phoneNumber as varchar(100),
@jobTitle as varchar,
@createDate as datetime,
@companyId as int,
@adminUser as int,
@userExpireDate as datetime,
@userAccessLeaderboardClips as int,
@userAccessLeaderboardUsers as int,
@userAccessLeaderboardVideos as int,
@userAccessShared as int,
@userAccessEmbed as int,
@userAccessTinyUrl as int,
@userAccessAllowCreatedVideos as int,
@userAccessDownload int,

-- ClientCompanyDetailsPerUser
@companyName varchar(200),
@logoName varchar(200),

-- UserCustomerDetails
@postcode varchar(15),
@optIn bit,
@accountType varchar(50),

-- UserTransactions
@transactionDesc varchar(150),
@transactionDate datetime,
@TransactionAmount smallmoney

-- Outputs
--@UserLoginID INT OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;

-- Insert statements for procedure here
INSERT INTO UserLogin(
Username,
Password,
AuthID,
DateCreated,
LastLogin,
Fullname,
Title,
Mobile,
DDI,
Email,
AdminUser,
CompanyID,
UserExpireDate,
CurrentLogin,
LastActionDateTime,
ActiveLicense,
UserAccessLeaderboardClips,
UserAccessLeaderboardUsers,
UserAccessLeaderboardVideos, 
UserAccessShared, 
UserAccessEmbed,
UserAccessTinyUrl,
UserAccessAllowCreatedVideos,
UserAccessDownload)
VALUES(
@emailAddress,
@password,
null,
@createDate,
null,
@fullName,
@jobTitle,
@phoneNumber,
null,
@emailAddress,
@adminUser,
@companyId,
@userExpireDate,
null,
null,
null,
@userAccessLeaderboardClips,
@userAccessLeaderboardUsers,
@userAccessLeaderboardVideos,
@userAccessShared,
@userAccessEmbed,
@userAccessTinyUrl,
@userAccessAllowCreatedVideos,
@userAccessDownload)

DECLARE @NewUserID as int

SET @NewUserID = SCOPE_IDENTITY()

INSERT INTO ClientCompanyDetailsPerUser(CompanyName, CompanyLogoLocation, LargeCompanyLogoLocation, UserID)
VALUES (@companyName,@logoName,@logoName,@NewUserID)

INSERT INTO UserCustomerDetails(UserID, PostCode, OptIn, AccountType)
VALUES (@NewUserID,@postcode,@optIn,@accountType)

INSERT INTO UserTransactions(UserID, TransactionDesc, TransactionDate, TransactionAmount, AccountType, ResellerParameter, CampaignParameter)
VALUES (@NewUserID,@transactionDesc,@transactionDate,@TransactionAmount,@accountType,NULL,NULL)

END

我只是无法理解为什么当发送零时它表示参数丢失但是当我发送大于零的东西时它完全正常工作?

4 个答案:

答案 0 :(得分:2)

使用SqlParameter构造函数的此重载指定整数参数值时请小心。因为此重载采用Object类型的值,所以当值为零时,必须将整数值转换为Object类型。

如果您不执行此转换,编译器会假定您尝试调用SqlParameter (string, SqlDbType)构造函数重载.- MSDN

尝试使用此

cmd.Parameters.Add(new SqlParameter("@adminUser", SqlDbType.Int).Value=0);

答案 1 :(得分:0)

您需要在参数名称之前添加“@”,这样对于要传递给过程的所有参数

cmd.Parameters.Add(new SqlParameter("@adminUser", 0));

答案 2 :(得分:0)

宾果! - 修好了。

我已将代码更改为:

cmd.Parameters.AddWithValue("@adminUser", 0);

感谢您寻求帮助!

崔佛

答案 3 :(得分:0)

你需要在每个参数名称之前添加@符号,就像你在中添加了一样       cmd.Parameters.AddWithValue(“@ adminUser”,0); for cmd.Parameters.Add(new SqlParameter(“adminUser”,0));