使用c#.net备份数据库 [英] backup database using c#.net
本文介绍了使用c#.net备份数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在按钮clik上创建数据库的后面这里是我的代码,它创建了一个目录,如果找不到但是如果没有创建该特定数据库的备份
i want to create back of the database on button clik here is my code,it create a directory if it not found but if there is not create the backup of that perticular database
using System.Data;
using System.Data.SqlClient;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
//Metioned here your database name
string dbname = "test1";
SqlConnection sqlcon=new SqlConnection();
SqlCommand sqlcmd = new SqlCommand();
SqlDataAdapter da = new SqlDataAdapter();
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
//Mentioned Connection string make sure that user id and password sufficient previlages
sqlcon.ConnectionString = @"Server=MOHANDB\SQLEXPRESS;database=" + dbname + ";uid=ravindran;pwd=srirangam;";
//Enter destination directory where backup file stored
string destdir = "D:\\backupdb";
//Check that directory already there otherwise create
if (!System.IO.Directory.Exists(destdir))
{
System.IO.Directory.CreateDirectory("D:\\backupdb");
}
try
{
//Open connection
sqlcon.Open();
//query to take backup database
sqlcmd = new SqlCommand("backup database test to disk='" + destdir + "\\" + DateTime.Now.ToString("ddMMyyyy_HHmmss") + ".Bak'", sqlcon);
sqlcmd.ExecuteNonQuery();
//Close connection
sqlcon.Close();
Response.Write("Backup database successfully");
}
catch (Exception ex)
{
Response.Write("Error During backup database!");
}
}
}
推荐答案
你也可以在click_event中激活一个查询来创建一个在数据库中备份。
从SourceTable_Name中选择*到UrBackUpTable_Name
u can also fire a query in click_event to create a backup in database.
Select * into UrBackUpTable_Name from SourceTable_Name
这篇关于使用c#.net备份数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文