How to create a SQL Server database on production server - sql-server

I'm working on an Angular / .Net core project, and I want to deploy it on my homemade server equipped with Windows Server 2016.
I'm stuck at figuring out the best way to create my database from my migrations files. I've seen some topics on stackoverflow about it, but I don't want to make mistakes on my server, so I'm asking for the best way to do that.
The first option I think is to install Visual Studio IDE on my server. Then I can open my project and run an Update-database command, which will read all migrations files and create database.
But I don't think I'm supposed to install Visual Studio on a server...
The second option is something I saw on StackOverflow. In my development environment, I can use the command dotnet ef migrations script, which will generate a script.
But I'm not sure where I should put this script. I think probably in Microsoft SQL Server Management studio, New query and put my script.
Could you please tell me the best way ?

you need dotnet sdk in order to use dotnet ef migrations script but you can use your development pc to connect to your remote SQL Server and do migrations. for this, just point your connection string to remote SQL Server and use either Update-Database or dotnet ef update database

Another solution exists and You can use SQL server Generate Script to query all your database objects and execute it on target SQL server.

The best way you can do "first create" of your database in production environment is to Generate Script from SSMS (SQL Server Management Studio) and run that script on production Db server. The reason of that is because your database is empty (you can put all of data in script) and you can easily change the name, etc.
After that, on new versions of your application, the best way is to use Update-Database -Script and save/add that script to your deployment scripts.
When you are preparing for go-live, try that scripts with ROLLBACK TRANSACTION, if something fails...

Related

LocalDB: create db from existing SQL Server database

Just wondering if there's a quick way of creating a new localdb from an existing SQL Server database. The idea is to reproduce the "real" db structure in order to run some integration tests from the VSTS during a CI build.
Currently, I'm using the Generate Scripts option to get the SQL for creating the required db objects, but unfortunately it needs some cleaning before it can be run against a new localdb.
Is there an alternative approach for this kind of work?
Thanks,
Luis

How to use SQL Server Database Project

I am running SQL Server 2012 and VS 2010 with SSDT (SQL Server Data Tools) installed. My dev DB uses stored procs, functions, CLR objects, etc. It has a snapshot of prod data of about 500GB.
I created SQL Server Database Project and then imported the database. This created all tables, views, procs and functions files under schema names. Great stuff -- now I can do a version control just like in other VS projects, create deployments, etc. So far, so good.
But, I am confused as to what my development process should be for changing/adding procs/tables under SQL Server Database Project. It appears that any changes I make are applied to some LocalDb/Projects database and NOT to my dev database.
Am I suppose to author all my objects in that LocalDb, then Build and deploy to my dev database via Publish? I am worried about my existing tables in the dev DB since if the publish process drops and recreates tables, I will loose my prod data snapshot.
What is the right development process to follow in SQL Server Database Project?
Think of the source database (in your case, your database project) as being the "to be" state after deployment. When a deployment is initiated, the executable (SqlPackage.exe) compares the source with the target and generates a difference/delta script to make the target look like the source. This is why we no longer have to specify CREATE or ALTER; the tool figures it out. To answer your question about ongoing development, you can develop either way. You can develop in the project files and publish them to a common Dev database (say, if you're on a team), or you can develop in the database with tools like SQL Server Management Studio (SSMS) and synchronize with the project files with a schema compare (I use the latter technique because I like SSMS).
For deployment, you'll have to have SSDT installed on the machine from which you execute the deployment (SSDT ships with SQL Server 2012 and later; I don't know about SQL Server 2008). You can create scripts to simplify deployment. You'll essentially call SqlPackage.exe (it lives in x:\Program Files (x86)\Microsoft SQL Server\nnn\DAC\bin) with an action and a source. I use Publish Profiles as well to take care of most command properties. So an example deployment might look like this:
SqlPackage.exe /Action:Publish /SourceFile:MyDatabase.dacpac /Profile:MyProfile.publish.xml
For more information:
SQL Server Data Tools Documentation
http://msdn.microsoft.com/en-us/library/hh272686(v=vs.103).aspx
SqlPackage.exe Documentation
http://msdn.microsoft.com/en-us/library/hh550080(v=vs.103).aspx
Make changes inside the VS DB project.
Deploy changes to localDB to test
Publish the database to your production server. I prefer to use Schema Compare to do this manually, but you can also publish the project via the right click --> publish menu (which will also create a publishing profile), or using command line arguments. The publish process won't drop and create tables (unless you tell it to drop & recreate the entire db).
Alternatively, in the project settings you can change the connection string to point to your production server (as pointed out in the comment). However, I recommend against this, as it will then attempt to publish to the production server every time you run a local build (F5).

How to create a autorun/.exe file of database

I'm developing a CMS and I'm using SQL Server with it. I want to automatically create my database in SQL Server 2008 when my app is installed.
I can generate a script. I'm wondering that there might be a way that I can install SQL script in a database using an MSI file.
e.g. in this link we can install database using a MSI file
http://www.microsoft.com/download/en/details.aspx?id=23654
I just wanna know how can we do it. Is it created using SQL Server or Visual Studio
you can attach a database through the app.config. in your installation you should copy this database to the right place.
Look here how to set your connection string:
http://www.connectionstrings.com/sql-server-2008

Deploy SQL Server Schema Changes using SQLCMD

I'm using Visual Studio Team System 2008's Database tools to develop my databases. On my local dev machine, when I want to deploy schema changes to the SQL Server instance on my machine, I just use the Data --> Schema Compare feature of VS2008.
But with live databases I can't do this because I can't connect to the database directly from my machine and the server haven't got VS2008 installed.
So I was thinking about the SQLCMD tool. Isn't that what VS2008 uses "under the hood"?
I want to use as part of an automatic deployment strategy. I want to be able to publish SQL scripts generated by VS2008 to the server and have an application run scripts on the live database to update the schema.
UPDATE
I'm trying to achieve automatic change script generation by taking the deploy script VS2008 Database Edition generates and comparing it against a live database. Only I want to do it through code, no tool or anything. It must be able to run from a Windows Service on the server.
SqlCmd would work. If you also want automatic versioning support you should check out Tarantino.Net, a database management tool that keeps track of which sql-files have already been run.

Using SSIS to build a database from a VS2005/2008 database project

How do I allow an SSIS package to consume a Visual Studio 2005 or 2008 Database project which houses the creation scripts for the tables and other objects (note: this is not Visual Studio Database Edition - just Pro with Business Intelligence)?
The idea is to use this to recreate my test instance by building the schema from source safe and the data from prod.
I have access to both VS2005 and 2008 Pro and this is hitting Sql Server 2005 for both Prod and Test.
The short answer is, you cannot "allow an ssis package to consume a VS 2005 or 2008 db project". I am not sure why you want to use SSIS to run the create scripts. You can run the scripts from sqlcmd utility.
If you still want to use SSIS, this is what I would do:
create a deployment script (VS for DB Pro can do that)
create a package
in your control flow, use Execute Process Task and configure it to run sqlcmd with the script file as your input
You can call the scripts in the DB project through Execute SQL tasks in the SSIS package. However, there's not a direct way to consume them.

Resources