SqlException:过程或函数 sqQuestion_AddNewQuestion 指定的参数过多
Posted
技术标签:
【中文标题】SqlException:过程或函数 sqQuestion_AddNewQuestion 指定的参数过多【英文标题】:SqlException: Procedure or function sqQuestion_AddNewQuestion has too many arguments specified 【发布时间】:2021-02-20 03:37:12 【问题描述】:我正在尝试将一些数据输入到本地 SQL Server 数据库中。但是,尽管多次计算参数的数量,我仍然得到这个错误:
过程或函数 sqQuestion_AddNewQuestion 指定的参数过多
问题出现在SqlDataAccess.SaveDataInTransaction
。关于此的许多其他问题都涉及导致问题的 foreach 循环,但我没有使用。
我对 C# 还很陌生,所以也许我错过了显而易见的东西。
代码如下:
dbo.问题:
CREATE TABLE [dbo].[Questions]
(
[Id] INT NOT NULL PRIMARY KEY IDENTITY(1,1),
[Topic] NVARCHAR(50) NOT NULL,
[Title] NVARCHAR(50) NOT NULL,
[Description] NVARCHAR(200) NOT NULL,
[AuthorId] NVARCHAR(128) NOT NULL,
[Image] NVARCHAR(MAX) NULL,
[CreatedDate] DATETIME2 NOT NULL DEFAULT getutcdate()
)
存储过程:
CREATE PROCEDURE [dbo].[sqQuestion_AddNewQuestion]
@Topic nvarchar(50),
@Title nvarchar(50),
@Description nvarchar(200),
@AuthorId nvarchar(128),
@CreatedDate datetime2,
@Image nvarchar(Max)
AS
BEGIN
SET NOCOUNT ON
INSERT INTO dbo.Questions(Topic, Title, [Description], AuthorId, CreatedDate, [Image])
VALUES (@Topic, @Title, @Description, @AuthorId, @CreatedDate, @Image)
END
问题数据库模型:
public class QuestionDbModel
public int Id get; set;
public string Topic get; set;
public string Title get; set;
public string Description get; set;
public string Image get; set;
public string AuthorId get; set;
public DateTime CreatedDate get; set;
public List<CommentModel> Comments get; set;
QuestionModel(用于允许用户上传图片):
public class QuestionModel
public int Id get; set;
public string Topic get; set;
public string Title get; set;
public string Description get; set;
[DisplayName("Image")]
public string Image get; set;
[NotMapped]
[DisplayName("Upload Image")]
public IFormFile ImageFile get; set;
public string AuthorId get; set;
public DateTime CreatedDate get; set;
public List<CommentModel> Comments get; set;
仪表板控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(QuestionModel question)
var wwwRootPath = _hostEnvironment.WebRootPath;
string file = Path.GetFileNameWithoutExtension(question.ImageFile.FileName);
string ext = Path.GetExtension(question.ImageFile.FileName);
string fileName = DateTime.Now.ToString("yymmssfff") + ext;
var path = Path.Combine(wwwRootPath + "/QuestionImages/", fileName);
using (var filestream = new FileStream(path, FileMode.Create))
question.ImageFile.CopyToAsync(filestream);
QuestionDbModel newQuestion = new QuestionDbModel
Topic = question.Topic,
Title = question.Title,
Description = question.Description,
Image = fileName,
AuthorId = question.AuthorId,
CreatedDate = question.CreatedDate
;
try
_sqlDataAccess.StartTransaction("*DAtabaseName*");
_sqlDataAccess.SaveDataInTransaction("dbo.sqQuestion_AddNewQuestion", newQuestion);
_sqlDataAccess.CommitTransaction();
return RedirectToAction(nameof(Index));
catch
_sqlDataAccess.RollbackTransation();
throw;
SqlDataAccess (SaveDataInTransaction):
public void SaveDataInTransaction<T>(string storedProcedure, T parameters)
_connection.Execute(storedProcedure, parameters,
commandType: CommandType.StoredProcedure, transaction: _transaction);
【问题讨论】:
【参考方案1】:“QuestionDbModel”类与预期参数列表不匹配。作为测试,尝试创建另一个类,该类的属性与存储过程期望的参数列表相匹配。如果这样可以解决问题,那么您可以决定使用继承或由“QuestionDbModel”类支持的接口,但只公开 6 个属性/参数。
【讨论】:
我删除了 List以上是关于SqlException:过程或函数 sqQuestion_AddNewQuestion 指定的参数过多的主要内容,如果未能解决你的问题,请参考以下文章
从休眠命名查询调用存储过程时出现 SQLException 00604 & 01003
当我尝试通过存储过程插入空值时出现 SqlException