System.Data.SqlClient.SqlException:'无效的列名''

时间:2018-11-06 16:09:49

标签: c# sql-server visual-studio

我正在尝试从Visual Studio插入数据库表,但遇到相同的错误,我不知道这可能是什么。

  

System.Data.SqlClient.SqlException:'无效的列名'

这是我的代码,我制作了两个类,分别是Gateway,Dept和Form1:

namespace insertar
{
    class Dept
    {   
        public string Dept_No { get; set; }
        public string DNombre { get; set; }
        public string Loc { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace insertar
{
    class Gateway
    {
        public bool Save(Dept dept)
        {
            Form1 form = new Form1();

            string connectionString = @"Data Source=DESKTOP-IE39262;Initial Catalog=Hospital;Integrated Security=True";
            SqlConnection connection = new SqlConnection(connectionString);
            //connection.Open();

            /*string query = @"INSERT INTO Dept VALUES (" + dept.Dept_No + "," + dept.DNombre +
                             "," + dept.Loc + ")";*/

            SqlCommand command = new SqlCommand("INSERT INTO Dept(Dept_No, DNombre, Loc)" + "VALUES (" + dept.Dept_No + "," + dept.DNombre +
                             "," + dept.Loc + ")", connection);
            connection.Open();
            command.ExecuteNonQuery();
            connection.Close();

            return true;
        }
    }
}

private void guardarbtn_Click(object sender, EventArgs e)
{
    Dept dept = new Dept();
    dept.Dept_No = dept_no.Text;
    dept.DNombre = dnombre.Text;
    dept.Loc = loc.Text;

    Gateway gateaway = new Gateway(); //class gateway
    gateaway.Save(dept);

    MessageBox.Show("Departamento insertado exitosamente");

    dept_no.Text = "";
    dnombre.Text = "";
    loc.Text = "";
}

1 个答案:

答案 0 :(得分:5)

由插入值引起的错误是一个字符串,因此您需要使用'来包含您的值。

但是有一个大问题,比SQL-Injection多。

我建议您使用参数代替连接的SQL语句字符串。

确保参数数据类型的大小与表模式相同。

string connectionString = @"Data Source=DESKTOP-IE39262;Initial Catalog=Hospital;Integrated Security=True";
string sqlQuery = "INSERT INTO Dept (Dept_No, DNombre, Loc) VALUES (@Dept_No,@DNombre,@Loc)";
using (SqlConnection connection = new SqlConnection(connectionString))
using (SqlCommand command = new SqlCommand(sqlQuery, connection))
{
    command.Parameters.Add("@Dept_No", SqlDbType.VarChar,100).Value = dept.Dept_No;
    command.Parameters.Add("@DNombre", SqlDbType.VarChar, 100).Value = dept.DNombre;
    command.Parameters.Add("@Loc", SqlDbType.VarChar, 100).Value = dept.Loc;
    connection.Open();
    command.ExecuteNonQuery();
}

注意

我将使用using语句,因为Using语句的目的是当控制权到达使用结束时,它将处置使用阻塞的对象并释放内存。它的目的不仅是为了自动关闭连接,基本上还会处理连接对象,显然,连接也因此而关闭。

根据MSDN

  

通常,当您使用IDisposable对象时,应在using语句中声明并实例化它。 using语句以正确的方式在对象上调用Dispose方法,并且(如前所述,当您使用它时)它还会导致对象本身在调用Dispose时就超出范围。在using块中,该对象为只读对象,无法修改或重新分配。

     

using语句确保即使在调用对象的方法时发生异常,也将调用Dispose。通过将对象放在try块中,然后在finally块中调用Dispose,可以达到相同的结果。实际上,这就是编译器翻译using语句的方式。前面的代码示例在编译时扩展为以下代码(请注意,花括号用于创建对象的有限作用域):

因此您可以减少代码connection.Close();,因为using可以帮助您做到这一点。

相关问题