我知道在ASP.NET的C#语言下,能够通过编写程序代码备份和还原数据库,不知道怎么创建数据库?谢谢知道的能给与帮助。
备份数据库内的核心代码如下:
{.......
string path = pathTextBox.Text;//需要备份到的路径
string dbname = dbDropDownList.SelectedValue;//备份数据库的名称
string backupSql = "use master;backup database @dbname to disk = @path;";//需要执行的代码
SqlCommand myCommand = new SqlCommand(backupSql, new SqlConnection(ConnStr));
myCommand.Parameters.Add("@dbname", SqlDbType.Char);
myCommand.Parameters["@dbname"].Value = dbname;
myCommand.Parameters.Add("@path", SqlDbType.Char);
myCommand.Parameters["@path"].Value = path;
try
{
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
infoLabel.Text = "备份成功";
}
catch(Exception ex)
{
infoLabel.Text = "备份失败<br>" + ex.ToString();
}
finally
{
myCommand.Connection.Close();
}
}
数据库还原的核心代码:
{.................
string path = pathTextBox.Text;//需要还原文件的路径
string dbname = dbDropDownList.SelectedValue;//还原数据库名称
string restoreSql = "use master;restore database @dbname from disk = @path;";
SqlCommand myCommand = new SqlCommand(restoreSql, new SqlConnection(ConnStr));
myCommand.Parameters.Add("@dbname", SqlDbType.Char);
myCommand.Parameters["@dbname"].Value = dbname;
myCommand.Parameters.Add("@path", SqlDbType.Char);
myCommand.Parameters["@path"].Value = path;
try
{
myCommand.Connection.Open();
myCommand.ExecuteNonQuery();
infoLabel.Text = "恢复成功";
}
catch(Exception ex)
{
infoLabel.Text = "恢复失败<br>" + ex.ToString();
}
finally
{
myCommand.Connection.Close();
}
}