Create New DB by Executing Scripts C#

string sqlConnectionString = @”Integrated Security=True;Initial Catalog=master;Data Source=.”; //Reference Connection String
FileInfo File = new FileInfo(Server.MapPath(“/sample.sql”)); // .SQL path
string script = File.OpenText().ReadToEnd();
SqlConnection conn = new SqlConnection(sqlConnectionString);
Server server = new Server(new ServerConnection(conn));
var db = new Database(server, “sampledb”); // Mention DB Name here i’ve mentioned sampledb as my new DB Name


While generating .sql script in db.Follow these steps

DBname –> Tasks –> Generate Scripts –> select specific database objects (check Tables & SP’s) –> Next –>Advanced(Script Use Database = false) –> OK –>Next

it is mandatory to false Script Use Database if u want to create new DB with different name or else it is not required


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s