SQL连接编译错误

本文关键字:错误 编译 连接 SQL | 更新日期: 2023-09-27 17:59:56

我刚买了一台新电脑,正试图记住如何根据需要设置一切,但我失败了。我想在下面运行这段简单的代码,但遇到了编译错误。有人能指出我需要做些什么来消除所有这些编译错误吗?

using (SQLConnnection conn = new SQLConnection(connectionStringSQL)
{
  conn.Open();
  Using (SqlCommand command = new SqlCommand("SELECT * FROM Table1");
  conn.Close();
}

编译错误列表----

1) "SQL.SQLConnection":using语句中使用的类型必须隐式转换为"System.IDisposable"
2) "SQL.SQLConnection"不包含接受1个参数的构造函数
3) "SQL.SQLConnection"不包含"Open"的定义,也找不到接受第一个"SQL.SQL Connection"类型参数的扩展方法"Open"(是否缺少using指令或程序集引用?)4) 与"System.Data.SqlClient.SqlCommand.SqlCommand(string,System.Data.SqlClient.SqlConnection)"匹配的最佳重载方法包含一些无效参数
5) 无法从"SQL.SQLConnection"转换为"System.Data.SqlClient.SQLConnection"6) 名称"command"在当前上下文中不存在7) "SQL.SQLConnection"不包含"Close"的定义,也找不到接受"SQL.SQL Connection"类型的第一个参数的扩展方法"Close)(是否缺少using指令或程序集引用?)

SQL连接编译错误

如果你清楚地阅读了第五个错误,你会看到的。您正在使用

 using SQL.SQLConnection;

但是你应该使用

 using System.Data.Sqlclient.Sqlconnection;

检查您的命名空间标头或指定它。我可以从其他错误中读取此信息。此外,你还缺了一个括号。参见selman22答案。

using (SqlConnection conn = new SqlConnection(connectionStringSQL))
{
     conn.Open();
     using (SqlCommand command = new SqlCommand("SELECT * FROM Table1"))
     {
     }
     conn.Close();
}
  1. SQLConnnection->SqlConnection
  2. Using->using
  3. 不要忘记为using加上括号
  4. 不要在using using(...); <----后面加分号

由于您使用using,您不需要关闭连接,它会关闭,此外,由于您不执行查询:

using (SqlConnection conn = new SqlConnection(connectionStringSQL))
{
     conn.Open();
     using (SqlCommand command = new SqlCommand("SELECT * FROM Table1"))
     {
                    cmd1 = conn.CreateCommand();
                    cmd1.CommandType = CommandType.Text;
                    cmd1.CommandText = command;
                    cmd1.ExecuteNonQuery();
     }
}

尽管您可能想执行DataReader,但这取决于