在当前上下文中不存在命名的"CommandType"

use*_*283 5 .net c# sql asp.net asp.net-mvc

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

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();
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

根据文档https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtype(v=vs.110).aspx我看不出我做错了什么,正如我所包括的那样System.Data.SqlClient.

Chr*_*tos 12

您错过了包含System.Data命名空间.因此,添加以下内容,您将解决您的问题.

using System.Data;
Run Code Online (Sandbox Code Playgroud)