What is the best way to create databases on C# application, I mean by a click of a button, it will create a database with a different name but the same model?
Currently, I script TSQL onto a C# application, so when I click a button, a new database will be created with the name I defined, it works well, however, it is extremely difficult to maintain, is there a better way to create database on C#?
Regards
You can use SMO to create databases. See MSDN sample.
One way is to just have a copy of your database file (mdf) available for pushing onto a new server. This similiar to what microsoft does with it's Model database. Whenever you create a new database it starts by making a copy of that one.
Your button click could copy the database file to the desired location, rename it as appropriate, then attach it to the running sql server instance. Whenever you need to update the database with a new schema, just copy the mdf to your deployment directory.
Sql Server 2008 Management Studio can generate SQL script for database generation. Save script in text file and later spool it to RDBMS and create database.
This is not too hard to maintain.
To generate script in SqlServer 2008 Management Studio: right click on database -> Tasks -> Create Scripts, click few times and save to file.
Related
I want to create clone of database programatically. My requirement is, I have a default master template of database, I want to create a new database when my application will execute and this new database will be copy of existing master database, just its name will be changed.
One option is to create a backup of the master database and restore it under a different name using a script:
RESTORE DATABASE [CopyOfMaster] FROM
DISK = N'C:\Temp\Master.bak'
WITH FILE = 1,
MOVE N'Master' TO N'C:\Temp\copy.mdf',
MOVE N'Master_log' TO N'C:\Temp\copy_log.ldf',
NOUNLOAD
GO
Explanation of parameters:
DISK - path and file of your backup file
MOVE - you need one line for each file group in the original database. You specify new file name and path for each file group
More reading on MSDN
You need to script your master template database and save the script to a file. Use SQL Server Management Studio, right click on your master template database and select 'script database as'. You will have to change the database name in the script to some identifier which you can perform a string replace to change the name at runtime.
In your code you can load the script file and execute to create your database with a different name. You can make use of the SqlCommand to execute the loaded script (http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.aspx)
You can also make use of the command line 'sqlcmd' utility or SQL Server Management Object(SMO). Although I have not explored any of those options.
I am using SQL server 2008 R2 and VS2010. I made simple application by using this tools. I attach database as .mdf in my application and deploy that application on other machine its works fine. Now if I plan for new release of my app which some extended features, I can upload Code by DLL, But problem is updating .mdf file, to handle this I am exporting database into .xls sheets (Application have one utility to backup database) and then import into SQL Server to create new .mdf file. Someone have better solution on this? Can I open old version of .mdf file in SQL Server(Third party software) and Execute DML/DDL script on it to make latest code and database compatible ? May I keep .sql file in one of my project code and execute it by some utility..? Any Class in C# which can handle this..?
I did not get your query completely. Do you want to upgrade the DB through the application?
You can of course run .sql files through your application, but I'm not sure it would help you change the Database configuration.
Alternatively, if you already have the updated .mdf file and the database name is same, then you can follow the following steps.
1. Detach the database by SSMS in the third party environment through SSMS.
2. Replace the .mdf, .ldf and .ndf (if any) in the disk.
3. Attach the updated .mdf file.
This will get the new Object definitions as well as data.
As far as I'm aware, there is no process for merging .mdf files, because the SQL Server might not be able to identify the similar objects properly as sys tables may be different, and also would not know which data to keep in the final data base, in case the table structure, constraints or data conflicts occur.
However, looking at your requirement, the best way I can suggest is,
1. Generate the Alter scripts for the tables modified (By right clicking on the object name and using Script Table As.. option). Of course, I assume you have the list of objects modified and the modifications.
2. Connect the two DB servers over network and write an SSIS package or Import data from the old DB to the new one for the tables you want.
Hope this helps.
I have a file that contains a SQL Server 2012 database backup. I am using this file with a C# WinForms application, that creates a database, restores the backup into it, and then performs some more functions.
I don't want to just add this backup file to my application, because this is a security issue. I was thinking I should add the backup as an embedded resource, and then save it to a temp file, restore from it and delete it.
Is there a better way to do this? Is there a way to access an embedded resource directly through a file path that I can pass to SQL server to use in the RESTORE DATABASE command?
SQL Server has no idea whatsoever about how to restore a database from something living inside an executable. Given that you state that you've considered it be able to be an embedded resource, I am going to assume that it's not a "last known good" backup, but more a "default" backup.
If I am correct in this thinking, then you're basically saying you have a default set-up. If that is the case, you don't need a backup at all, your code just needs to be able to create a database and populate it with certain things to "become" that default state. By embedding a backup as a resource, you are (in my mind) stating that the structure and data is well known, so why go down the RESTORE DATABASE path at all?
For example I have developed c# application (with sql database) and installed on win OS, then use to save some data. Now I need to install new window, what did happen with my saved data?
Generally the data you are referring to might be either created during the installation or when the program itself is used (like an OLTP applicaiton). If lets say you have followed all the best practices and created the application using the created installation package, then the uninstaller should give you the option to either store your preferences like template and layout and such(if your program supprts those kind of things). But as for the data and even more in your case data stored in SQL is not deleted UNLESS you specifically mention in the uninstallation to drop the table or database. So as you are mentioning new window, just ensure the database exists and all the data is in contact(most likely it will be). Then also ensure that the new window is pointing to the data with all the necessary constrings and authentication.
I have a SQL Server database, i need to export all of the data into an Access mdb that users can download. What's the simplest way of doing this from C#?
I realise I could have a blank (but with schema in place) mdb, and when i want to export I could copy it, then read all the data from SQL Server into the mdb via datasets, but that seems like a right faff. Is there an easier way?
Thanks
Could you create a DTS Package to do the export, then write a C# app to execute the DTS package, the user then just needs to execute your C# app? You could also set the DTS package to run automatically if you don't want user interaction.