-1

I have an ASP.Net web forms application connected to a SQL Server Database 'Db_1'.

Now, there is a button of which button click event, there will be some calculations, then create a new database 'Db_2' and store the results in the new database 'Db_2'.

How do I create the database 'Db_2'?

2
  • 1
    By executing the CREATE DATABASE command connected to master database. Commented Apr 19, 2020 at 16:47
  • Depending on what level of control you need SMO might be the prefered way. In any case there are already a lot of questions and answers that should cover most aspects. Commented Apr 19, 2020 at 17:07

1 Answer 1

0

You can execute the below ADO.NET code to create the database.

// Creating instance of SqlConnection  
SqlConnection conn = new SqlConnection(".........");  
conn.Open();// open the database connection  
SqlCommand cmd = new SqlCommand();// Creating instance of SqlCommand  
cmd.Connection = conn; // set the connection to instance of SqlCommand  
cmd.CommandText = "Create database Db_2"; // set  
//the sql command ( Statement )  
cmd.ExecuteNonQuery();  
conn.Close();// Close the connection

Once database is create you need to create an another steps to create the table and insert the data into that table using same ADO.NET process with different SQL Server command like create table() and insert into table values ().

For the steps involved in two and three, you need to create and use another instance of the newly created database.

Sign up to request clarification or add additional context in comments.

2 Comments

... and most importantly change the connection to use the new database first.
@Opengenic Although, thank you comment is not promoted by SO community. See, What should I do when someone answers my question?.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.