System.Data.SqlClient.SqlException:列名或提供的值数与表定义不匹配

时间:2016-08-20 09:05:55

标签: c# sql .net asp.net-mvc tsql

我希望用户输入显示的所有数据(数据库中的列),填写并提交。我无法使用HTML.HiddenFor元素自动生成Id和CompanyID。

  

System.Data.SqlClient.SqlException:列名或提供的值数与表定义不匹配..

型号:

namespace Project.Models
{
    public class Contact
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public int Phone { get; set; }
        public int CompanyID { get; set; }
    }
}

查看:

@model Project.Contact

<h2>Create Contact</h2>

@using (Html.BeginForm("Create", "Home", FormMethod.Post))
{
    <table>
        <tr>
            <th>First name</th>
            <th>Last name</th>
            <th>Email</th>
            <th>Phone</th>
        </tr>
        <tr>
            <td>@Html.HiddenFor(m => m.Id)</td>
            <td></td>
            <td>@Html.TextBoxFor(m => m.FirstName)</td>
            <td>@Html.TextBoxFor(m => m.LastName)</td>
            <td>@Html.TextBoxFor(m => m.Email)</td>
            <td>@Html.TextBoxFor(m => m.Phone)</td>
            <td>@Html.HiddenFor(m => m.CompanyID)</td>
        </tr>

    </table>
    <br />
    <input type="submit" name="btn" class="btn-primary" value="Create" />
}

控制器:

using Arbetsprov_Sublime___Andre_Kordasti.Models;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Web.Mvc;

namespace Arbetsprov_Sublime___Andre_Kordasti.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(Contact model)
        {
            var connection = new SqlConnection(@"Data Source=.\SQLExpress;Initial Catalog=Sublime;Integrated Security=True");
            connection.Open();

            var command = new SqlCommand("INSERT INTO Contact Values('" + model.Id + "','" + model.FirstName + " " + model.LastName + "','" + model.Email + " " + model.Phone + "','" + model.CompanyID + "')", connection);
            command.ExecuteNonQuery();

            return View();
        }
    }
}

3 个答案:

答案 0 :(得分:1)

var command = new SqlCommand("INSERT INTO Contact(Id,FirstName,LastName,Email,Phone,CompanyId) Values('" + model.Id + "','" + model.FirstName + " " + model.LastName + "','" + model.Email + " " + model.Phone + "','" + model.CompanyID + "')", connection);
command.ExecuteNonQuery();

如果有一个必须是autoGenerated的列,你必须告诉你给出哪些列的值,以便sql知道未提及的属性应该是autoGenerated

答案 1 :(得分:0)

也许您的联系表“ID”列值转发器。例如:当前身份证已经有

var command = new SqlCommand("INSERT INTO Contact (Id, FirstName, LastName, Email , Phone, CompanyID) Values('" + model.Id + "','" + model.FirstName + " " + model.LastName + "','" + model.Email + " " + model.Phone + "','" + model.CompanyID + "')", connection);
command.ExecuteNonQuery();

或(如果你的表id列是自动递增的话)

var command = new SqlCommand("INSERT INTO Contact (FirstName, LastName, Email , Phone, CompanyID) Values('" + model.FirstName + " " + model.LastName + "','" + model.Email + " " + model.Phone + "','" + model.CompanyID + "')", connection);
command.ExecuteNonQuery();

答案 2 :(得分:0)

尝试不向其发送ID

var command = new SqlCommand("INSERT INTO Contact(FirstName,LastName,Email,Phone,CompanyId) Values("'"+ model.FirstName + "',' " + model.LastName + "','" + model.Email + "',' " + model.Phone + "','" + model.CompanyID + "')", connection);
command.ExecuteNonQuery();

如果它没有工作,请告诉我抛出的异常是相同的还是改变的。

您正在使用的查询中发送给我的评论中有一些comass缺失&#39;,&#39;。

当你说

INSERT INTO tbl(Column1,Column2,Column3) values ('value1''value2'+','value3')

它会给你同样的错误,因为你提到了3列,但只提供了2个值。希望我清楚。