CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

请指教winform数据库备份和恢复的代码

楼主liujun808(Ludream)2005-02-04 19:53:12 在 .NET技术 / C# 提问

请问哪位高手知道winform数据库备份和恢复的代码,请指教!! 问题点数:20、回复次数:8Top

1 楼54duke(萝卜肉丁)回复于 2005-02-04 19:56:20 得分 0

什么数据库?  
  access    
  sql   server?Top

2 楼pantian(香草园主)回复于 2005-02-04 19:57:50 得分 0

唉,一看就是一个半路出道的小伙子。  
   
  你问个问题也说清楚一点呀。  
   
  在winform里,你要备份什么样的数据库,你想恢复什么样的数据库?不同的数据库所用的方法是有很大差异的。  
   
  再说,一般情况下,我们都是使用数据库的相关产品来备份、恢复的。而不是使用winform。Top

3 楼liujun808(Ludream)回复于 2005-02-04 20:07:01 得分 0

access数据库和sql数据库都给可以吗?Top

4 楼liujun808(Ludream)回复于 2005-02-04 20:08:57 得分 0

应为现在编码用到的是access数据库,可能以后要导入到sql数据库中进行管理,所以可能两个数据库都需要,请指教!Top

5 楼xiongchen(二氧化鬼)回复于 2005-02-04 20:11:40 得分 20

