ASP.NET MVC - 存储过程执行返回无效对象
Posted
技术标签:
【中文标题】ASP.NET MVC - 存储过程执行返回无效对象【英文标题】:ASP.NET MVC - stored procedure execution returns invalid object 【发布时间】:2022-01-16 19:42:42 【问题描述】:我正在开发一个 ASP.NET MVC Web 应用程序,当我尝试通过 .ExecuteReader()
执行存储过程时,我收到“无效对象”错误。我是新手,所以我不确定如何纠正它。我试图研究它,但无法找到答案。
请看下面我的代码。
internal class CostCtrDAO
private string connectionString = @"Data Source=DESKTOP-54N4IDK\SQLSERVER2019;Initial Catalog=""Ceiling Database New"";Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
//public string Cost_Center_Name get; private set;
//internal List<CeilingByCostCtr> FetchAll()
//
// throw new NotImplementedException();
//
//performs all operations on the database - get all, create, delete, get one, search
public List<CeilingByCostCtr> FetchAll()
//if (CostCtr is null)
//
// throw new ArgumentNullException(nameof(CostCtr));
//
List<CeilingByCostCtr> returnList = new List<CeilingByCostCtr>();
// Access the database
using (SqlConnection connection = new SqlConnection(connectionString))
string sqlQuery = "SELECT * FROM dbo.uspGetCostCtrByParam";
SqlCommand command = new SqlCommand(sqlQuery, connection);
//command.CommandType = System.Data.CommandType.StoredProcedure;
// Associate @CostCtr with parameter CostCtr
// command.Parameters.AddWithValue("@CostCtr", System.Data.SqlDbType.NVarChar).Value = CostCtr;
connection.Open();
SqlDataReader reader = command.ExecuteReader();
CeilingByCostCtr ceilingByCostCtr = new CeilingByCostCtr();
if (reader.HasRows)
while (reader.Read())
ceilingByCostCtr.CostCtr = reader.GetString(0);
ceilingByCostCtr.Cost_Center_Name = reader.GetString(1);
ceilingByCostCtr.BR = reader.GetString(2);
ceilingByCostCtr.Branch_Name = reader.GetString(3);
ceilingByCostCtr.Division = reader.GetString(4);
ceilingByCostCtr.Division_Name = reader.GetString(5);
ceilingByCostCtr.New = reader.GetString(6);
ceilingByCostCtr.Date = reader.GetString(7);
ceilingByCostCtr.Type = reader.GetString(8);
ceilingByCostCtr.Ref2 = reader.GetString(9);
ceilingByCostCtr.Proj_Code = reader.GetString(10);
ceilingByCostCtr.Proj_Name = reader.GetString(11);
ceilingByCostCtr.Act_Code = reader.GetString(12);
ceilingByCostCtr.Act_Name = reader.GetString(13);
ceilingByCostCtr.B_or_O = reader.GetString(14);
ceilingByCostCtr.Jul = reader.GetString(15);
ceilingByCostCtr.Aug = reader.GetString(16);
ceilingByCostCtr.Sep = reader.GetString(17);
ceilingByCostCtr.Oct = reader.GetString(18);
ceilingByCostCtr.Nov = reader.GetString(19);
ceilingByCostCtr.Dec = reader.GetString(20);
ceilingByCostCtr.Jan = reader.GetString(21);
ceilingByCostCtr.Feb = reader.GetString(22);
ceilingByCostCtr.Mar = reader.GetString(23);
ceilingByCostCtr.Apr = reader.GetString(24);
ceilingByCostCtr.May = reader.GetString(25);
ceilingByCostCtr.Jun = reader.GetString(26);
ceilingByCostCtr.Perm = reader.GetString(27);
ceilingByCostCtr.Temp = reader.GetString(28);
ceilingByCostCtr.LimitedTerm = reader.GetString(29);
ceilingByCostCtr.Monthly = reader.GetDecimal(30);
ceilingByCostCtr.Analyst = reader.GetString(31);
ceilingByCostCtr.AvgPYs = reader.GetDecimal(32);
ceilingByCostCtr.CalcPEs = reader.GetDecimal(33);
ceilingByCostCtr.Sal_Rate = reader.GetDecimal(34);
ceilingByCostCtr.Auth = reader.GetDecimal(35);
ceilingByCostCtr.Dollar_Adj = reader.GetDecimal(36);
ceilingByCostCtr.Budget = reader.GetDecimal(37);
ceilingByCostCtr.Mo_Rate = reader.GetDecimal(38);
ceilingByCostCtr.Import = reader.GetString(39);
ceilingByCostCtr.BudgetYear = reader.GetInt32(40);
ceilingByCostCtr.OngoingStatus = reader.GetString(41);
ceilingByCostCtr.OneTimeEndingDate = reader.GetInt32(42);
ceilingByCostCtr.Fund = reader.GetString(43);
ceilingByCostCtr.Program = reader.GetString(44);
ceilingByCostCtr.Element = reader.GetString(45);
returnList.Add(ceilingByCostCtr);
return returnList;
【问题讨论】:
您必须验证用于连接数据库的凭据是否可以看到存储的过程,并且您的名称正确。您发布的代码中也没有任何内容可以说明问题所在。 奇怪的是,即使它没有参数,您也不能从函数中进行这样的选择,但是您会收到不同的错误Parameters were not supplied for the function ''.
【参考方案1】:
如果dbo.uspGetCostCtrByParam
确实是您数据库中的存储过程,那么您应该像这样访问它:
// Access the database
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand("dbo.uspGetCostCtrByParam", connection))
command.CommandType = CommandType.StoredProcedure;
....
// possibly setting parameters here??
....
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
// now iterate over the rows returned and get what you need
您不能运行SELECT * FROM
来获取存储过程的结果...
【讨论】:
以上是关于ASP.NET MVC - 存储过程执行返回无效对象的主要内容,如果未能解决你的问题,请参考以下文章
从返回多个实际表的复杂存储过程中检索 asp.net MVC 中的数据
ASP.Net MVC 5 Sending Email 错误:根据验证程序,远程证书无效
asp.net mvc 从视图中的文本框获取数据返回存储过程以编辑记录
在 ASP.NET MVC 中执行 SQL Server 存储过程时如何防止超时错误?