2016-08-19 66 views
-3

我希望用户输入显示的所有数据(数据库中的列),填写并提交。但我不能让我的代码工作,我在这里做错了什么?我得到的错误:使用HTML帮助程序在SQL Server数据库中创建记录MVC

System.Data.SqlClient.SqlException: Incorrect syntax near 'Contact'.

型号:

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> 

<table> 
    <tr> 
     <th>First name</th> 
     <th>Last name</th> 
     <th>Email</th> 
     <th>Phone</th> 
    </tr> 
    <tr> 
     <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> 
    </tr> 
    @Html.HiddenFor(m => m.Id) 
    @Html.HiddenFor(m => m.CompanyID) 
</table> 
<br /> 
@using (Html.BeginForm()) 
{ 
    <input type="button" name="button" class="button1" value="Create" /> 
} 

控制器:

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

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

     public ActionResult Create(List<Contact> model) 
     { 
      string query = "INSERT INTO Contact"; 

      using (var connection = new SqlConnection(@"Data Source=.\SQLExpress;Initial Catalog=Sublime;Integrated Security=True")) 
      { 
       using (var command = new SqlCommand(query, connection)) 
       { 
        connection.Open(); 

        using (var dr = command.ExecuteReader()) 
        { 
         if (dr.HasRows) 
         { 
          while (dr.Read()) 
          { 
           var contact = new Contact(); 
           contact.Id = dr.GetInt32(dr.GetOrdinal("Id")); 
           contact.FirstName = dr.GetString(dr.GetOrdinal("FirstName")); 
           contact.LastName = dr.GetString(dr.GetOrdinal("LastName")); 
           contact.Email = dr.GetString(dr.GetOrdinal("Email")); 
           contact.Phone = dr.GetInt32(dr.GetOrdinal("Phone")); 
           contact.CompanyID = dr.GetInt32(dr.GetOrdinal("CompanyID")); 
           model.Add(contact); 
          } 
         } 
        } 
       } 
      } 

      return View(model); 
     } 
    } 
} 

我不是也知道我的文本框的连接是否正确对我的行动...

+4

有很多你的代码错误。在SO上有任何商业发布之前,您需要点击一些教科书并学习一些基本的T-SQL和ASP.NET MVC。 – Jakotheshadows

+3

了解[SQL Server插入语句](https://technet.microsoft.com/zh-cn/library/dd776381(v = sql.105).aspx)的工作原理。 – ekad

+0

具体有什么问题? – Malphai

回答

1

您正在使用ExecuteReader,它是一种获取数据的方法,尝试执行Insert。

您需要改用ExecuteNonQuery;最好的做法是通过参数添加数据。

第二个问题是您的INSERT命令不完整。

然后,第三个问题是您传递一个列表,所以您需要遍历列表,每个元素执行一次INSERT。

https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.executenonquery(v=vs.110).aspx

public ActionResult Create(List<Contact> model) 
    { 
     string query = "INSERT INTO Contact (Id, FirstName, LastName, Email, " 
        + "Phone, CompanyId) values (@Id,@FirstName,@LastName" 
        + ",@Email,@Phone,@CompanyID)"; 

     using (var connection = new SqlConnection(@"Data Source=.\SQLExpress;Initial Catalog=Sublime;Integrated Security=True")) 
     { 
      connection.Open(); 
      foreach(Contact modelElement in model) 
      { 
       SqlCommand command = new SqlCommand(queryString, connection); 
       command.Parameters.AddWithValue("@Id", modelElement.Id); 
       command.Parameters.AddWithValue("@FirstName", modelElement.FirstName); 
       command.Parameters.AddWithValue("@LastName", modelElement.LastName); 
       command.Parameters.AddWithValue("@Email", modelElement.Email); 
       command.Parameters.AddWithValue("@Phone", modelElement.Phone); 
       command.Parameters.AddWithValue("@CompanyID", modelElement.CompanyID); 
       command.ExecuteNonQuery(); 
      } 
     } 
    } 
+0

我试过你的代码,我得到“System.NullReferenceException:对象引用未设置为对象的实例” – Malphai

+3

不要使用'?'作为SQL的参数占位符服务器 - 在您的SQL查询语句 –

+0

Malphai中,使用*命名*参数,如“@ Id”,“@ FirstName”等,你从哪里得到异常? –