using   System;  
  using   System.Drawing;  
  using   System.Collections;  
  using   System.ComponentModel;  
  using   System.Windows.Forms;  
  using   System.Data;  
  using   System.Data.SqlClient;  
   
  namespace   BKAndRC  
  {  
  public   class   Form1   :   System.Windows.Forms.Form  
  {  
  private   System.Windows.Forms.TextBox   txtName;  
  private   System.Windows.Forms.TextBox   txtPwd;  
  private   System.Windows.Forms.TextBox   txtServer;  
  private   System.Windows.Forms.Label   label1;  
  private   System.Windows.Forms.Label   label2;  
  private   System.Windows.Forms.Label   label3;  
  private   System.Windows.Forms.Button   button1;  
  private   System.Windows.Forms.Label   label4;  
  private   System.Windows.Forms.ComboBox   comboBox1;  
  private   System.Windows.Forms.Button   button2;  
  private   System.Windows.Forms.Label   label5;  
  private   System.Windows.Forms.TextBox   txtSavefile;  
  private   System.Windows.Forms.Button   button3;  
  private   System.Data.SqlClient.SqlConnection   sqlconn;  
  private   System.Data.SqlClient.SqlCommand   sqlcom;  
  private   System.Windows.Forms.Label   label6;  
  private   System.Windows.Forms.TextBox   txtNewdb;  
  private   System.ComponentModel.Container   components   =   null;  
  System.Random   iR=new   Random();  
   
  public   Form1()  
  {  
  InitializeComponent();  
  }  
   
  protected   override   void   Dispose(   bool   disposing   )  
  {  
  if(   disposing   )  
  {  
  if   (components   !=   null)    
  {  
  components.Dispose();  
  }  
  }  
  base.Dispose(   disposing   );  
  }  
   
  #region   Windows   Form   Designer   generated   code  
  private   void   InitializeComponent()  
  {  
  this.txtName   =   new   System.Windows.Forms.TextBox();  
  this.txtPwd   =   new   System.Windows.Forms.TextBox();  
  this.txtServer   =   new   System.Windows.Forms.TextBox();  
  this.label1   =   new   System.Windows.Forms.Label();  
  this.label2   =   new   System.Windows.Forms.Label();  
  this.label3   =   new   System.Windows.Forms.Label();  
  this.button1   =   new   System.Windows.Forms.Button();  
  this.label4   =   new   System.Windows.Forms.Label();  
  this.comboBox1   =   new   System.Windows.Forms.ComboBox();  
  this.sqlconn   =   new   System.Data.SqlClient.SqlConnection();  
  this.button2   =   new   System.Windows.Forms.Button();  
  this.label5   =   new   System.Windows.Forms.Label();  
  this.txtSavefile   =   new   System.Windows.Forms.TextBox();  
  this.button3   =   new   System.Windows.Forms.Button();  
  this.sqlcom   =   new   System.Data.SqlClient.SqlCommand();  
  this.txtNewdb   =   new   System.Windows.Forms.TextBox();  
  this.label6   =   new   System.Windows.Forms.Label();  
  this.SuspendLayout();  
  this.txtName.Location   =   new   System.Drawing.Point(152,   24);  
  this.txtName.Name   =   "txtName";  
  this.txtName.Size   =   new   System.Drawing.Size(64,   21);  
  this.txtName.TabIndex   =   0;  
  this.txtName.Text   =   "sa";    
  this.txtPwd.Location   =   new   System.Drawing.Point(152,   56);  
  this.txtPwd.Name   =   "txtPwd";  
  this.txtPwd.Size   =   new   System.Drawing.Size(64,   21);  
  this.txtPwd.TabIndex   =   1;  
  this.txtPwd.Text   =   "";  
  this.txtServer.Location   =   new   System.Drawing.Point(152,   88);  
  this.txtServer.Name   =   "txtServer";  
  this.txtServer.TabIndex   =   2;  
  this.txtServer.Text   =   "(local)";  
  this.label1.Location   =   new   System.Drawing.Point(48,   24);  
  this.label1.Name   =   "label1";  
  this.label1.Size   =   new   System.Drawing.Size(96,   23);  
  this.label1.TabIndex   =   3;  
  this.label1.Text   =   "登录名:";  
  this.label2.Location   =   new   System.Drawing.Point(48,   56);  
  this.label2.Name   =   "label2";  
  this.label2.Size   =   new   System.Drawing.Size(96,   23);  
  this.label2.TabIndex   =   4;  
  this.label2.Text   =   "登录密码:";  
  this.label3.Location   =   new   System.Drawing.Point(48,   88);  
  this.label3.Name   =   "label3";  
  this.label3.Size   =   new   System.Drawing.Size(96,   23);  
  this.label3.TabIndex   =   5;  
  this.label3.Text   =   "服务器:";  
  this.button1.Location   =   new   System.Drawing.Point(48,   120);  
  this.button1.Name   =   "button1";  
  this.button1.Size   =   new   System.Drawing.Size(56,   23);  
  this.button1.TabIndex   =   6;  
  this.button1.Text   =   "连接";  
  this.button1.Click   +=   new   System.EventHandler(this.button1_Click);  
  this.label4.Location   =   new   System.Drawing.Point(48,   168);  
  this.label4.Name   =   "label4";  
  this.label4.Size   =   new   System.Drawing.Size(104,   16);  
  this.label4.TabIndex   =   7;  
  this.label4.Text   =   "要操作的数据库:";  
  this.comboBox1.Enabled   =   false;  
  this.comboBox1.Location   =   new   System.Drawing.Point(168,   168);  
  this.comboBox1.Name   =   "comboBox1";  
  this.comboBox1.Size   =   new   System.Drawing.Size(144,   20);  
  this.comboBox1.TabIndex   =   8;  
  this.comboBox1.Text   =   "请选择";  
  this.button2.Enabled   =   false;  
  this.button2.Location   =   new   System.Drawing.Point(48,   256);  
  this.button2.Name   =   "button2";  
  this.button2.TabIndex   =   9;  
  this.button2.Text   =   "备份";  
  this.button2.Click   +=   new   System.EventHandler(this.button2_Click);  
  this.label5.Location   =   new   System.Drawing.Point(48,   200);  
  this.label5.Name   =   "label5";  
  this.label5.Size   =   new   System.Drawing.Size(104,   16);  
  this.label5.TabIndex   =   10;  
  this.label5.Text   =   "存储文件:";  
  this.txtSavefile.Location   =   new   System.Drawing.Point(168,   200);  
  this.txtSavefile.Name   =   "txtSavefile";  
  this.txtSavefile.Size   =   new   System.Drawing.Size(160,   21);  
  this.txtSavefile.TabIndex   =   11;  
  this.txtSavefile.Text   =   "c:\\\\test.dat";  
  this.button3.Enabled   =   false;  
  this.button3.Location   =   new   System.Drawing.Point(216,   256);  
  this.button3.Name   =   "button3";  
  this.button3.TabIndex   =   12;  
  this.button3.Text   =   "还原";  
  this.button3.Click   +=   new   System.EventHandler(this.button3_Click);  
  this.txtNewdb.Location   =   new   System.Drawing.Point(168,   232);  
  this.txtNewdb.Name   =   "txtNewdb";  
  this.txtNewdb.Size   =   new   System.Drawing.Size(160,   21);  
  this.txtNewdb.TabIndex   =   14;  
  this.txtNewdb.Text   =   "";  
  this.label6.Location   =   new   System.Drawing.Point(48,   232);  
  this.label6.Name   =   "label6";  
  this.label6.Size   =   new   System.Drawing.Size(104,   16);  
  this.label6.TabIndex   =   13;  
  this.label6.Text   =   "还原为:";  
  this.AutoScaleBaseSize   =   new   System.Drawing.Size(6,   14);  
  this.ClientSize   =   new   System.Drawing.Size(376,   285);  
  this.Controls.AddRange(new   System.Windows.Forms.Control[]   {  
      this.txtNewdb,  
      this.label6,  
      this.button3,  
      this.txtSavefile,  
      this.label5,  
      this.button2,  
      this.comboBox1,  
      this.label4,  
      this.button1,  
      this.label3,  
      this.label2,  
      this.label1,  
      this.txtServer,  
      this.txtPwd,  
      this.txtName});  
  this.Name   =   "Form1";  
  this.Text   =   "Form1";  
  this.ResumeLayout(false);  
   
  }  
  #endregion  
  Top

6 楼xiongchen(二氧化鬼)回复于 2005-02-04 20:11:56 得分 0

[STAThread]  
  static   void   Main()    
  {  
  Application.Run(new   Form1());  
  }  
   
  private   void   button1_Click(object   sender,   System.EventArgs   e)  
  {  
  string   connstr="Data   Source="   +   txtServer.Text   +   ";User   id="   +   txtName.Text   +   ";Password="   +   txtPwd.Text   +   ";   Initial   Catalog=master";  
  sqlconn=new   SqlConnection(connstr);  
  sqlconn.Open();  
  sqlcom=new   SqlCommand("select   name   from   sysdatabases",sqlconn);  
  SqlDataReader   myReader   =   sqlcom.ExecuteReader();  
  try    
  {  
  comboBox1.Items.Clear();  
  while   (myReader.Read())    
  {  
  comboBox1.Items.Add(myReader.GetString(0));  
  }  
  }  
  finally    
  {  
  myReader.Close();  
  }  
  if   (comboBox1.Items.Count>1)  
  {  
  button2.Enabled=true;  
  button3.Enabled=true;  
  comboBox1.Enabled=true;  
  comboBox1.Focus();  
  }  
  }  
   
  private   void   button2_Click(object   sender,   System.EventArgs   e)  
  {  
  sqlcom=new   SqlCommand("sp_addumpdevice",sqlconn);  
  sqlcom.CommandType=CommandType.StoredProcedure;  
  SqlParameter   sparm=sqlcom.Parameters.Add("@devtype",SqlDbType.VarChar,20);  
  sparm.Value="disk";  
  sparm=sqlcom.Parameters.Add("@logicalname",SqlDbType.VarChar);  
  sparm.Value="test"   +   iR.Next(20000).ToString();     //该参数对应逻辑文件名称,不能重复,每次需更改  
  sparm=sqlcom.Parameters.Add("@physicalname",SqlDbType.VarChar);  
  sparm.Value=txtSavefile.Text;//如果已进行一次绑定,下一次需更名。  
  try  
  {  
  sqlcom.ExecuteNonQuery();  
  }  
  catch   (System.Exception     e1)  
  {  
  MessageBox.Show("存储文件重名,请更改存储文件名");  
  return;  
  }  
  txtNewdb.Text=comboBox1.SelectedItem.ToString();  
  sqlcom=new   SqlCommand("BACKUP   DATABASE   "   +   comboBox1.SelectedItem.ToString()   +   "   TO   test12345",sqlconn);  
  sqlcom.ExecuteNonQuery();  
  MessageBox.Show("备份成功,你可以尝试删除该数据库,点击还原即可恢复");  
  }  
   
  private   void   button3_Click(object   sender,   System.EventArgs   e)  
  {  
  //通过文件还原,如果要还原成其它数据库,在sql   server的联机Transact_sql中有相关的参考和示例--RESTORE   DATABASE  
  sqlcom=new   SqlCommand("RESTORE   DATABASE   "   +   txtNewdb.Text     +   "   FROM   Disk='"   +txtSavefile.Text   +"'"   ,sqlconn);  
  //此处只能还原为原来的数据库名称,否则会出错!如果想更名,需要加上WITH   MOVE   xxx   TO   xx   条件。  
  sqlcom.ExecuteNonQuery();  
  MessageBox.Show("还原成功");  
  }  
   
  }  
  }  
  Top

7 楼xiongchen(二氧化鬼)回复于 2005-02-04 20:12:35 得分 0

上面两段是同一类中的,因为受这里贴子长度限制,只能两次分开了!  
  Top

8 楼54duke(萝卜肉丁)回复于 2005-02-05 00:16:11 得分 0

goodTop

相关问题

  • 如何在代码中实现备份pordaxs的数据库?
  • 备份access数据库原代码,看看错在哪里?
  • 如何用代码实现mysql数据库备份?
  • 求一段sql数据库备份得代码
  • (100分)请教access数据库备份代码??在线等!!!!!!!
  • (100分)请教access数据库备份代码??在线等!!!!!!!
  • 10分求SQL数据库备份和还原Delphi代码?
  • 关于用C#代码实现数据库备份的问题
  • 请大家指教写pb代码备份sybase数据库
  • vb代码数据库备份,恢复问题

关键词

  • 数据库
  • winform
  • access
  • sql
  • 备份
  • 还原
  • sqlcom
  • 恢复
  • 请指教
  • combobox

得分解答快速导航

  • 帖主:liujun808
  • xiongchen

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo