SQL连接编译错误

时间:2014-04-24 12:36:20

标签: c#

我刚买了一台新电脑,并且正在努力记住如何根据需要重新设置所有内容,但这是史诗般的失败。我想在下面运行这段简单的代码,但是已经被编译错误所困扰。有人可以如此友善地指出我需要做些什么来消除所有这些编译错误?

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'的定义,并且没有扩展方法'Open'可以找到接受类型'SQL.SQLConnection'的第一个参数(你是否缺少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'的定义,并且没有扩展方法'Close'接受类型'SQL.SQLConnection'的第一个参数可以找到(你是否缺少using指令或汇编引用?)

3 个答案:

答案 0 :(得分:3)

如果您阅读第5个错误,您会看到。您正在使用

 using SQL.SQLConnection;

但你应该使用

 using System.Data.Sqlclient.Sqlconnection;

检查命名空间标题或指定它。我可以从错误中读到这个。此外,你错过了一个支架。见selman22回答。

答案 1 :(得分:1)

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. 的括号
  5. 请勿在{{1​​}} using
  6. 之后加分号

答案 2 :(得分:1)

因为您使用的是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,但这取决于您