Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Note: I'm used to SQL and Visual Studio but new to C#.
Microsoft Doc for Net Core 3.1 shows this example:
"
C#
IDbConnection connection;
connection = new System.Data.SqlClient.SqlConnection
"
But in VS 2022 SqlConnection is not available as option under System.Data.SqlClient
What am I missing?

What I have tried:

I copied the code from MS Doc on IDbConnection Interface without success.
Posted
Updated 14-Dec-22 22:03pm
v2
Comments
PIEBALDconsult 14-Dec-22 13:58pm    
You will need to use your fingers to do some typing.

This has nothing to do with Visual Studio (2022 or any other). Classes, interfaces etc are defined in the headers that you include via using statements. So check the documentation for the feature you want to use, and ensure they are included in the Project References and using statement(s).
 
Share this answer
 
Comments
Rebecka Thermia 14-Dec-22 12:53pm    
using System; and using System.Data; as MS doc shows. This is not the problem
Richard Deeming 15-Dec-22 3:56am    
"... ensure they are included in the Project References ..."

Perhaps you missed that part of Richard's answer? Since your own solution suggests that this is precisely the problem.
Rebecka Thermia 15-Dec-22 4:08am    
I will give you that, but as I sayed in my Question I'm novice in C#. Reading thru the MS doc didn't mention that package as I could see. I found the answer in video by Tim Corey.
Richard MacCutchan 15-Dec-22 4:12am    
Well that's yet another reason why you should spend time studying the language and its requirements before you start writing code.
Rebecka Thermia 15-Dec-22 4:26am    
I don't fully agree to that. I have coded a lot in several languages , just not i C#. In my experience the quickest way to learn is to start coding.
The problem is solved. It was missing a dependency package. Thanks to you who tried to help me.
 
Share this answer
 
Back in .NET Framework, the System.Data.SqlClient namespace was included in the System.Data assembly. But for .NET Core and .NET 5/6/7/etc. that is no longer the case. For those platforms, or for class libraries targeting .NET Standard, you need to reference a NuGet package to use this namespace.

Since Microsoft only seems to be happy when developers are confused, there are now two ways to reference the SqlClient namespace in .NET:
They're largely similar, but the Microsoft.Data.SqlClient package is newer, and is recommended for any new projects:
Introducing the new Microsoft.Data.SqlClient - .NET Blog[^]
 
Share this answer
 
Comments
Rebecka Thermia 15-Dec-22 4:18am    
Thank you for this information, that explains a lot.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900