How to connect a WinForms App to a database using a ADO.NET Entity data Model

Michael Tadyshak 80 Reputation points
2024-02-13T22:23:38.1366667+00:00

Working with a tutorial from YouTube titled "C# Tutorial - Getting Started with Model View View-Model (MVVM) | FoxLearn" I was able to create an empty database and run a downloaded script from Microsoft on Github and the "Northwind sample database" is seen populated with data in SSMS.

Next a WinForms application is created in Visual Studio with C# and it shows adding an "ADO.NET Entity Data Model". When it asks for a data connection the tutorial enters "foxlearn.Northwind.dbo" as the data connection.

On my machine I have identified the following:

User Name: LAPTOP-CAUSE-OF\miket

Server Name: localhost\SQLEXPRESS

Server: LAPTOP-CAUSE-OF-OUR-JOY\SQLEXPRESS

Instance Name: SQLEXPRESS

Database File: C:\Program Files\Microsoft SQL Server\MSSQL16.SQLEXPRESS\MSSQL\DATA\Northwind.mdf

If I do "Add Server" and choose "Data Source=Microsoft SQL Server Database File", "Data Provider=.NET Framework Data Provider for SQL Server" I get an error "The connection properties object must be of type "SqlFileConnectionProperties" How would I connect my Winforms App to this database?

SQL Server | Other
Developer technologies | C#
0 comments No comments
{count} votes

3 answers

Sort by: Most helpful
  1. Q&A Assist
    2024-02-13T22:23:38.14+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To connect a WinForms App to a database using an ADO.NET Entity Data Model, you can follow these steps:

    1. In Visual Studio, create a new C# WinForms App (.NET Framework) project.
    2. Install the Entity Framework 6 NuGet package by selecting the project node in Solution Explorer, then choosing Project > Manage NuGet Packages. In the NuGet Package Manager, click on the Browse link, search for "EntityFramework", and click Install in the right pane.
    3. Add a new connection to the Northwind database in Server Explorer by right-clicking on Data Connections and selecting Add Connection. Choose Microsoft SQL Server as the data source, and enter the server name and database file path for your Northwind database. If you're using SQL Server Express, the server name may be in the format "localhost\SQLEXPRESS". Once you've successfully configured the connection information, the database will be added as a node underneath the Data Connections node.
    4. In the Solution Explorer, right-click on the project node and choose Add > New Item. Select ADO.NET Entity Data Model, enter "NorthwindModel" as the name, and click Add.
    5. In the Entity Data Model Wizard, select "Generate from database" and click Next. Choose the Northwind database connection you added in step 3, and enter "NorthwindEntities" as the name of the connection string. Click Next, then select the tables you want to include in your model and click Finish.
    6. You can now use the NorthwindEntities object to query and manipulate data in your WinForms application.

    References:

    0 comments No comments

  2. Anonymous
    2024-02-14T08:38:16.33+00:00

    Hi @Michael Tadyshak , Welcome to Microsoft Q&A,

    Have you downloaded the sql driver correctly?

    Please open the installer to check whether Data storage and processing has been downloaded. To ensure that sql can be used normally. User's image

    Best Regards,

    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

    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

  3. Karen Payne MVP 35,586 Reputation points Volunteer Moderator
    2024-02-14T13:25:19.21+00:00

    I would recommend using Entity Framework Core. Use EF Power Tools (a Visual Studio extension and free) to reverse engineer the database. See my sample project on GitHub, all you need to do is create the database and populate as per instructions in the readme file. The connection string is in the project file appsettings.json. Note: My NorthWind database is modified from the original NorthWind database and named NorthWin2024.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.