我有这样的参数;
comm.CommandText = "usp_ProjectsCreateNew";
SqlParameter param = comm.CreateParameter();
param.ParameterName = "@ProjectId";
param.Direction = ParameterDirection.Output;
param.DbType = DbType.Int32;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@ProjectHeading";
param.Value = heading;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@ProjectLongDescription";
param.Value = longSummary;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@ProjectUrl";
param.Value = url;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@PromoFront";
param.Value = promoFront;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@ProjectThumbnail";
param.Value = thumbnailFileName;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@ProjectImage2";
param.Value = imageFileName1;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@ProjectImage3";
param.Value = imageFileName2;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@ProjectImage4";
param.Value = imageFileName3;
comm.Parameters.Add(param);
param = comm.CreateParameter();
param.ParameterName = "@ProjectImage5";
param.Value = imageFileName4;
comm.Parameters.Add(param);
comm.Transaction = tran;
int result = comm.ExecuteNonQuery();
我的存储过程代码是这样的;
CREATE PROC [dbo].[usp_ProjectsCreateNew]
(
@ProjectId INT output,
@ProjectHeading varchar(100),
@ProjectLongDescription varchar(max),
@ProjectUrl varchar(100),
@PromoFront bit,
@ProjectThumbnail varchar(50),
@ProjectImage2 varchar(50),
@ProjectImage3 varchar(50),
@ProjectImage4 varchar(50),
@ProjectImage5 varchar(50)
)
AS
BEGIN TRY
BEGIN TRAN
INSERT INTO [Projects]([ProjectHeading], [ProjectLongSummary], [ProjectUrl],
[PromoFront], [ProjectThumbnail], [ProjectImage2], [ProjectImage3], [ProjectImage4], [ProjectImage5])
VALUES(@ProjectHeading, @ProjectLongDescription, @ProjectUrl,
@PromoFront, @ProjectThumbnail, @ProjectImage2, @ProjectImage3, @ProjectImage4, @ProjectImage5);
SELECT @ProjectId = @@IDENTITY;
COMMIT TRAN
END Try
BEGIN CATCH
PRINT Error_Message();
ROLLBACK TRAN
END CATCH
但是我收到了这个错误;
消息:过程或函数'usp_ProjectsCreateNew'需要参数'@ProjectId',这是未提供的。
来源:.Net SqlClient数据提供商
方法:Void OnError(System.Data.SqlClient.SqlException,Boolean,System.Action`1 [System.Action])
答案 0 :(得分:2)
首先,你没有告诉SqlCommand
它是一个存储过程!
添加以下行:
comm.CommandType = CommandType.StoredProcedure;
设置comm
的{{1}}属性后。
然后:你的一些参数不清楚 - 我建议你使用这样的东西来创建和添加.CommandText
对象的参数:
SqlCommand
我总是会为每个参数明确定义cmd.Parameters.Add("@PromoFront", SqlDbType.Bit).Value = 1;
,以使其绝对清晰。不这样做可能会导致不必要的结果。