【问题标题】:Create record in SQL Server database with HTML helper MVC [closed]使用 HTML 助手 MVC 在 SQL Server 数据库中创建记录 [关闭]
【发布时间】:2016-12-27 00:14:06
【问题描述】:

我希望用户输入所有显示的数据(数据库中的列),填写并提交。但是我无法让我的代码工作,我在这里做错了什么?我得到错误:

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>

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

我也不确定我的文本框是否正确连接到我的操作...

【问题讨论】:

  • 你的代码有很多问题。在 SO 上发布任何业务之前,您需要阅读一些教科书并学习一些基本的 T-SQL 和 ASP.NET MVC。
  • 先了解SQL Server Insert Statement的工作原理。
  • 具体有什么问题?
  • 您的表单甚至不包含任何表单控件(它们都在表单标签之外。您甚至没有向您展示 POST 方法(只是Create 的 GET 方法,并且其中的代码使毫无意义)。
  • 我真的能得到任何有用的建议吗?有一些代码差异?

标签: c# sql-server asp.net-mvc database create-table


【解决方案1】:

您正在使用 ExecuteReader,这是一种获取数据的方法,正在尝试执行插入操作。

您需要改用 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();
            }
        }
    }

【讨论】:

  • 我试过你的代码,我得到“System.NullReferenceException: object reference not set to an instance of an object”
  • 不要使用 ? 作为 SQL Server 的参数占位符 - 在 SQL 查询语句中使用 命名 参数,如 @Id@FirstName
  • Malphai,你从哪里得到异常? (行)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多