C#数据库备份与还原问题
我的代码:privatevoid数据备份ToolStripMenuItem1_Click(objectsender,EventArgse){stringconnectio...
我的代码:
private void 数据备份ToolStripMenuItem1_Click(object sender, EventArgs e)
{
string connectionString = Properties.Settings.Default.mydbConnectionString;
SqlConnection sqlConn = new SqlConnection(connectionString);
string strSQL = @"backup database mydb to disk='c:\mydb2.bak'";
SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn);
if (MessageBox.Show("文件将保存在C盘目录下", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
try
{
sqlConn.Open();
sqlCmd.ExecuteNonQuery();
MessageBox.Show("备份成功!");
}
catch
{
sqlCmd.Dispose();
sqlConn.Close();
MessageBox.Show("备份失败!");
}
}
}
private void 数据库还原ToolStripMenuItem_Click(object sender, EventArgs e)
{
string connectionString = Properties.Settings.Default.mydbConnectionString;
SqlConnection sqlConn = new SqlConnection(connectionString);
string strSQL = @"restore database mydb from disk='c:\mydb2.bak'";
SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn);
if (MessageBox.Show("数据库将回到上次备份的状态", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
try
{
sqlConn.Open();
sqlCmd.ExecuteNonQuery();
MessageBox.Show("还原成功!");
}
catch
{
sqlCmd.Dispose();
sqlConn.Close();
MessageBox.Show("还原失败!");
}
}
}
哪位大师知道问题出在哪里呀?急救~~~
先谢谢两位的回答哈~~~我运行的时候都能通过,只是不能成功备份和还原~我非常之郁闷呢~~~ 展开
private void 数据备份ToolStripMenuItem1_Click(object sender, EventArgs e)
{
string connectionString = Properties.Settings.Default.mydbConnectionString;
SqlConnection sqlConn = new SqlConnection(connectionString);
string strSQL = @"backup database mydb to disk='c:\mydb2.bak'";
SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn);
if (MessageBox.Show("文件将保存在C盘目录下", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
try
{
sqlConn.Open();
sqlCmd.ExecuteNonQuery();
MessageBox.Show("备份成功!");
}
catch
{
sqlCmd.Dispose();
sqlConn.Close();
MessageBox.Show("备份失败!");
}
}
}
private void 数据库还原ToolStripMenuItem_Click(object sender, EventArgs e)
{
string connectionString = Properties.Settings.Default.mydbConnectionString;
SqlConnection sqlConn = new SqlConnection(connectionString);
string strSQL = @"restore database mydb from disk='c:\mydb2.bak'";
SqlCommand sqlCmd = new SqlCommand(strSQL, sqlConn);
if (MessageBox.Show("数据库将回到上次备份的状态", "提示", MessageBoxButtons.YesNo,
MessageBoxIcon.Information, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
{
try
{
sqlConn.Open();
sqlCmd.ExecuteNonQuery();
MessageBox.Show("还原成功!");
}
catch
{
sqlCmd.Dispose();
sqlConn.Close();
MessageBox.Show("还原失败!");
}
}
}
哪位大师知道问题出在哪里呀?急救~~~
先谢谢两位的回答哈~~~我运行的时候都能通过,只是不能成功备份和还原~我非常之郁闷呢~~~ 展开
展开全部
在C#的某事件中执行sql备份语句
完整备份
Backup Database NorthwindCS
To disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
--差异备份
Backup Database NorthwindCS
To disk=’G:\Backup\NorthwindCS_Diff_20070908.bak’
With Differential
--日志备份,默认截断日志
Backup Log NorthwindCS
To disk=’G:\Backup\NorthwindCS_Log_20070908.bak’
--日志备份,不截断日志
Backup Log NorthwindCS
To disk=’G:\Backup\NorthwindCS_Log_20070908.bak’
With No_Truncate
--截断日志不保留
Backup Log NorthwindCS
With No_Log
--或者
Backup Log NorthwindCS
With Truncate_Only
--截断之后日志文件不会变小
--有必要可以进行收缩
--文件备份
Exec Sp_Helpdb NorthwindCS --查看数据文件
Backup Database NorthwindCS
File=’NorthwindCS’ --数据文件的逻辑名
To disk=’G:\Backup\NorthwindCS_File_20070908.bak’
--备份设置密码保护备份
Backup Database NorthwindCS
To disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
With Password = ’123’,init
Restore Database NorthwindCS
From disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
With Password = ’123’
private void button1_Click(object sender, EventArgs e)
{
try
{
string backupFolder = System.IO.Path.Combine(Application.StartupPath, "data");
if (!System.IO.Directory.Exists(backupFolder))//判断文件夹是否存在
{
//创建文件夹
System.IO.Directory.CreateDirectory(backupFolder);
}
string backupFileName = System.IO.Path.Combine(backupFolder, DateTime.Today.ToString("yyyyMMdd.bak"));
string sqltxt = string.Format("BACKUP DATABASE [FOOT] TO DISK=''", backupFileName);
SqlConnection conn = baseclass.DBConn.cycon();
conn.Open();
SqlCommand cmd = new SqlCommand(sqltxt, conn);
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Close();
}
希望对你有帮助
完整备份
Backup Database NorthwindCS
To disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
--差异备份
Backup Database NorthwindCS
To disk=’G:\Backup\NorthwindCS_Diff_20070908.bak’
With Differential
--日志备份,默认截断日志
Backup Log NorthwindCS
To disk=’G:\Backup\NorthwindCS_Log_20070908.bak’
--日志备份,不截断日志
Backup Log NorthwindCS
To disk=’G:\Backup\NorthwindCS_Log_20070908.bak’
With No_Truncate
--截断日志不保留
Backup Log NorthwindCS
With No_Log
--或者
Backup Log NorthwindCS
With Truncate_Only
--截断之后日志文件不会变小
--有必要可以进行收缩
--文件备份
Exec Sp_Helpdb NorthwindCS --查看数据文件
Backup Database NorthwindCS
File=’NorthwindCS’ --数据文件的逻辑名
To disk=’G:\Backup\NorthwindCS_File_20070908.bak’
--备份设置密码保护备份
Backup Database NorthwindCS
To disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
With Password = ’123’,init
Restore Database NorthwindCS
From disk=’G:\Backup\NorthwindCS_Full_20070908.bak’
With Password = ’123’
private void button1_Click(object sender, EventArgs e)
{
try
{
string backupFolder = System.IO.Path.Combine(Application.StartupPath, "data");
if (!System.IO.Directory.Exists(backupFolder))//判断文件夹是否存在
{
//创建文件夹
System.IO.Directory.CreateDirectory(backupFolder);
}
string backupFileName = System.IO.Path.Combine(backupFolder, DateTime.Today.ToString("yyyyMMdd.bak"));
string sqltxt = string.Format("BACKUP DATABASE [FOOT] TO DISK=''", backupFileName);
SqlConnection conn = baseclass.DBConn.cycon();
conn.Open();
SqlCommand cmd = new SqlCommand(sqltxt, conn);
cmd.ExecuteNonQuery();
conn.Close();
MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
this.Close();
}
希望对你有帮助
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
你好,建议楼主将调试时,出现的问题描述一下,比如异常信息等。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
这个数据库是Sql server 吗??
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询