1. Add a connection string with Database name as blank
<connectionStrings>
<add name="constr" connectionString="Data Source=.\SQL2008R2;Initial Catalog=;User Id=sa;Password=pass@123"/>
</connectionStrings>
2. Add the namespaces
using System.Data;
using System.Configuration;
3. Add the code
protected void Page_Load(object sender, EventArgs e)
{
string databaseName = "TestDB";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = con;
cmd.CommandText = "sp_detach_db";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@dbName", databaseName);
cmd.Parameters.AddWithValue("@skipchecks", "true");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}