Results 1 to 2 of 2

Thread: How to create and handle a database with SQL Express using code first

  1. #1

    Thread Starter
    Join Date
    Jul 2018

    How to create and handle a database with SQL Express using code first

    Hi all,

    In the program I'm developing I need to use a database to store data. I really don't need a complicated one, only 3 tables in a first moment:

    -Configuration data

    The details I think I've to menction are:

    -Once installed the program in a new machine I need to be able to (2 options)

    1.- Create the new database locally (if I could even install automatically SQL Express if not present, much better). Once created I would populate it taking the data from an excel (I'll do it with NPOI)

    2.- Connect to the database that is contained in other computer on the LAN that acts as server (the idea is the usual/typical scenario: we can have only one computer holding the database + program or more than one computer, where everyone has the program but shared database holded in the computer who plays the server role).

    Not to say that on the interface I'll make the user select "this computer is the server" or "this computers connect to other computer who is the server". Depending on the user action create database locally or browse thorought LAN to find SQL Express instance on the server and connect to it.

    Since I want to use a database that I can use by free I think SQL Express is my best choice, and since I prefer doing everything over code I think I'll prefer "code first" instead of database first.

    Can anyone suggest me some tutorial that explain how to do this in the clearest possible way? I found this one thanks to jmcilhinney, but it seems to use "database first" instead of "code first".


    Many thanks in advance.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002

    Re: How to create and handle a database with SQL Express using code first

    I like the approach, as I'll be doing the same kind of thing. I don't have a tutorial, but a few suggestions. For one thing, with SQL Server Express, you don't necessarily have to do that "browse through the LAN" bit. When SQL Server Express is installed, by default, it acts as a server, so it will be discoverable across the LAN already. As long as you know the name of the database to look for, there is a method (I have it in VB, but it would look pretty similar in C#) you can use to see all instances of SQL Server visible to the computer. One out on the LAN would be visible by default, unless set up to not be visible. You can then check whether the instance has the DB in question, or not. So, you could look at all instances of SQL Server (it won't make a distinction between Express and other), find the ones that have the database, and either offer the user a choice, or choose for them.

    After that it becomes a deployment question, not a coding question. You'd end up deciding during installation whether or not SQL Server Express ought to be installed on that particular computer. Frankly, that doesn't scare me nearly as much as the thought of users trying to figure out the right answer to that question. You can give them the option....can you get them to make the right choice?
    My usual boring signature: Nothing

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