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

Bernard Bailey 21 Reputation points
2021-03-10T01:35:32.807+00:00

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.

C#
C#
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,177 questions
{count} votes

Accepted answer
  1. Karen Payne MVP 35,026 Reputation points
    2021-03-10T04:31:15.01+00:00

    Hello,

    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.

    76142-f1.png


3 additional answers

Sort by: Most helpful
  1. Daniel Zhang-MSFT 9,611 Reputation points
    2021-03-10T03:01:46.617+00:00

    Hi BernardBailey-0080,
    When you create a new project, you need to choose Windows Forms App(.NET).
    76094-310.png
    Then right click you project name in solution explore->Properties->In the Application page, change your target framework to .NET 5.0.
    76121-3101.png
    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);  
        cnn.Open();  
        DataSet ds = new DataSet();  
        SqlDataAdapter adapter = new SqlDataAdapter("SELECT Name from test", cnn);  
        adapter.Fill(ds);  
       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,211 Reputation points
    2021-03-10T06:35:48.497+00:00

    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.

    https://www.c-sharpcorner.com/UploadFile/18fc30/understanding-the-basics-of-ado-net/

    https://dotnettutorials.net/lesson/ado-net-using-sql-server/


  3. SM2428 1 Reputation point
    2022-07-15T16:09:14.36+00:00

    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.