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?
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With