Results 1 to 5 of 5

Thread: Deploying Desktop App With SQL Server

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2024
    Posts
    5

    Deploying Desktop App With SQL Server

    Hello;

    I have a windows desktop app that uses a SQL Server database. The application is for one user who will have the database on their PC. I have the app working fine using SQL installed locally with the following code on my development PC:

    'Attach to the database
    MyCn.ConnectionString = "Server=localhost\SQLEXPRESS;Database=GBC;Trusted_Connection=True;"

    I am struggling as to how to get it deployed. The Installer extensions I can find in VS 2022 don't seem to give me a way to deploy the database with the app. Can you point me in the direction I need to go? Do I just need to install SQL Express on the user's PC?

    Many thanks for reading.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,913

    Re: Deploying Desktop App With SQL Server

    Thread moved to Application Deployment, as this is not a VB.NET issue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,913

    Re: Deploying Desktop App With SQL Server

    There is no inbuilt way to deploy a database the way you are doing it. There's two ways to work with SQL Server databases:

    1. The database is permanently attached to a SQL Server instance and you connect by specifying the instance and database names.
    2. The data file is part of your application and the database is attached at run time, then detached when the app quits. You connect by specifying the instance and the file path.

    If you use option 1, there's no inbuilt way to deploy that database with your app. You basically have to execute the appropriate SQL to create the database, build the schema and insert and initial data. That SQL can be executed manually by the person doing the installation, or it could be executed by the application on the first run, or many installers will allow you to add steps that can perform arbitrary tasks like this. Whether you can make use of that last option and how you do it depends on the installer you're using. If you're using the Microsoft extension in VS for Setup projects, you can add a custom action or the like to execute the appropriate SQL code.

    If you use option 2, all you need to do is include the data file in your project. It should be configured appropriately by default to de deployed with your app like any other file. As long as the user has SQL Server installed on their local machine, the connection string can be used to attach that file automatically at run time. In that case, you would use a connection string with the AttachDbFilename attribute and use "|DataDirectory|" for the folder path.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2024
    Posts
    5

    Re: Deploying Desktop App With SQL Server

    Quote Originally Posted by jmcilhinney View Post
    There is no inbuilt way to deploy a database the way you are doing it. There's two ways to work with SQL Server databases:

    1. The database is permanently attached to a SQL Server instance and you connect by specifying the instance and database names.
    2. The data file is part of your application and the database is attached at run time, then detached when the app quits. You connect by specifying the instance and the file path.

    If you use option 1, there's no inbuilt way to deploy that database with your app. You basically have to execute the appropriate SQL to create the database, build the schema and insert and initial data. That SQL can be executed manually by the person doing the installation, or it could be executed by the application on the first run, or many installers will allow you to add steps that can perform arbitrary tasks like this. Whether you can make use of that last option and how you do it depends on the installer you're using. If you're using the Microsoft extension in VS for Setup projects, you can add a custom action or the like to execute the appropriate SQL code.

    If you use option 2, all you need to do is include the data file in your project. It should be configured appropriately by default to de deployed with your app like any other file. As long as the user has SQL Server installed on their local machine, the connection string can be used to attach that file automatically at run time. In that case, you would use a connection string with the AttachDbFilename attribute and use "|DataDirectory|" for the folder path.
    Thank you very much for the explanation. My apologies for posting in the incorrect location.

  5. #5
    New Member
    Join Date
    Dec 2024
    Posts
    12

    Re: Deploying Desktop App With SQL Server

    The best approach is to have the user install SQL Server Express on their machine and then deploy your application along with the database files. This way, the application can connect to the local SQL Server instance. Also, use a tool like SQL Server Management Studio (SSMS) to generate a SQL script that contains the schema and data for your database. You can then include this SQL script as part of your application installer, so when the user runs the installer, it also installs the database.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width