Create Database Instance from Model - wpf

*Seems like there is some confusion. I created a SQL Server Compact Edition file and can see it from the Server Explorer. I can also right click and add tables manually. What I want to do is run the generated sqlce file to add all of the tables and columns from my model to the SDF.
-- background --
In Visual Studio 2012 (Ultimate), I designed a model using the model designer. It created an edmx file. I right clicked the model and chose "Generate Database from Model..." and created an sqlce file. My understanding is that I should be able to execute this file on an sdf somehow to create a SQL Server Compact Edition Instance of my database. I don't see the option on right click to execute the sql code, and the other option is to "Run SQL Scripts in Solution Explorer" which doesn't seem to make sense.
http://msdn.microsoft.com/en-us/library/yea4bc1b.aspx It says to drag the sqlce file to a database reference, but I'm not really sure what they mean. I have tried to drag it to the server explorer where the sdf is connected.
I tried right clicking on the sdf in the Server Explorer to do New SQL Query and pasting the sqlce in, but it seems that Create Table isn't supported.
Any ideas?

Generate Database from Model... only generates tables and relationships.
You need to have a database created already, and have it in the Database References folder. Then you drag your script file to that database reference, as described in your mentioned link.

Related

SQL Server in App_Data MVC

I just created a SQL Server Database in my App_Date by right clicking -> Add new Item -> SQL Server (Not compact).
I have created a model with the necessary data.
How do I go about sending data to that database? I don't mind using any method i.e. Linq/EF etc.
Best you open the sql server compact sdf file by double clicking it. Here you will be able to modify the database tables / columns etc.
Next add a new project item. Select Entity Framework database and go through the guided wizard.

How can I generate a script of my database as it is?

My primary reason for this is to keep track of database schema changes for my application. In SQL Server Management Studio I am able to generate a create script which creates the database but it doesn't contain any test data. Ideally when the script is run it should DROP the existing database (assuming it already exists) and then recreating it using this new script containing schema changes and test data from my development machine.
So how can I generate a script that will create a database with all the tables, stored procs, triggers, views, test data, etc?
I've tried using the import/export functionality but that's no good because it doesn't seem to copy over stored procedures. Plus it would be nice to have a script so I can track changes to the schema using mercurial.
I am using SQL Server Express 2008 R2 along with SQL Server Management Studio.
You didn't mention which version of SQL Server, but in SQL 2008 this is very easy
SQL 2008
Expand Databases
Right Click Database
Choose Tasks > Generate Scripts
Generate and
Publish Dialog will open Choose your
objects (i.e. Tables, procs, etc)
Click Next On the Set Scripting
Options choose Advanced Options Under
General choose SCRIPT DROP AND
CREATE - SCRIPT DROP AND CREATE
Types of Data To Script - Schema and
Data Close Advanced Window Choose to
save to file.
I wrote an open source command line utility named SchemaZen that does this. It's much faster than scripting from management studio and it's output is more version control friendly. It supports scripting both schema and data.
To generate scripts run:
schemazen.exe script --server localhost --database db --scriptDir c:\somedir
Then to recreate the database from scripts run:
schemazen.exe create --server localhost --database db --scriptDir c:\somedir
Try Microsoft SQL Server Database Publishing Wizard. This is a powerful flexible tool for scripting schema / data rom SQL Server.
Personally I use Microsoft Visual Studio 2010 Database Project with a Source Control Repository (SVN) to track changes to the schema.
Watch out for difference in database collation. If you develop on a database with a case insensitive collation and try and run the SSMS generated scripts against as database with a case sensitive collation then errors in case will break the scripts.
Usually i make backups fom a database before start a new development on it.
the best way is restore the backup when needed, i don't know how to get it by the script way!

SQL dump of users tables Gmed emr

I am working for a company that uses Gmed EMR. Gmed lacks some features that my client requires and I have had to build an external secure web based application to fill in. Gmed runs on Microsoft SQL server. I have access to the server, but I've never used Microsoft SQL server before. How can I safety SQL dump the users tables? Should I even try this?
To get a dump, do the following (OTTOMH):
Open SQL Server Management Studio
Right click on your database
On the menu go to TASKS and then GENERATE SCRIPTS then click NEXT
Choose "Specific Objects"
Choose the tables that you want
In the options make sure you choose the option for SCRIPT DATA
Choose to put it into a script or a window and click FINISH
This will give you a script to create the table, as well as the INSERT statements for data.
If you want just the data dump, you can right click a database and choose to EXPORT data.
It depends on the format you want them in. It's pretty easy to export to a text file. Open SQL Server Management Studio (the GUI), right-click the database you want to export from, then click Export Data. It's a straightforward wizard that lets you select what tables or other objects you want to export, and in what format or to what file.

