i want to create a new data base of existing database.. the name of database willbe given by textbox.

Dani AI

Generated

asked for a way to create a new database from an existing one with the new name supplied from a textbox. There are three practical options depending on the goal: (A) full copy (schema + data), (B) schema-only, or (C) object-by-object copy. For a reliable full copy on SQL Server the simplest approach is to BACKUP the source database and RESTORE it under the new name (RESTORE ... WITH MOVE). For schema-only copying, script generation (SSMS or the SMO scripter) is appropriate — which is the direction hinted at.

A compact, safe workflow that can be called from ASP.NET/C#:

  1. Run BACKUP DATABASE [Source] TO DISK = '...'; 2) use RESTORE FILELISTONLY to get logical file names from the backup; 3) build a RESTORE DATABASE [NewName] ... WITH MOVE 'logicalData' TO 'path\NewName.mdf', MOVE 'logicalLog' TO 'path\NewName_log.ldf', REPLACE; 4) execute that RESTORE. Example skeleton (adapt paths, permissions and error handling):
using System.Data.SqlClient;
// open connection to master on the SQL Server instance
// run BACKUP DATABASE [Source] TO DISK = @bak
// run RESTORE FILELISTONLY FROM DISK = @bak and read LogicalName rows
// build RESTORE DATABASE [NewName] ... WITH MOVE 'LogicalData' TO @mdf, MOVE 'LogicalLog' TO @ldf, REPLACE

Notes and common pitfalls: the backup/restore runs on the SQL Server host — the backup path must be accessible to the SQL Server service account (use a server-local path or a UNC share with proper rights). Logical file names from FILELISTONLY must be used to create correct MOVE clauses. RESTORE will duplicate database-level objects and data; server-level logins may require remapping on the target server. For very large databases or cloud-managed instances, consider provider-specific copy tools or SMO/Transfer if finer control is needed.

Recommended Answers

All 4 Replies

You want to duplicate the complete structure in code?

ya i want to make same database

IMO it would be best to generate an SQL script from your original database, and execute that (you can use SMO).

how i t can perform if you have have code plz post or mail me..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.