Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# System.Data.SqlClient.SqlException:';无效的列名'';_C#_Sql Server_Visual Studio - Fatal编程技术网

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

C# System.Data.SqlClient.SqlException:';无效的列名'';,c#,sql-server,visual-studio,C#,Sql Server,Visual Studio,我试图从VisualStudio中插入到数据库表中,但遇到了相同的错误,我不知道可能是什么 System.Data.SqlClient.SqlException:“无效列名” 这是我的代码,我创建了两个类,Gateway、Dept和Form1: namespace insertar { class Dept { public string Dept_No { get; set; } public string DNombre { get; s

我试图从VisualStudio中插入到数据库表中,但遇到了相同的错误,我不知道可能是什么

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



插入值导致的错误是字符串,因此需要使用
来包含值

但还有一个大问题

我建议您使用参数而不是连接的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语句的目的是当控件到达使用结束时,它将处理使用块的对象并释放内存。它的目的不仅仅是自动关闭连接,基本上它将处理连接对象,显然,连接也因此关闭

根据:

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

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


因此可以减少代码
connection.Close()使用
将有助于您做到这一点。

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

但还有一个大问题

我建议您使用参数而不是连接的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语句的目的是当控件到达使用结束时,它将处理使用块的对象并释放内存。它的目的不仅仅是自动关闭连接,基本上它将处理连接对象,显然,连接也因此关闭

根据:

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

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


因此可以减少代码
connection.Close()
因为
使用
将帮助您做到这一点。

为什么您不将SQL参数化?SQL注入与您的朋友相去甚远。您是否确实检查了数据库以确保列存在?传递的字符串没有引号,导致服务器将其解释为列名。阅读如何使用
SqlParameter
。为什么不将SQL参数化?SQL注入与您的朋友相去甚远。您是否确实检查了数据库以确保列存在?传递的字符串没有引号,导致服务器将其解释为列名。阅读如何使用
SqlParameter
。添加没有类型和大小的参数是一个坏主意,因为它可能导致错误的类型或截断。这就是
AddWithValues
被否决的原因谢谢你的提醒我将答案编辑为
Add
with size。你可以使用语句“堆叠”,这样缩进就少了,代码更容易阅读(IMO)。如果您不同意,可以随意更改(回滚编辑)。我也是一个尽可能晚(在执行之前)打开连接的粉丝。在这种情况下,差异应该为0,但如果计算了参数,并且该计算是CPU密集型的或“较慢”的,无论出于何种原因,它都会导致DB连接打开的时间稍短。添加没有类型和大小的参数是一个坏主意,因为这可能会导致错误的类型或截断。这就是
AddWithValues
被否决的原因谢谢你的提醒我将答案编辑为
Add
with size。你可以使用语句“堆叠”,这样缩进就少了,代码更容易阅读(IMO)。如果您不同意,可以随意更改(回滚编辑)。我也是一个尽可能晚(在执行之前)打开连接的粉丝。在这种情况下,差异应该为0,但如果计算了参数,并且计算是CPU密集型的或“较慢”,无论出于何种原因,它都会导致DB连接打开的时间稍微短一些。
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();
}