如果数据库文件.mdf和.log不存在于带有应用程序的文件夹中,那么我需要创建新的数据库,但如果我喜欢这个
CREATE DATABASE Mydvds.mdf
我有例外,数据库Mydvds.mdf已经存在。如果我试图像这样删除数据库
DROP DATABASE Mydvds.mdf
我有例外,基地不存在或我没有预先制作。 我正在通过
检查数据库列表SELECT * FROM sysdatabases
此列表中的C:\ Mydvds.mdf。 我如何从服务器中删除此数据库? 或者你能告诉我另一种做法吗?
public void create_db()
{
string base_path = Environment.CurrentDirectory + @"..\..\..\My_cool_db.mdf";
string log_path = Environment.CurrentDirectory + @"..\..\..\My_cool_db_log.ldf";
string ConnectionString = @"Data Source=(LocalDB)\v11.0";
string cmnd_create_db;
connection = new SqlConnection(ConnectionString);
cmnd_create_db = "CREATE DATABASE My_cool_db ON PRIMARY " +
"(NAME = My_cool_db, " +
"FILENAME = '" + @base_path + "', " +
"SIZE = 4MB, MAXSIZE = 20MB, FILEGROWTH = 10%) " +
"LOG ON (NAME = My_cool_db_log, " +
"FILENAME = '" + @log_path + "', " +
"SIZE = 4MB, " +
"MAXSIZE = 5MB, " +
"FILEGROWTH = 10%)";
ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=" + @base_path + ";Integrated Security=True";
connection = new SqlConnection(ConnectionString);
cmnd = connection.CreateCommand();
cmnd.CommandText = cmnd_create_db;
connection.Open();
cmnd.ExecuteNonQuery();
List<string> db_cmnd = new List<string>();
db_cmnd.Add(File.ReadAllText(Environment.CurrentDirectory + "\\res\\Main.sql"));
db_cmnd.Add(File.ReadAllText(Environment.CurrentDirectory + "\\res\\Category_table.sql"));
db_cmnd.Add(File.ReadAllText(Environment.CurrentDirectory + "\\res\\ForeignKeyConstraint1.sql"));
foreach (string command_text in db_cmnd)
{
cmnd.CommandText = command_text;
cmnd.ExecuteNonQuery();
}
connection.Close();
connection.Dispose();
cmnd.Dispose();
}