How to install and configure a database with .NET?

I am currently working on a project that involves using SQLServer. I would like to know what strategy should I use when installing the database creation software? I need to set up tables, stored procedures, and users.

Does my software need to do a startup check to find out if a database exists and then, if not, create it? Is there a way to automate this when installing SQL Server?

Thanks.

EDIT Well now I have a lot of nice solution, but I'm really looking for a solution (free or open source would be great) that would allow me to deploy a new application that requires SQLServer to be just installed and configured for the needs of the software.

+4
source share
6 answers

You can use the migration infrastructure, for example Migrator.Net , then you can start the migration every time your application starts. It’s good that you can update your database by releasing a new version of your software.

Go to the Getting Started page. This may clarify the concept.

I have successfully used this approach to solve the problem you are facing.

+1
source

RedGate offers an SQL Packager that allows you to create a script / .exe to deploy an entire db, including all (stored procedures, tables, etc.) from a single .exe. If you can afford it and want to have an easy way to do it (without having to do it yourself), this is the way to go; -)

  • Easy deployment of database updates to the client database.
  • Script and gently and quickly compress your schema and data.
  • Package any existing SQL script as .exe or run as a C # project
  • Simplify deployment and upgrades for SQL Server 2000, 2005, and 2008
+3
source

You do all this with SQL scripts. And then your installer runs them against the SQL Server client.

+1
source

You can write a T-SQL script that creates objects only when they do not exist. For the database:

if db_id('dbname') is null create database dbname 

For a stored procedure (see MSDN for a list of object types):

 if object_id('spname', 'P') is null exec ('create procedure dbo.spname as select 1') go alter procedure dbo.spname as <procedure definition> 

The good thing about such scenarios is that running them several times does not pose a problem - all objects already exist, so the script will not do anything a second time.

+1
source

Server setup is quite simple if you use MS SQL Server. As for creating a database and tables, you usually do this only once. The whole point of the database is that the data is persistent, so if there is a chance that the database will not exist, you either have serious stability problems, or b) there is no need for a real database.

Designing a database, tables, and procedures is an entire component of the software development process. When I do this, I usually keep all of my creation scripts in the original control. Once created, you will write the program in such a way that it assumes that the database already exists. Checking the connection is one thing, but a program should never think that there is no database at all.

0
source

you can make a script from all the objects that exist in your db. after that you can run this script from your code.

when you create your db script using the script wizard on the sql server, in the "select script options" section, set "Enable if not exist" to "Yes". with this work, only if db does not exist.

0
source

Source: https://habr.com/ru/post/1301920/


All Articles