我遇到以下代码的问题,我一直在
"程序或功能' InsertFile'期待参数' @ ID',这是 未提供"
我必须在返回ID时做错事。
ALTER PROCEDURE [dbo].[InsertFile]
-- Add the parameters for the stored procedure here
--@AssetID INT,
@ComputerName varchar(max),
@FilePath varchar(max),
@Owner varchar(100),
@Size int,
@Extension varchar(50),
@CreationDate datetime,
@ModifiedDate datetime,
@AccessedDate datetime,
@ID int output
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
IF NOT EXISTS (SELECT * From DC_Files Where computerName = @ComputerName AND FilePath = @FilePath)
BEGIN
INSERT INTO DC_Files (ComputerName, FilePath, Owner, Size, Extension, CreationDate, ModifiedDate, AccessedDate)
VALUES (@ComputerName, @FilePath, @Owner, @Size, @Extension, @CreationDate, @ModifiedDate, @AccessedDate)
END
ELSE
BEGIN
UPDATE DC_Files
SET Owner = @Owner, Size = @Size, CreationDate = @CreationDate, ModifiedDate = @ModifiedDate, AccessedDate = @AccessedDate
WHERE computerName = @ComputerName AND FilePath = @FilePath
END
SET @ID = SCOPE_IDENTITY()
END
C#代码:
SqlCommand cmd = new SqlCommand("InsertFile",conn);
cmd.CommandType = CommandType.StoredProcedure;
//cmd.Parameters.AddWithValue("@AssetID", FileInfo);
cmd.Parameters.AddWithValue("@ComputerName", Environment.MachineName);
cmd.Parameters.AddWithValue("@FilePath", FilePath);
cmd.Parameters.AddWithValue("@Owner", FileSecurity.GetOwner(typeof(NTAccount)).Value);
cmd.Parameters.AddWithValue("@Size", FileInfo.Length);
cmd.Parameters.AddWithValue("@Extension", FileInfo.Extension);
cmd.Parameters.AddWithValue("@CreationDate", FileCreationTime);
cmd.Parameters.AddWithValue("@ModifiedDate", FileModifiedTime);
cmd.Parameters.AddWithValue("@AccessedDate", FileAccessedTime);
var returnParameter = cmd.Parameters.Add("@ID", SqlDbType.Int);
cmd.ExecuteNonQuery();
答案 0 :(得分:4)
您必须将方向设置为输出,因为默认情况下,所有参数的方向都是输入。
// Create parameter with Direction as Output
SqlParameter returnParameter = new SqlParameter("@ID", SqlDbType.Int)
{
Direction = ParameterDirection.Output
};
cmd.Parameters.Add(returnParameter);
答案 1 :(得分:0)
尝试在您的C#代码
中添加输出参数,如下所示SqlParameter outPutParameter = new SqlParameter();
outPutParameter.ParameterName = "@ID";
outPutParameter.SqlDbType = System.Data.SqlDbType.Int;
outPutParameter.Direction = System.Data.ParameterDirection.Output;
cmd.Parameters.Add(outPutParameter);