Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is wrong with this code. Why can't I use SqlConnection?

Tags:

c#

sql

sqlclient

I am 100% newbie to SQl and wanted to make a ConsoleApp with the use of database. I read some about it and tried. When I needed to make SqlConnection, my VS 2019 Preview showed me this

Severity Code Description Project File Line Suppression State Error CS1069 The type name 'SqlConnection' could not be found in the namespace 'System.Data.SqlClient'. This type has been forwarded to assembly 'System.Data.SqlClient, Version=0.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Consider adding a reference to that assembly. ConsoleApp1 C:\Users\User\Desktop\Bald Code\ConsoleApp1\ConsoleApp1\Program.cs 12 Active

i don't get why it doesn't work

Here's my code

using System;

using System.Data;

using System.Data.SqlClient;

namespace ConsoleApp1

{

    class Program
    {
        static void Main(string[] args)
        {
            string connectionString;
            SqlConnection cnn;
        }
    }
}
like image 713
Redlik Avatar asked Feb 01 '19 02:02

Redlik


People also ask

How do I declare SqlConnection?

Creating a SqlConnection Object A SqlConnection is an object, just like any other C# object. Most of the time, you just declare and instantiate the SqlConnection all at the same time, as shown below: SqlConnection conn = new SqlConnection( "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI");

What does SqlConnection mean?

A SqlConnection object represents a unique session to a SQL Server data source. With a client/server database system, it is equivalent to a network connection to the server. SqlConnection is used together with SqlDataAdapter and SqlCommand to increase performance when connecting to a Microsoft SQL Server database.

When should you use the SqlConnection object?

If you want to access a database multiple times, you should establish a connection using the Connection object. You can also make a connection to a database by passing a connection string via a Command or Recordset object. However, this type of connection is only good for one specific, single query.

Do I need to call SqlConnection open?

The SqlConnection draws an open connection from the connection pool if one is available. Otherwise, it establishes a new connection to an instance of SQL Server. If the SqlConnection goes out of scope, it is not closed. Therefore, you must explicitly close the connection by calling Close.


3 Answers

If you just updated EntityFrameworkCore from version 2.x to 3.x and you're running into this, change your using statement to Microsoft.Data.SqlClient instead of System.Data.SqlClient.

If you're using EntityFrameworkCore.SqlServer it already has that as a dependency, so you shouldn't need to install it explicitly.

This Microsoft blog explains the change.

like image 51
Chad Hedgcock Avatar answered Oct 20 '22 00:10

Chad Hedgcock


Assuming that you're using .NET Core - just add NuGet package: System.Data.SqlClient

Your .csproj might look similar to:

<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 24
Jazb Avatar answered Oct 20 '22 01:10

Jazb


Most likely the System.Data.SqlClient.DLL in not in the Bin folder, and you have not set reference to the DLL in the project. You have missed the database choice when You install visual studio. And now You just manually add references named "System.Data.SqlClient".

1.right click you project name and choose nuget package options.

2.search "System.Data.SqlClient" and install it.

enter image description here

Hope this is fix Your Error

like image 21
ADH - THE TECHIE GUY Avatar answered Oct 20 '22 00:10

ADH - THE TECHIE GUY