Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# SqlConnection could not be found

I am trying to connect to the SQL database in my network using the code below. I've searched on Stackoverflow and web for this error and found so many other people experiencing this issue, applied and tested many none fixed my issue. Currently the code I have is below:

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;";
            }
        }
    }
}

In the place of *** I have server and database names. Also, where do I enter the specific table?

like image 907
Peace Avatar asked Dec 03 '18 14:12

Peace


1 Answers

All I did was create a new console app and enter this code to connect to DB.

So I can infer that you're using .NET Core, and so you need to add the NuGet package: System.Data.SqlClient

Your .csproj file should look like:

<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>
like image 85
David Browne - Microsoft Avatar answered Oct 11 '22 18:10

David Browne - Microsoft