繁体   English   中英

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

[英]System.Data.SqlClient.SqlException: 'Invalid column name ' '

我正在尝试从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 = "";
}

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

但是有一个大问题,那就是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语句的目的是当控件到达使用结束时,它将处置使用block的对象并释放内存。 它的目的不仅是为了自动关闭连接,基本上它还会处理连接对象,显然,连接也因此而关闭。

根据MSDN

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM