Sql server 从在ASP.NET MVC中生成空白列标题的SQL Server存储过程中访问数据
我有一个具有以下输出的SQL Server存储过程:Sql server 从在ASP.NET MVC中生成空白列标题的SQL Server存储过程中访问数据,sql-server,asp.net-mvc,stored-procedures,Sql Server,Asp.net Mvc,Stored Procedures,我有一个具有以下输出的SQL Server存储过程: unit_id on_date notes type_code type_order status (No column name) (No column name) 3 2016-12-08 00:00:00.000 AVL -1 D NULL 16 3 2016-12
unit_id on_date notes type_code type_order status (No column name) (No column name)
3 2016-12-08 00:00:00.000 AVL -1 D NULL 16
3 2016-12-08 00:00:00.000 RSU 1 D 3 2
3 2016-12-08 00:00:00.000 TOW 2 D 6 5
……等等
最后两列没有列名
当我使用存储过程创建模型时,它将它们列为
public Nullable<int> Column1 { get; set; }
public Nullable<int> Column2 { get; set; }
但我犯了个错误
System.IndexOutOfRangeException:“Column1”
因此,我的控制器说,Column1
和Column2
在存储过程返回的结果中不作为列标题存在(即使模型支架是从存储过程生成的)
有人能告诉我如何访问这些列吗
谢谢如果无法从存储过程内部为这些列提供列别名,那么您唯一的选择就是按位置访问这些列(而不是按名称) 因此,不是:
Column1 = SPOutput3["Column1"] as int?,
使用
并将“42”替换为该列在结果集中的任何列位置(基于0)。Hi@CodeCaster,当我尝试时,
Column1=SPOutput.GetInt16(0),
I getSystem.InvalidCastException:“指定的强制转换无效”。
能否修改存储过程以显式提供列名(别名)对于这两列?这可能是解决这一长期问题的最简单方法term@marc_s,同意,但不幸的是,如果不能在存储过程中提供显式的列别名,那么访问这些列的唯一方法就是通过position-Column1=SPOutput3[42]as int?
(并替换“42”)无论该列的实际位置如何)
Column1 = SPOutput3["Column1"] as int?,
Column1 = SPOutput3[42] as int?,