Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i create my project with sql server database in the main form menustripeitemtools open and new i want in user click new create new empty copy of database with its tables and views
for my project how to do this

What I have tried:

I create my windows form application by c# with sql server db Iwount to create anew copy from database on clik in menustripitemtools  named new how to do this
Posted
Updated 1-Jan-19 7:44am
Comments
[no name] 1-Jan-19 12:50pm    
Have you tried it just using SQL? Or do you want us to code the SQL too?

(The C# "program" simply "sends" the SQL commands).

One way to create a fresh copy of a database is to restore it from an initial backup copy. In other words

In Management Studio
- Create a new, empty database database
- Create the objects you want a new database to contain
- Create a full backup of the database, for more info, see Create a Full Database Backup (SQL Server) - SQL Server | Microsoft Docs[^]

Now you have an initial backup which you can use in your project
- Include the backup files in your project
- When needed use the RESTORE command to restore a new database from your backup, see RESTORE (Transact-SQL) - SQL Server | Microsoft Docs[^]

The approach above requires that you have administrative privileges to create a new database (obviously), but also the computer running the SQL server needs to have access to the files that are going to be restored. So you may need to copy the files to a proper location prior using them.

Another, quite similar approach would be to script the database instead of restoring a backup. In SSMS you can easily script the database, see Tutorial: Script objects in SQL Server Management Studio - SQL Server | Microsoft Docs[^]. Once you have the script you can execute the commands from the script as already described.

A short comment about this: Usually creating a database is not an operation normal users do. When the database is created one needs to know thing like
- The devices where the database files are going to be located
- What privileges should be granted to the database in context of the whole database server
- What kind of configuration the database server has, is it sufficient for the database created
- Any modifications needed for the server backup plans
etc.

So before creating new databases for the users, one should think if a single, shared database would be sufficient. Just pointing this out since this aspect wasn't touched in the question.
 
Share this answer
 
There are several ways to do it, you can do it programmatically like in this example:
https://www.c-sharpcorner.com/article/create-a-sql-server-database-dynamically-in-C-Sharp/[^]

You can also generate a script in SQL Server Management Studio and run that from your application, but beware of the "GO" statements which will not work with the SqlCommand.ExecuteNonQuery command: How to execute an .SQL script file using c# - Stack Overflow[^]
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900