Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
ASP.NET MVC中带参数的存储过程调用_Asp.net_Asp.net Mvc - Fatal编程技术网

ASP.NET MVC中带参数的存储过程调用

ASP.NET MVC中带参数的存储过程调用,asp.net,asp.net-mvc,Asp.net,Asp.net Mvc,我有一个用于数据库中另一个存储过程的工作控制器,但我正在尝试测试另一个 当我请求URL时 我得到以下错误消息,这是可以理解的,但我似乎无法找出它发生的原因 程序或功能 “esp_GetPlacesWithinGeoSpan”预期 参数'@MinLat',它不是 供应 这是我正在使用的代码 using System; using System.Collections.Generic; using System.Linq; using System.Web;

我有一个用于数据库中另一个存储过程的工作控制器,但我正在尝试测试另一个

当我请求URL时

我得到以下错误消息,这是可以理解的,但我似乎无法找出它发生的原因

程序或功能 “esp_GetPlacesWithinGeoSpan”预期 参数'@MinLat',它不是 供应

这是我正在使用的代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Mvc.Ajax;
    using System.Data;
    using System.Text;
    using System.Data.SqlClient;

namespace prototype.Controllers
{
    public class MapController : Controller
    {
        //Initial variable definitions
        //Array with chars to be used with the Trim() methods
        char[] lastComma = { ',' };

        //Minimum and maximum lat/longs for queries
        float _minLat;
        float _maxLat;
        float _minLng;
        float _maxLng;


        //Creates stringbuilder object to store SQL results
        StringBuilder json = new StringBuilder();

        //Defines which SQL-server to connect to, which database, and which user
        SqlConnection con = new SqlConnection(...connection string here...);

        // 
        // HTTP-GET: /Map/ 

        public string CallProcedure_getPlaces(float minLat, float maxLat, float minLng, float maxLng)
        {
            con.Open();

            using (SqlCommand cmd = new SqlCommand("esp_GetPlacesWithinGeoSpan", con))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("@MinLat", _minLat);
                cmd.Parameters.AddWithValue("@MaxLat", _maxLat);
                cmd.Parameters.AddWithValue("@MinLng", _minLng);
                cmd.Parameters.AddWithValue("@MaxLng", _maxLng);


                using (SqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        json.AppendFormat("\"{0}\":{{\"c\":{1},\"f\":{2}}},", reader["PlaceID"], reader["PlaceName"], reader["SquareID"]);
                    }
                }
                con.Close();
            }
            return "{" + json.ToString().TrimEnd(lastComma) + "}";
        }


        //http://host.com/Map?minLat=0&maxLat=50&minLng=0&maxLng=50
        public ActionResult Index(float minLat, float maxLat, float minLng, float maxLng)
        {
            _minLat = minLat;
            _maxLat = maxLat;
            _minLng = minLng;
            _maxLng = maxLng;

            return Content(CallProcedure_getPlaces(_minLat, _maxLat, _minLng, _maxLng));
        }
    }
}

如果您能帮助解决此问题,我们将不胜感激。

您的
CommandType
错误。应该是:

cmd.CommandType = CommandType.StoredProcedure;

由于您使用的是
CommandType.Text
,我猜想ADO.NET正在尝试将参数映射到查询的文本中,而不是生成对存储过程的正确调用。

如果您希望使用文本类型传递参数

CommandType.Text
然后您应该像这样传递参数:

GetPlacesWithinGeoSpan @MinLat, @MaxLat, @MinLng, @MaxLng
因为你这样做的方式就像你传递的参数,但是它没有映射到任何东西,所以它被忽略了


希望这会有帮助

太棒了!哈哈,真不敢相信我错过了!在创建过程之前,我正在使用文本进行测试。谢谢这是一个精神上的小问题。顺便说一句,你真的不应该在你的控制器中执行数据访问。这最好是通过使用另一个层来实现的,例如存储库。您能在此基础上进行扩展吗?或者给我举个例子?我对asp.net一点都不熟悉,我只是觉得这是一种简单的,因此是一种体面的方式。我很想学习。@cc0这里有一个在MVC中使用ADO.net(SqlConnection)和存储库的非常简单的例子: