当前上下文中不存在名为“CommandType”的

问题描述 投票:0回答:5

有人可以帮助我理解为什么我会在这里收到该错误吗?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Configuration;

namespace JsPractice.Controllers
{
    public class SolutionController : Controller
    {

        public ActionResult Index ( )
        {

            return View();
        }

        [HttpPost]
        public ActionResult CreateNew ( int problem_id, string solver, string solution_code, string test_code )
        {
            // Going to move this to controller later .. 
            using ( SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalJsPracticeDb"].ConnectionString) )
            {
                using ( SqlCommand cmd = new SqlCommand("AddSolution", con) )
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@problem_id", problem_id);
                    cmd.Parameters.AddWithValue("@solver", solver);
                    cmd.Parameters.AddWithValue("@solution_code", solution_code);
                    cmd.Parameters.AddWithValue("@test_code", test_code);
                    con.Open();
                    cmd.ExecuteNonQuery();
                }

            }
            return View();
        }

    }
}

根据文档https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtype(v=vs.110).aspx我不明白我在做什么做错了,因为我已经包括了

System.Data.SqlClient

c# sql asp.net .net asp.net-mvc
5个回答
15
投票

您错过了包含

System.Data
命名空间。因此添加以下内容,您将解决您的问题。

using System.Data;

1
投票

对于此类错误,右键单击错误词并说“解决”。如果您的项目中引用了定义程序集,则会出现所有错误。


1
投票

您错过了命名空间。添加以下行。

using System.Data;

它会解决你的问题。


0
投票

请更换此行:

cmd.CommandType = CommandType.StoredProcedure;

有了这个:

cmd.CommandType = System.Data.CommandType.StoredProcedure;

-1
投票

在页面顶部添加

using System.Data
或只需单击命令类型 - 在那里您可以看到类似灯泡的图标,单击它并添加
using System.Data;

© www.soinside.com 2019 - 2024. All rights reserved.