找不到 C# SqlConnection

Pea*_*ace 6 c# sql database sql-server .net-core

我正在尝试使用以下代码连接到我网络中的 SQL 数据库。我在 Stackoverflow 和 web 上搜索了这个错误,发现有很多其他人遇到了这个问题,应用并测试了很多都没有解决我的问题。目前我的代码如下:

using System;
using System.Data.SqlClient;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            using ( SqlConnection conn = new SqlConnection)
            {
                conn.ConnectionString = "Server=***;Database=***;Trusted_Connection=True;";
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

在 *** 的位置,我有服务器和数据库名称。另外,我在哪里输入特定的表格?

Dav*_*oft 13

我所做的只是创建一个新的控制台应用程序并输入此代码以连接到数据库。

所以我可以推断你正在使用 .NET Core,所以你需要添加 NuGet 包:System.Data.SqlClient

您的 .csproj 文件应如下所示:

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="System.Data.SqlClient" Version="4.5.1" />
  </ItemGroup>

</Project>
Run Code Online (Sandbox Code Playgroud)