.NET 5 Database Connection. How do you connect using .NET5?

Bernard Bailey 21 Reputation points

I have looked everywhere to find a way to connect a database in a .NET5 Windows Form Application. I can do it using a Windows Frameworks WIndows Forms Application but I am trying to figure out how to do this with the .NET5. Is there any way to do this or is a feature that is being worked on. I am learning C# right now and would like to be able to use .Net5 to build application. Thanks, Bernard A. Bailey, Jr.

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,602 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,376 Reputation points


    See my code sample on GitHub which should set you up for interacting with SQL-Server. The interface has just enough to show it works so some columns for identifiers are showing which is actually good for learning purposes.


3 additional answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,621 Reputation points

    Hi BernardBailey-0080,
    When you create a new project, you need to choose Windows Forms App(.NET).
    Then right click you project name in solution explore->Properties->In the Application page, change your target framework to .NET 5.0.
    And I made a test via sql server, it worked fine.
    Here is a code example you can refer to.

    using System.Data.SqlClient;  
    private void button1_Click(object sender, EventArgs e)  
        SqlConnection cnn;  
        string connStr ="ConnectString"  
        cnn = new SqlConnection(connStr);  
        DataSet ds = new DataSet();  
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT Name from test", cnn);  
       this.listBox1.DataSource = ds.Tables[0];  
       this.listBox1.DisplayMember = "Name";  

    Best Regards,
    Daniel Zhang

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Duane Arnold 3,216 Reputation points

    ADO.NET is ADO.NET. It basically makes no difference what version of .NET you use, if you know the basic fundlementals of how to use ADO.NET.



  3. SM2428 1 Reputation point

    Hi BernardBailey-0080,

    I came across this site when searching for help with a similar issue of not being able to create an SQL connection in a vb.net .NET5 Windows Desktop Application.

    My solution was to add a NuGet package called "System.Data.SqlClient" by Microsoft. You can open the NuGet Package manager by going to Tools > NuGet Package Manager... > Manage NuGet Packages for Solution...

    Hopefully, this will help you or others looking for a solution.