SQL Server Mangement Studio Alter Script (2005)

Is it possible to generated ALTER sql script from the changes you perform in Server Studio. This is possible in different tools for mysql, basically you get the log of executed statements. Is similar thing possible here, or how do you go about finding out what the modification (ALTER) script is?
Thanks.
-- MB
You will be able to auto generate change scripts in SSMS. Take a look at this article, I believe it might be what you are looking for. http://www.mssqltips.com/tip.asp?tip=1723
from the article
As a part of my best practices, I always save the T-SQL scripts used for creation and modification of objects in SQL Server. When creating and modifying tables using SQL Server Management Studio designer it is easy to right click in the designer and select "Generate Change Script...", but is there a way to automatically script the creation and/or modification of tables made through of SQL Server Management Studio (SSMS) designer? This tip shows you an option that exists within SSMS to automatically generate these scripts for all table changes when using the table designer.
If you're altering a table (by right-clicking and selecting Design, then there is a button named Generate Change Script on the Table Designer toolbar.
If you're talking about changes made in a properties window, there is a script button at the top of each of those that can generate scripts for changes you make through the UI.
Not sure about SQL Server 2005, but in SQL Server Management Studio 2008 and later, you can definitely do this.
When you modify a table in the table designer (right-click on a table in your Object Explorer and pick "Design" from the menu), you can have SSMS generate the ALTER script for you by right-clicking in the designer and choosing "Generate Change Script":

Copy table to a different database on a different SQL Server

I would like to copy a table from one database to another. I know you can easily do the following if the databases are on the same SQL Server.
SELECT * INTO NewTable FROM existingdb.dbo.existingtable;
Is there any easy way to do this if the databases are on two different SQL Servers, without having to loop through every record in the original table and insert it into the new table?
Also, this needs to be done in code, outside of SQL Server Management Studio.
Yes. add a linked server entry, and use select into using the four part db object naming convention.
Example:
SELECT * INTO targetTable
FROM [sourceserver].[sourcedatabase].[dbo].[sourceTable]
If it’s only copying tables then linked servers will work fine or creating scripts but if secondary table already contains some data then I’d suggest using some third party comparison tool.
I’m using Apex Diff but there are also a lot of other tools out there such as those from Red Gate or Dev Art...
Third party tools are not necessary of course and you can do everything natively it’s just more convenient. Even if you’re on a tight budget you can use these in trial mode to get things done….
Here is a good thread on similar topic with a lot more examples on how to do this in pure sql.
SQL Server(2012) provides another way to generate script for the SQL Server databases with its objects and data. This script can be used to copy the tables’ schema and data from the source database to the destination one in our case.
Using the SQL Server Management Studio, right-click on the source database from the object explorer, then from Tasks choose Generate Scripts.
In the Choose objects window, choose Select Specific Database Objects to specify the tables that you will generate script for, then choose the tables by ticking beside each one of it. Click Next.
In the Set Scripting Options window, specify the path where you will save the generated script file, and click Advanced.
From the appeared Advanced Scripting Options window, specify Schema and Data as Types of Data to Script. You can decide from here if you want to script the indexes and keys in your tables. Click OK.
Getting back to the Advanced Scripting Options window, click Next.
Review the Summary window and click Next.
You can monitor the progress from the Save or Publish Scripts window. If there is no error click Finish and you will find the script file in the specified path.
SQL Scripting method is useful to generate one single script for the tables’ schema and data, including the indexes and keys. But again this method doesn’t generate the tables’ creation script in the correct order if there are relations between the tables.
Microsoft SQL Server Database Publishing Wizard will generate all the necessary insert statements, and optionally schema information as well if you need that:
http://www.microsoft.com/downloads/details.aspx?familyid=56E5B1C5-BF17-42E0-A410-371A838E570A
Generate the scripts?
Generate a script to create the table then generate a script to insert the data.
check-out SP_ Genereate_Inserts for generating the data insert script.
Create the database, with Script Database as... CREATE To
Within SSMS on the source server, use the export wizard with the destination server database as the destination.
Source instance > YourDatabase > Tasks > Export data
Data Soure = SQL Server Native Client
Validate/enter Server & Database
Destination = SQL Server Native Client
Validate/enter Server & Database
Follow through wizard

Resources