c# - 无法找到 System.Data.SqlClient 引用

标签 c# sqlclient

我安装了全新的 Visual Studio 2017 Professional。我正在使用 .NET 4.7.1 构建一个快速 POC 控制台应用程序,但无法找到 System.Data.SqlClient 的引用。

我检查了我的系统,找到了 4 个版本的 System.Data.SqlClient.dll,但没有一个是正确的,也无法编译。 我还尝试使用 System.Data,但其中没有对 SqlClient 的引用。我已经手动添加了System.Data的dll/引用,但也没有解决引用问题。

我的应用程序目前非常简单,并且由于缺少引用而无法编译。

我需要采取哪些步骤才能解决此问题?

using System;
using System.Data;
using System.Data.SqlClient;

namespace ConsoleApp1
{
    class Database
    {
        public void Start()
        {

            string connString = @"server=(local);initial     catalog=MyDatabase;Integrated Security=SSPI;";
            using (SqlConnection conn = new SqlConnection(connString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("SELECT TOP 10 ID, Name FROM TableA", conn))
                {
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        while(reader.Read())
                        {
                            Console.WriteLine("ID: [{0}], Name: [{1}]", reader.GetValue(0), reader.GetValue(1));
                        }
                    }
                }
            }
        }
    }
}

最佳答案

dotnet添加包System.Data.SqlClient

关于c# - 无法找到 System.Data.SqlClient 引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49035178/

相关文章:

c# - Where 子句中条件的执行顺序

c# - 将 ASP.NET MVC 5 应用程序部署到 Win Server 2008

c# - DependencyResolver 不解析依赖关系。为什么?

c# - 如何在 Windows 8 上修复 .NET Framework 4.0?

c# - 在 PowerShell 中迭代多行数据

c# - “不要公开通用列表”,为什么在方法参数中使用 collection<T> 而不是 list<T>

c# - 是否可以在 ASP.NET MVC 项目中利用 ICommand?

c# - 在循环内添加SQL参数,在循环外执行。 (在 C# SQL 客户端中使用参数进行单查询多插入)

c# - 异常: 'system.data.sqlclient.sqlconnection' 的类型初始值设定项?

c# - SqlDataAdapter、SqlCommand 从字符串中删除前导数字