none
Include Database In Winform Project RRS feed

  • Question

  • I have a SQL Server database that houses parts information.  I want to attach that database to my C# winform app so that the database can be queried and have information displayed on the form to the end user.  I want the database to be with the app, so that no connection to the server itself is needed.

    How is this done?

    Sunday, April 14, 2019 5:15 PM

All replies

  • If you already have the program developed using the database on the database server, then where you need to post to is to the MSDN MS SQL Server forum.

    You can have them explain how to detach the database from the database engine  on the database server, how to use the detached MDF file with your program and how to deploy the detached MDF file with your program with the things needed from a database standpoint to use the database on the user's workstation with your program in a standalone/single user manner.

    https://social.msdn.microsoft.com/forums/sqlserver/en-us/home?forum=sqlexpress

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home

    Sunday, April 14, 2019 6:26 PM
  • I am still in start-up stages.  I am wanting to plan a course of action before I get to far in and have to start over for something small.

    Asking now from a C# side of things - is it possible to include a local copy of the database?

    Sunday, April 14, 2019 6:33 PM
  • There are two things to consider here: whether you want to include the database, and whether you also want to include the database engine.

    Including the database is easy. For example, if you know that the target computers are always going to have an instance of SQL Server Express installed, then you can just develop your code to use SQL Server and include the .mdf file that contains the database in your project. Then access it using a "user instance" in the connection string.

    However, including the engine is not as easy. Not many databases provide a way to include the engine in your projects, most of them will require to use an installer to deploy the engine before your program can use the database. This includes all versions of SQL Server, including Express and localdb, which require the user to first install the engine on their computers. One exception would be SQL CE, which can be deployed by just including the DLLs with your program binaries. However, note that SQL CE is not compatible with the full SQL Server, so it would require changes to your program. If you are going to do such changes, you may prefer to change instead to a SQLite database, which can also be deployed with your app by including a couple of DLLs.

    Sunday, April 14, 2019 7:30 PM
    Moderator
  • I am still in start-up stages.  I am wanting to plan a course of action before I get to far in and have to start over for something small.

    Asking now from a C# side of things - is it possible to include a local copy of the database?

    Using the ADO.NET MS SQL Server client driver in whatever type of .NET program is the same no matter if one is using a Windows desktop program or Web program in using C# or VB.NET along with using an ADO.NET connectionstring for the program.

    https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection?view=netframework-4.7.2

    Your issues are how do you setup to use a detached MDF file with your program, how to deploy the detached MDF file with your program and what MS SQL Server components must be installed on a workstation in order for a detached MDF file can be used by your program on the workstation in a standalone manner.

    The first thing is how to use Localdb, what is it and understand it

    https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-2016-express-localdb?view=sql-server-2017

    The second thing is how to use Visual Stuido's Service Based Database that is a detached MS SQL Server Express MDF file that is using Localdb out the box.

    https://www.youtube.com/watch?v=jz5vbkMhDIc

    After you learn how to do all of the above, then you need to go to the SQL Server Express forum on how to do the deployment to a workstation for the MDF database file and what needs to be on the workstation for your program to work with the MDF file, becuase C# is out of the picture at that point.

    There similar tutorial links like the one above that are working with a Windows form program, use Bing or Google to find them.

    Sunday, April 14, 2019 7:31 PM
  • I am still in start-up stages.  I am wanting to plan a course of action before I get to far in and have to start over for something small.

    Asking now from a C# side of things - is it possible to include a local copy of the database?

    Using the ADO.NET MS SQL Server client driver in whatever type of .NET program is the same no matter if one is using a Windows desktop program or Web program in using C# or VB.NET along with using an ADO.NET connectionstring for the program.

    https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlconnection?view=netframework-4.7.2

    Your issues are how do you setup to use a detached MDF file with your program, how to deploy the detached MDF file with your program and what MS SQL Server components must be installed on a workstation in order for a detached MDF file can be used by your program on the workstation in a standalone manner.

    The first thing is how to use Localdb, what is it and understand it

    https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-2016-express-localdb?view=sql-server-2017

    The second thing is how to use Visual Stuido's Service Based Database that is a detached MS SQL Server Express MDF file that is using Localdb out the box.

    https://www.youtube.com/watch?v=jz5vbkMhDIc

    After you learn how to do all of the above, then you need to go to the SQL Server Express forum on how to do the deployment to a workstation for the MDF database file and what needs to be on the workstation for your program to work with the MDF file, becuase C# is out of the picture at that point.

    There similar tutorial links like the one above that are working with a Windows form program, use Bing or Google to find them.

    Cool - this is the information I was looking for!!!

    One last question and I think I'm all set...I'm not clear on if there is a possibility to attach the .mdf file and query it w/o having to push a SQL Express or some sort of SQL instance to the users PC...

    Sunday, April 14, 2019 7:45 PM
  • One last question and I think I'm all set...I'm not clear on if there is a possibility to attach the .mdf file and query it w/o having to push a SQL Express or some sort of SQL instance to the users PC

    You're going to have to do something, and the place to ask is at the below forum.

    https://social.msdn.microsoft.com/forums/sqlserver/en-us/home?forum=sqlexpress

    Sunday, April 14, 2019 8:58 PM

  • Cool - this is the information I was looking for!!!

    One last question and I think I'm all set...I'm not clear on if there is a possibility to attach the .mdf file and query it w/o having to push a SQL Express or some sort of SQL instance to the users PC...

    For an attached database you need to have SQL-Server Express as per the official documentation.

    https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-2016-express-localdb?view=sql-server-2017


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, April 14, 2019 9:49 PM
    Moderator