【问题标题】:The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'在命名空间“System.Data.SqlClient”中找不到类型名称“SqlConnection”
【发布时间】:2020-11-28 15:22:37
【问题描述】:

我正在使用 VIM 文本编辑器在 Linux 中编写 C# 代码并遇到此问题。我已经读过在不同的文件中使用相同的命名空间显然很好,并确保拼写不是问题......它似乎也不是 OmniSharp 插件的问题。
在终端中我尝试了 dotnet add package System.Data.SqlClient,但它并没有停止产生警告:'Unnecessary using directive。'

使用系统; 使用 System.Data.SqlClient;

命名空间 myexamples {

class Drinks
{
string _connString = "Data Source=localhost;Initial Catalog=TestDB;";

public List<string> GetDrinks()
{
    List<string> drinksList = new List<string>();
    using (SqlConnection cnxn = new SqlConnection(_connString))
    {
    using (SqlCommand drinksCommand = new SqlCommand())
    {
        drinksCommand.Connection = cnxn;
        drinksCommand.CommandText = "SELECT name FROM Drinks";
        cnxn.Open();
        using (SqlDataReader drinksDataReader = drinksCommand.ExecuteReader())
        {
          while (drinksDataReader.Read() == true)
          {
             ...
          }
        }
    }
    }
}

} }

【问题讨论】:

标签: c# linux vim omnisharp


【解决方案1】:

您可以使用 sqlconnection 对象代替 sqlstringbuilder,它会正常工作

using(sqlconnection con=new sqlconnection)
{

enter code here}

【讨论】:

    猜你喜欢
    • 2015-06-26
    • 1970-01-01
    • 2013-03-25
    • 2017-07-12
    • 2011-05-13
    • 2021-03-18
    • 1970-01-01
    • 2023-02-22
    相关资源
    最近更新 更多