C# 备份sql服务器文件到本地 我用C#写了个备份的程序,可以用来备份sql数据库,可是在备份远程(局域网)sql数据库的时候,备份文件是备份在服务器上的,怎么让他备份到本地呢?或者将服务器上的备份文件copy到本地?我是菜鸟,,请高手指教!!谢谢!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 参考一下吧,好像只能备份到服务器上再copy到本地。http://topic.csdn.net/t/20031013/08/2348216.html private void BackUpSQLServer() { string str = strPath + "\\ " + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString(); string strConnectstring = "Server= " + this.textBox2.Text.Trim() + ";Database=Master;User ID= " + this.textBox4.Text.Trim() + ";Password= " + textBox4.Text.Trim() + "; "; SqlConnection conn = new SqlConnection(strConnectstring); SqlCommand cmdBK = new SqlCommand(); cmdBK.CommandType = CommandType.Text; cmdBK.Connection = conn; cmdBK.CommandText = @ "backup database " + textBox3.Text.Trim() + " to disk= ' " + str + " ' with init "; try { conn.Open(); cmdBK.ExecuteNonQuery(); this.richTextBox1.Text = "备份成功\n文件路径为: " + str + "\n操作时间:\n " + DateTime.Now.ToString(); this.richTextBox1.ForeColor = Color.Blue; } catch (Exception ex) { this.richTextBox1.Text = "备份失败\n失败原因\n " + ex.ToString() + "\n操作时间\n " + DateTime.Now.ToString(); this.richTextBox1.ForeColor = Color.Red; } finally { conn.Close(); conn.Dispose(); } } private void RestoreSQLServer() { string strConnectstring = "Server= " + this.textBox2.Text.Trim() + ";Database=Master;User ID= " + this.textBox4.Text.Trim() + ";Password= " + textBox5.Text.Trim() + "; "; SqlConnection conn = new SqlConnection(strConnectstring); conn.Open(); //KILL DataBase Process SqlCommand cmd = new SqlCommand( "SELECT spid FROM dbo.sysprocesses ,dbo.sysdatabases WHERE dbo.sysprocesses.dbid=dbo.sysdatabases.dbid AND dbo.sysdatabases.Name= ' " + textBox3.Text.Trim() + " ' ", conn); SqlDataReader dr; dr = cmd.ExecuteReader(); ArrayList list = new ArrayList(); while (dr.Read()) { list.Add(dr.GetInt16(0)); } dr.Close(); for (int i = 0; i < list.Count; i++) { cmd = new SqlCommand(string.Format( "KILL {0} ", list[i]), conn); cmd.ExecuteNonQuery(); } SqlCommand cmdRT = new SqlCommand(); cmdRT.CommandType = CommandType.Text; cmdRT.Connection = conn; cmdRT.CommandText = @ "restore database " + textBox3.Text.Trim() + " from disk= ' " + strPath + " ' "; try { cmdRT.ExecuteNonQuery(); this.richTextBox1.Text = "还原成功\n文件路径为: " + strPath + "\n操作时间:\n " + DateTime.Now.ToString(); this.richTextBox1.ForeColor = Color.Blue; } catch (Exception ex) { this.richTextBox1.Text = "还原失败\n失败原因\n " + ex.ToString() + "\n操作时间:\n " + DateTime.Now.ToString(); this.richTextBox1.ForeColor = Color.Red; } finally { conn.Close(); } } 给你个备份数据库的代码,参考下把:private void button2_Click(object sender, EventArgs e) { try { string strg = Application.StartupPath.ToString(); strg = strg.Substring(0, strg.LastIndexOf("\\")); strg = strg.Substring(0, strg.LastIndexOf("\\")); strg += @"\Data"; string sqltxt = @"BACKUP DATABASE db_MrCy TO Disk='" + strg + "\\" + txtpath.Text +".bak"+ "'"; SqlConnection conn = BaseClass.DBConn.CyCon(); conn.Open(); SqlCommand cmd = new SqlCommand(sqltxt, conn); cmd.ExecuteNonQuery(); conn.Close(); if (MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK) { this.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString()); } } 哦!我已经备份好了就是想知道怎么样把sql服务器上的备份文件copy到本地....................... winform下假进度条 datetimepicker 控件 问题 请问高手,如何从word文档中获取数据? socket客户在vista运行出错 高手们,来看看啊。 c#能否调用规则MFC动态库啊 为.net IDE开发addin插件的问题,请教各位大侠 可以在DateSet中使用t_sql语言吗?(来者有分) 动态把数据保存到数据库 c# activex 控件如何监控页面刷新和关闭啊 System.IO.FileNotFoundException: 未能加载文件或 多层架构的疑问
http://topic.csdn.net/t/20031013/08/2348216.html
private void BackUpSQLServer()
{
string str = strPath + "\\ " + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
string strConnectstring = "Server= " + this.textBox2.Text.Trim() + ";Database=Master;User ID= " + this.textBox4.Text.Trim() + ";Password= " + textBox4.Text.Trim() + "; ";
SqlConnection conn = new SqlConnection(strConnectstring); SqlCommand cmdBK = new SqlCommand();
cmdBK.CommandType = CommandType.Text;
cmdBK.Connection = conn;
cmdBK.CommandText = @ "backup database " + textBox3.Text.Trim() + " to disk= ' " + str + " ' with init "; try
{
conn.Open();
cmdBK.ExecuteNonQuery();
this.richTextBox1.Text = "备份成功\n文件路径为: " + str + "\n操作时间:\n " + DateTime.Now.ToString(); this.richTextBox1.ForeColor = Color.Blue;
}
catch (Exception ex)
{
this.richTextBox1.Text = "备份失败\n失败原因\n " + ex.ToString() + "\n操作时间\n " + DateTime.Now.ToString();
this.richTextBox1.ForeColor = Color.Red;
}
finally
{
conn.Close();
conn.Dispose();
}
} private void RestoreSQLServer()
{
string strConnectstring = "Server= " + this.textBox2.Text.Trim() + ";Database=Master;User ID= " + this.textBox4.Text.Trim() + ";Password= " + textBox5.Text.Trim() + "; ";
SqlConnection conn = new SqlConnection(strConnectstring);
conn.Open(); //KILL DataBase Process
SqlCommand cmd = new SqlCommand( "SELECT spid FROM dbo.sysprocesses ,dbo.sysdatabases WHERE dbo.sysprocesses.dbid=dbo.sysdatabases.dbid AND dbo.sysdatabases.Name= ' " + textBox3.Text.Trim() + " ' ", conn);
SqlDataReader dr;
dr = cmd.ExecuteReader();
ArrayList list = new ArrayList();
while (dr.Read())
{
list.Add(dr.GetInt16(0));
}
dr.Close();
for (int i = 0; i < list.Count; i++)
{
cmd = new SqlCommand(string.Format( "KILL {0} ", list[i]), conn);
cmd.ExecuteNonQuery();
} SqlCommand cmdRT = new SqlCommand();
cmdRT.CommandType = CommandType.Text;
cmdRT.Connection = conn;
cmdRT.CommandText = @ "restore database " + textBox3.Text.Trim() + " from disk= ' " + strPath + " ' "; try
{
cmdRT.ExecuteNonQuery();
this.richTextBox1.Text = "还原成功\n文件路径为: " + strPath + "\n操作时间:\n " + DateTime.Now.ToString();
this.richTextBox1.ForeColor = Color.Blue;
}
catch (Exception ex)
{
this.richTextBox1.Text = "还原失败\n失败原因\n " + ex.ToString() + "\n操作时间:\n " + DateTime.Now.ToString();
this.richTextBox1.ForeColor = Color.Red;
}
finally
{
conn.Close();
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string strg = Application.StartupPath.ToString();
strg = strg.Substring(0, strg.LastIndexOf("\\"));
strg = strg.Substring(0, strg.LastIndexOf("\\"));
strg += @"\Data";
string sqltxt = @"BACKUP DATABASE db_MrCy TO Disk='" + strg + "\\" + txtpath.Text +".bak"+ "'";
SqlConnection conn = BaseClass.DBConn.CyCon();
conn.Open();
SqlCommand cmd = new SqlCommand(sqltxt, conn);
cmd.ExecuteNonQuery();
conn.Close();
if (MessageBox.Show("备份成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation) == DialogResult.OK)
{
this.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString());
} }