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
db.Create();
db.ExecuteNonQuery(script);

Note:

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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s