System.Data.SqlClient is missing in a .NET Core project

When you create a new project in .NET Core, and you try to use a class from ADO.NET, such as SqlConnection, you’ll get missing reference errors. For example, your code might look something like this, and it’ll have red-squiggly lines under SqlConnection: In the past, you’d simply add a reference to System.Data.SqlClient, like this: To … Read more

C# – How to use SqlBulkCopy to do a Bulk Insert

If you find yourself doing SQL Inserts in a loop, you may want to consider doing a Bulk Insert instead. You can use SqlBulkCopy to do a Bulk Insert from C#. Make sure to prove you have a performance problem before switching your code to use Bulk Insert. Like any optimization, it adds complexity. In … Read more

C# – How to handle nulls with SqlDataReader

SqlDataReader returns a DBNull object when a column is null. This isn’t the same as a C# null. You can check if the column is null by comparing it with DBNull.Value or by using SqlDataReader.IsDBNull(). Here’s an example showing these two ways of checking if a column is null: After checking if the column is … Read more

C# – Using SqlDataReader to process multiple result sets

In this article I’ll show how to use the SqlDataReader ADO.NET class in two scenarios involving multiple result sets: I have a StreamingService database that has Movies, Shows, and Episodes (linked to the shows). First I’ll show the model classes I’m mapping the data into. Then I’ll show the two scenarios where I’m using SqlDataReader … Read more