C#Sql数据库远程恢复无法正常工作 [英] C# Sql Database remote restoring is not working
本文介绍了C#Sql数据库远程恢复无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已从此链接下载项目
将SQL Server备份文件还原到任何数据库
它在本地计算机上成功运行。它可以从本地机器中选择一个备份文件,并可以在同一台机器上恢复它。
但是当我们从本地机器中选择备份并尝试恢复时它在一台远程机器上,它显示找不到文件和一些''smo''错误...为什么请它帮忙......
提前感谢....
消息代码:
I have downloaded project from this link
Restore SQL Server Backup Files into any database
it is working successfully on the local machine. It can select a backup file from the local machine and can restore it on the same machine.
But when we are selecting a backup from the local machine and trying to restore it in a remote machine, it is showing an file could not found and some ''smo'' errors... why is it please help...
thanks in advance....
SOURCE CODE:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace TestRestore
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.TextBox Password;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.TextBox UserName;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.RadioButton SQLServer;
private System.Windows.Forms.RadioButton Windows;
private System.Windows.Forms.TextBox Server;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.TextBox BackUpFile;
private System.Windows.Forms.Button BackUp;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.ComboBox DatabaseList;
private System.Windows.Forms.RadioButton NewDatabase;
private System.Windows.Forms.RadioButton ExistingDatabase;
private System.Windows.Forms.TextBox DataBasePath;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox DataBaseName;
private System.Windows.Forms.Button Path;
private System.Windows.Forms.Button RefreshDatabases;
private System.Windows.Forms.ProgressBar pbar;
private System.Windows.Forms.Panel SQLServerPanel;
private System.Windows.Forms.Panel FilePanel;
private System.Windows.Forms.Panel ProgressPanel;
private System.Windows.Forms.Panel WelcomePanel;
private System.Windows.Forms.Panel DestinationPanel;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.Button Finish;
private System.Windows.Forms.Button Next;
private System.Windows.Forms.Button Previous;
private System.Windows.Forms.Button Cancel;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Panel FinishPanel;
private System.Windows.Forms.Label FinsihLabel;
private System.Windows.Forms.Label StatusLabel;
private System.Windows.Forms.Label label11;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.Password = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label();
this.UserName = new System.Windows.Forms.TextBox();
this.label4 = new System.Windows.Forms.Label();
this.SQLServer = new System.Windows.Forms.RadioButton();
this.Windows = new System.Windows.Forms.RadioButton();
this.Server = new System.Windows.Forms.TextBox();
this.label3 = new System.Windows.Forms.Label();
this.BackUpFile = new System.Windows.Forms.TextBox();
this.BackUp = new System.Windows.Forms.Button();
this.label6 = new System.Windows.Forms.Label();
this.DatabaseList = new System.Windows.Forms.ComboBox();
this.NewDatabase = new System.Windows.Forms.RadioButton();
this.ExistingDatabase = new System.Windows.Forms.RadioButton();
this.DataBasePath = new System.Windows.Forms.TextBox();
this.label2 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.DataBaseName = new System.Windows.Forms.TextBox();
this.Path = new System.Windows.Forms.Button();
this.RefreshDatabases = new System.Windows.Forms.Button();
this.pbar = new System.Windows.Forms.ProgressBar();
this.StatusLabel = new System.Windows.Forms.Label();
this.SQLServerPanel = new System.Windows.Forms.Panel();
this.FilePanel = new System.Windows.Forms.Panel();
this.ProgressPanel = new System.Windows.Forms.Panel();
this.label11 = new System.Windows.Forms.Label();
this.WelcomePanel = new System.Windows.Forms.Panel();
this.label10 = new System.Windows.Forms.Label();
this.DestinationPanel = new System.Windows.Forms.Panel();
this.panel1 = new System.Windows.Forms.Panel();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.label9 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.Finish = new System.Windows.Forms.Button();
this.Next = new System.Windows.Forms.Button();
this.Previous = new System.Windows.Forms.Button();
this.Cancel = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.FinishPanel = new System.Windows.Forms.Panel();
this.FinsihLabel = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SQLServerPanel.SuspendLayout();
this.FilePanel.SuspendLayout();
this.ProgressPanel.SuspendLayout();
this.WelcomePanel.SuspendLayout();
this.DestinationPanel.SuspendLayout();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
this.FinishPanel.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.Location = new System.Drawing.Point(6, 29);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(32, 32);
this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox1.TabIndex = 20;
this.pictureBox1.TabStop = false;
//
// Password
//
this.Password.Enabled = false;
this.Password.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(178)));
this.Password.Location = new System.Drawing.Point(152, 192);
this.Password.Name = "Password";
this.Password.PasswordChar = '•';
this.Password.Size = new System.Drawing.Size(144, 21);
this.Password.TabIndex = 19;
//
// label5
//
this.label5.Enabled = false;
this.label5.Location = new System.Drawing.Point(56, 192);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(100, 23);
this.label5.TabIndex = 18;
this.label5.Text = "&Password:";
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// UserName
//
this.UserName.Enabled = false;
this.UserName.Location = new System.Drawing.Point(152, 160);
this.UserName.Name = "UserName";
this.UserName.Size = new System.Drawing.Size(144, 20);
this.UserName.TabIndex = 17;
//
// label4
//
this.label4.Enabled = false;
this.label4.Location = new System.Drawing.Point(56, 160);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(100, 23);
this.label4.TabIndex = 16;
this.label4.Text = "&Login name:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// SQLServer
//
this.SQLServer.Location = new System.Drawing.Point(24, 128);
this.SQLServer.Name = "SQLServer";
this.SQLServer.Size = new System.Drawing.Size(176, 24);
this.SQLServer.TabIndex = 15;
this.SQLServer.Text = "S&QL Server authentication";
//
// Windows
//
this.Windows.Checked = true;
this.Windows.Location = new System.Drawing.Point(24, 96);
this.Windows.Name = "Windows";
this.Windows.Size = new System.Drawing.Size(160, 24);
this.Windows.TabIndex = 14;
this.Windows.TabStop = true;
this.Windows.Text = "&Windows authentication";
//
// Server
//
this.Server.Location = new System.Drawing.Point(112, 35);
this.Server.Name = "Server";
this.Server.Size = new System.Drawing.Size(184, 20);
this.Server.TabIndex = 13;
this.Server.Text = ".";
//
// label3
//
this.label3.Location = new System.Drawing.Point(40, 32);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(100, 23);
this.label3.TabIndex = 12;
this.label3.Text = "&SQL Server:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// BackUpFile
//
this.BackUpFile.Location = new System.Drawing.Point(24, 72);
this.BackUpFile.Name = "BackUpFile";
this.BackUpFile.Size = new System.Drawing.Size(256, 20);
this.BackUpFile.TabIndex = 22;
//
// BackUp
//
this.BackUp.Location = new System.Drawing.Point(288, 72);
this.BackUp.Name = "BackUp";
this.BackUp.Size = new System.Drawing.Size(24, 20);
this.BackUp.TabIndex = 23;
this.BackUp.Text = "...";
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 24);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(136, 23);
this.label6.TabIndex = 21;
this.label6.Text = "&Backup File Address:";
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// DatabaseList
//
this.DatabaseList.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.DatabaseList.Enabled = false;
this.DatabaseList.Location = new System.Drawing.Point(160, 34);
this.DatabaseList.Name = "DatabaseList";
this.DatabaseList.Size = new System.Drawing.Size(146, 21);
this.DatabaseList.TabIndex = 32;
//
// NewDatabase
//
this.NewDatabase.Checked = true;
this.NewDatabase.Location = new System.Drawing.Point(16, 64);
this.NewDatabase.Name = "NewDatabase";
this.NewDatabase.Size = new System.Drawing.Size(104, 24);
this.NewDatabase.TabIndex = 31;
this.NewDatabase.TabStop = true;
this.NewDatabase.Text = "&New Database";
//
// ExistingDatabase
//
this.ExistingDatabase.Location = new System.Drawing.Point(16, 32);
this.ExistingDatabase.Name = "ExistingDatabase";
this.ExistingDatabase.Size = new System.Drawing.Size(128, 24);
this.ExistingDatabase.TabIndex = 30;
this.ExistingDatabase.Text = "&Existing Database";
//
// DataBasePath
//
this.DataBasePath.Location = new System.Drawing.Point(160, 122);
this.DataBasePath.Name = "DataBasePath";
this.DataBasePath.Size = new System.Drawing.Size(144, 20);
this.DataBasePath.TabIndex = 27;
//
// label2
//
this.label2.Location = new System.Drawing.Point(56, 120);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 23);
this.label2.TabIndex = 26;
this.label2.Text = "Database &path";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label1
//
this.label1.Location = new System.Drawing.Point(56, 88);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(100, 23);
this.label1.TabIndex = 24;
this.label1.Text = "&Database name:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// DataBaseName
//
this.DataBaseName.Location = new System.Drawing.Point(160, 90);
this.DataBaseName.Name = "DataBaseName";
this.DataBaseName.Size = new System.Drawing.Size(144, 20);
this.DataBaseName.TabIndex = 25;
//
// Path
//
this.Path.Location = new System.Drawing.Point(309, 123);
this.Path.Name = "Path";
this.Path.Size = new System.Drawing.Size(24, 20);
this.Path.TabIndex = 29;
this.Path.Text = "...";
//
// RefreshDatabases
//
this.RefreshDatabases.Enabled = false;
this.RefreshDatabases.Location = new System.Drawing.Point(309, 35);
this.RefreshDatabases.Name = "RefreshDatabases";
this.RefreshDatabases.Size = new System.Drawing.Size(24, 20);
this.RefreshDatabases.TabIndex = 28;
this.RefreshDatabases.Text = "...";
//
// pbar
//
this.pbar.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pbar.Location = new System.Drawing.Point(56, 104);
this.pbar.Name = "pbar";
this.pbar.Size = new System.Drawing.Size(352, 16);
this.pbar.TabIndex = 3;
//
// StatusLabel
//
this.StatusLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.StatusLabel.Location = new System.Drawing.Point(16, 40);
this.StatusLabel.Name = "StatusLabel";
this.StatusLabel.Size = new System.Drawing.Size(424, 32);
this.StatusLabel.TabIndex = 2;
//
// SQLServerPanel
//
this.SQLServerPanel.Controls.Add(this.UserName);
this.SQLServerPanel.Controls.Add(this.Password);
this.SQLServerPanel.Controls.Add(this.Server);
this.SQLServerPanel.Controls.Add(this.label3);
this.SQLServerPanel.Controls.Add(this.Windows);
this.SQLServerPanel.Controls.Add(this.SQLServer);
this.SQLServerPanel.Controls.Add(this.label4);
this.SQLServerPanel.Controls.Add(this.label5);
this.SQLServerPanel.Controls.Add(this.pictureBox1);
this.SQLServerPanel.Location = new System.Drawing.Point(0, 80);
this.SQLServerPanel.Name = "SQLServerPanel";
this.SQLServerPanel.Size = new System.Drawing.Size(464, 264);
this.SQLServerPanel.TabIndex = 35;
//
// FilePanel
//
this.FilePanel.Controls.Add(this.label6);
this.FilePanel.Controls.Add(this.BackUp);
this.FilePanel.Controls.Add(this.BackUpFile);
this.FilePanel.Location = new System.Drawing.Point(0, 80);
this.FilePanel.Name = "FilePanel";
this.FilePanel.Size = new System.Drawing.Size(464, 264);
this.FilePanel.TabIndex = 36;
//
// ProgressPanel
//
this.ProgressPanel.Controls.Add(this.label11);
this.ProgressPanel.Controls.Add(this.pbar);
this.ProgressPanel.Controls.Add(this.StatusLabel);
this.ProgressPanel.Location = new System.Drawing.Point(0, 80);
this.ProgressPanel.Name = "ProgressPanel";
this.ProgressPanel.Size = new System.Drawing.Size(464, 264);
this.ProgressPanel.TabIndex = 37;
//
// label11
//
this.label11.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label11.Location = new System.Drawing.Point(16, 16);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(160, 16);
this.label11.TabIndex = 4;
this.label11.Text = "Please wait...";
//
// WelcomePanel
//
this.WelcomePanel.Controls.Add(this.label10);
this.WelcomePanel.Location = new System.Drawing.Point(0, 80);
this.WelcomePanel.Name = "WelcomePanel";
this.WelcomePanel.Size = new System.Drawing.Size(464, 264);
this.WelcomePanel.TabIndex = 38;
//
// label10
//
this.label10.Location = new System.Drawing.Point(8, 8);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(448, 88);
this.label10.TabIndex = 0;
this.label10.Text = "Welcome to MFT JET Restore wizard. This wizard helps you to restore SQL Server da" +
"tabases to any location.";
//
// DestinationPanel
//
this.DestinationPanel.Controls.Add(this.RefreshDatabases);
this.DestinationPanel.Controls.Add(this.Path);
this.DestinationPanel.Controls.Add(this.DataBaseName);
this.DestinationPanel.Controls.Add(this.label1);
this.DestinationPanel.Controls.Add(this.label2);
this.DestinationPanel.Controls.Add(this.DataBasePath);
this.DestinationPanel.Controls.Add(this.ExistingDatabase);
this.DestinationPanel.Controls.Add(this.NewDatabase);
this.DestinationPanel.Controls.Add(this.DatabaseList);
this.DestinationPanel.Location = new System.Drawing.Point(0, 80);
this.DestinationPanel.Name = "DestinationPanel";
this.DestinationPanel.Size = new System.Drawing.Size(464, 264);
this.DestinationPanel.TabIndex = 39;
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.White;
this.panel1.Controls.Add(this.pictureBox2);
this.panel1.Controls.Add(this.label9);
this.panel1.Controls.Add(this.label8);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(464, 80);
this.panel1.TabIndex = 40;
//
// pictureBox2
//
this.pictureBox2.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox2.Image")));
this.pictureBox2.Location = new System.Drawing.Point(384, 16);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(48, 48);
this.pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.pictureBox2.TabIndex = 2;
this.pictureBox2.TabStop = false;
//
// label9
//
this.label9.Location = new System.Drawing.Point(8, 40);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(368, 32);
this.label9.TabIndex = 1;
//
// label8
//
this.label8.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(178)));
this.label8.Location = new System.Drawing.Point(8, 8);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(176, 24);
this.label8.TabIndex = 0;
this.label8.Text = "MFT JET Restore";
//
// Finish
//
this.Finish.Location = new System.Drawing.Point(376, 352);
this.Finish.Name = "Finish";
this.Finish.Size = new System.Drawing.Size(80, 24);
this.Finish.TabIndex = 42;
this.Finish.Text = "&Finish";
this.Finish.Click += new System.EventHandler(this.Finish_Click);
//
// Next
//
this.Next.Location = new System.Drawing.Point(288, 352);
this.Next.Name = "Next";
this.Next.Size = new System.Drawing.Size(80, 24);
this.Next.TabIndex = 42;
this.Next.Text = "&Next >";
this.Next.Click += new System.EventHandler(this.Next_Click);
//
// Previous
//
this.Previous.Location = new System.Drawing.Point(200, 352);
this.Previous.Name = "Previous";
this.Previous.Size = new System.Drawing.Size(80, 24);
this.Previous.TabIndex = 42;
this.Previous.Text = "< &Back";
this.Previous.Click += new System.EventHandler(this.Previous_Click);
//
// Cancel
//
this.Cancel.Location = new System.Drawing.Point(112, 352);
this.Cancel.Name = "Cancel";
this.Cancel.Size = new System.Drawing.Size(80, 24);
this.Cancel.TabIndex = 42;
this.Cancel.Text = "&Cancel";
this.Cancel.Click += new System.EventHandler(this.Cancel_Click);
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(0, 339);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(464, 7);
this.groupBox1.TabIndex = 43;
this.groupBox1.TabStop = false;
//
// groupBox2
//
this.groupBox2.Location = new System.Drawing.Point(0, 75);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(464, 7);
this.groupBox2.TabIndex = 45;
this.groupBox2.TabStop = false;
//
// FinishPanel
//
this.FinishPanel.Controls.Add(this.FinsihLabel);
this.FinishPanel.Location = new System.Drawing.Point(0, 80);
this.FinishPanel.Name = "FinishPanel";
this.FinishPanel.Size = new System.Drawing.Size(464, 264);
this.FinishPanel.TabIndex = 46;
//
// FinsihLabel
//
this.FinsihLabel.Location = new System.Drawing.Point(8, 16);
this.FinsihLabel.Name = "FinsihLabel";
this.FinsihLabel.Size = new System.Drawing.Size(448, 88);
this.FinsihLabel.TabIndex = 0;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(464, 383);
this.Controls.Add(this.panel1);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.Finish);
this.Controls.Add(this.Next);
this.Controls.Add(this.Previous);
this.Controls.Add(this.Cancel);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.DestinationPanel);
this.Controls.Add(this.ProgressPanel);
this.Controls.Add(this.FilePanel);
this.Controls.Add(this.SQLServerPanel);
this.Controls.Add(this.FinishPanel);
this.Controls.Add(this.WelcomePanel);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "JET Restore";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.SQLServerPanel.ResumeLayout(false);
this.SQLServerPanel.PerformLayout();
this.FilePanel.ResumeLayout(false);
this.FilePanel.PerformLayout();
this.ProgressPanel.ResumeLayout(false);
this.WelcomePanel.ResumeLayout(false);
this.DestinationPanel.ResumeLayout(false);
this.DestinationPanel.PerformLayout();
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.FinishPanel.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
this.Windows.CheckedChanged += new EventHandler(OnChangeAuthenticationType);
this.SQLServer.CheckedChanged += new EventHandler(OnChangeAuthenticationType);
this.ExistingDatabase.CheckedChanged += new EventHandler(OnChangeDestination);
this.NewDatabase.CheckedChanged += new EventHandler(OnChangeDestination);
this.BackUp.Click += new EventHandler(this.BackUp_Click);
this.Path.Click += new EventHandler(this.Path_Click);
this.Navigate(true);
// this.introductionPage1.IntroductionText = @"This wizard helps you restore a database in different Microsoft SQL Server database.
//
//
//
//Programmer : Phorozan@gmail.com";
}
private void OnChangeAuthenticationType(object sender , EventArgs e)
{
bool b = !(this.Windows.Checked);
this.UserName.Enabled = b;
this.Password.Enabled = b;
this.label4.Enabled = b;
this.label5.Enabled = b;
if(((RadioButton)sender).Name == "SQLServer" && ((RadioButton)sender).Checked)
{
this.UserName.Focus();
this.UserName.SelectAll();
}
}
private void OnChangeDestination(object sender , EventArgs e)
{
bool b = !(this.ExistingDatabase.Checked);
this.DataBaseName.Enabled = b;
this.DataBasePath.Enabled = b;
this.label1.Enabled = b;
this.label2.Enabled = b;
this.Path.Enabled = b;
this.DatabaseList.Enabled= !b;
this.RefreshDatabases.Enabled= !b;
if(((RadioButton)sender).Name == "NewDatabase" && ((RadioButton)sender).Checked)
{
this.DataBaseName.Focus();
this.DataBaseName.SelectAll();
}
else
{
this.DatabaseList.Focus();
this.FillDatabaseList(this.ConnectionString());
}
}
private void Path_Click(object sender, System.EventArgs e)
{
FolderBrowserDialog f = new FolderBrowserDialog();
f.ShowNewFolderButton = true;
f.Description = "Select path to create new database:";
if(System.IO.Directory.Exists(this.DataBasePath.Text))
f.SelectedPath = this.DataBasePath.Text;
if (f.ShowDialog()==DialogResult.OK)
this.DataBasePath.Text = f.SelectedPath;
f.Dispose();
}
private string ConnectionString()
{
string s;
s = string.Format("Server={0};",this.Server.Text);
if(this.Windows.Checked)
s += "Trusted_Connection=True;";
else
s += string.Format("UId={0};PWD={1};",this.UserName.Text,this.Password.Text);
s+="Connect Timeout=1;Max Pool Size=1;Connection Lifetime=10;";
return s;
}
private void FillDatabaseList(string ConnectionString)
{
this.DatabaseList.Items.Clear();
SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand cmd = connection.CreateCommand();
SqlDataReader reader;
cmd.CommandText = "Select Name From sysDatabases";
try
{
connection.Open();
reader = cmd.ExecuteReader();
string Database="";
while(reader.Read())
{
Database = reader.GetString(0);
if(Database != "master" && Database != "model" && Database != "msdb" && Database != "tempdb" )
this.DatabaseList.Items.Add(Database);
}
reader.Close();
(reader as IDisposable).Dispose();
}
catch(SqlException ex)
{
MessageBox.Show(ex.ToString(),"SQL Server Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
}
finally
{
if(connection.State == ConnectionState.Open)
connection.Close();
cmd.Dispose();
connection.Dispose();
if(this.DatabaseList.Items.Count>0)
this.DatabaseList.SelectedIndex = 0;
}
}
private void RefreshDatabases_Click(object sender, System.EventArgs e)
{
this.FillDatabaseList(this.ConnectionString());
}
// private bool CheckInput()
// {
// if (this.Server.Text == "")
// {
// MessageBox.Show("Specify SQL Server .","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
// this.Server.Focus();
//
// return false;
// }
//
// if (this.UserName.Text == "" && this.SQLServer.Checked == true)
// {
// MessageBox.Show("Enter User Name.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
// this.UserName.Focus();
//
// return false;
// }
//
// if(!System.IO.File.Exists(this.BackUpFile.Text))
// {
// MessageBox.Show("Select BackUp File.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
// this.UserName.Focus();
//
// return false;
// }
// if (this.ExistingDatabase.Checked)
// {
//
// if(this.DatabaseList.SelectedIndex < 0)
// {
// MessageBox.Show("Select Database.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
// this.DatabaseList.Focus();
//
// return false;
// }
// }
// else
// {
// if(this.DataBaseName.Text=="")
// {
// MessageBox.Show("Enter Database Name.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
// this.DataBaseName.Focus();
//
// return false;
// }
// if(!System.IO.Directory.Exists(this.DataBasePath.Text))
// {
// MessageBox.Show("Enter a valid path.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
// this.DataBasePath.Focus();
//
// return false;
// }
// }
//
// return true;
// }
private bool GetFileRestored(string ConnectionString,string DatabaseName,ref string DataFile , ref string LogFile)
{
SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand cmd = connection.CreateCommand();
SqlDataReader reader;
cmd.CommandText = this.PrepareCommand("Restore.sql",DatabaseName);
try
{
connection.Open();
reader = cmd.ExecuteReader();
while(reader.Read())
{
if(reader.GetString(2)=="D")
DataFile = reader.GetString(0);
else
LogFile = reader.GetString(0);
}
reader.Close();
(reader as IDisposable).Dispose();
}
catch(SqlException ex)
{
MessageBox.Show(ex.ToString(),"SQL Server Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return false;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return false;
}
finally
{
if(connection.State == ConnectionState.Open)
connection.Close();
cmd.Dispose();
connection.Dispose();
}
return true;
}
private string PrepareCommand(string Name , string DatabaseName)
{
string Commands = this.LoadSQLFromAssembly(Name);
if (Commands == null)
return null;
Commands = Commands.Replace("%Database%" , DatabaseName);
Commands = Commands.Replace("%DatabasePath%" , this.DataBasePath.Text + "\\");
Commands = Commands.Replace("%BackUpPath%" , this.BackUpFile.Text);
return Commands;
}
private bool ExecuteSQLCommand(string ConnectionString , string Commands)
{
SqlConnection connection = new SqlConnection(ConnectionString);
SqlCommand cmd = connection.CreateCommand();
cmd.CommandText = Commands;
try
{
connection.Open();
cmd.ExecuteNonQuery();
}
catch(SqlException ex)
{
MessageBox.Show(ex.ToString(),"SQL Server Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return false;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
return false;
}
finally
{
if(connection.State == ConnectionState.Open)
connection.Close();
cmd.Dispose();
connection.Dispose();
}
return true;
}
private string LoadSQLFromAssembly (string Name)
{
System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream(this.GetType(),"SQL." + Name);
if(stream == null)
{
MessageBox.Show("Internal Error occured! Close Application & try again.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
return null;
}
System.IO.StreamReader reader= new System.IO.StreamReader(stream);
if (reader == null)
{
MessageBox.Show("Internal Error occured! Close Application & try again.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
return null;
}
string s = reader.ReadToEnd();
reader.Close();
return s;
}
private void BackUp_Click(object sender, System.EventArgs e)
{
OpenFileDialog f = new OpenFileDialog();
f.Filter = "SQL Server BackUp Files|*.bak|All Files|*.*";
f.Title = "Open Backup File";
if(System.IO.File.Exists(this.BackUpFile.Text))
f.FileName = this.BackUpFile.Text;
if(f.ShowDialog()==DialogResult.OK)
{
this.BackUpFile.Text = f.FileName;
}
f.Dispose();
}
#region Validateion
private bool Validate_File()
{
if(!System.IO.File.Exists(this.BackUpFile.Text))
{
MessageBox.Show("Select BackUp File.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
this.UserName.Focus();
return false;
}
return true;
}
private bool Validate_SQLServer()
{
SqlConnection connection = new SqlConnection(this.ConnectionString());
try
{
connection.Open();
connection.Close();
return true;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message , "Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
return false;
}
finally
{
connection.Dispose();
}
}
private bool Validate_Destination()
{
if (this.ExistingDatabase.Checked)
{
if(this.DatabaseList.SelectedIndex < 0)
{
MessageBox.Show("Select Database.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
this.DatabaseList.Focus();
return false;
}
}
else
{
if(this.DataBaseName.Text=="")
{
MessageBox.Show("Enter Database Name.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
this.DataBaseName.Focus();
return false;
}
if(this.DatabaseList.Items.Contains(this.DataBaseName.Text))
{
MessageBox.Show("Database already exists!","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
this.DataBaseName.Focus();
return false;
}
if(!System.IO.Directory.Exists(this.DataBasePath.Text))
{
MessageBox.Show("Enter a valid path.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
this.DataBasePath.Focus();
return false;
}
}
return true;
}
#endregion
private void DoRestore()
{
this.StatusLabel.Text = "Gathering information about operation...";
this.SetProgressBar(0);
string Commands , DataFile="" , LogFile ="" , DatabaseName ="";
if(this.NewDatabase.Checked)
{
Commands = this.PrepareCommand("CreateNewDatabase.sql" , this.DataBaseName.Text);
if(Commands == null)
{
this.FinsihLabel.Text = "Wizard can not do operation!";
this.Navigate(true);
return;
}
this.StatusLabel.Text = string.Format( "Creating new database with name {0} ..." , this.DataBaseName.Text) ;
this.SetProgressBar(10);
if(this.ExecuteSQLCommand(this.ConnectionString() , Commands))
{
this.StatusLabel.Text = "Database Created Successfully!" ;
}
else
{
return;
}
this.SetProgressBar(40);
DatabaseName = this.DataBaseName.Text;
}
else
{
this.StatusLabel.Text = "" ;
DatabaseName = this.DatabaseList.SelectedItem.ToString();
}
this.StatusLabel.Text += string.Format( "\nLoading {0} ..." ,@"\\BLR_11285_OCR1\ E:\Testdb.bak") ;
this.SetProgressBar(50);
if (this.GetFileRestored(this.ConnectionString(),DatabaseName,ref DataFile , ref LogFile))
{
this.StatusLabel.Text = "Generating SQL scripts ..." ;
Commands = this.PrepareCommand("RestoreFinall.sql" , DatabaseName);
if(Commands == null)
{
this.FinsihLabel.Text = "Wizard can not do operation!";
this.Navigate(true);
return;
}
this.StatusLabel.Text = "Finalizing SQL scripts ..." ;
this.SetProgressBar(60);
Commands = Commands.Replace("%OldData%" , DataFile).Replace("%OldLog%",LogFile);
this.SetProgressBar(70);
this.StatusLabel.Text = "Finalizing operation ..." ;
this.ExecuteSQLCommand(this.ConnectionString() , Commands);
this.StatusLabel.Text = "Operation successfully done." ;
this.SetProgressBar(100);
}
this.FinsihLabel.Text = string.Format ("Restore wizard successfully finished. Result : \n\nFile {0} successfully restore to {1}.",this.BackUpFile.Text , DatabaseName);
this.Navigate(true);
}
private void SetProgressBar(int value)
{
this.pbar.Value = value;
Application.DoEvents();
}
private int Pointer = 0;
private const int MaxPages = 6;
private void Navigate(bool forward)
{
Pointer = forward ? Pointer + 1 : ((Pointer == MaxPages) ? Pointer - 2 : Pointer - 1);
this.Previous.Enabled = (Pointer == 1) ? false : true;
this.Next.Enabled = (Pointer == MaxPages) ? false : true;
this.Finish.Enabled = !this.Next.Enabled;
this.Cancel.Enabled = this.Next.Enabled;
this.WelcomePanel.Visible = (Pointer == 1 ) ? true : false;
this.SQLServerPanel.Visible = (Pointer == 2 ) ? true : false;
this.FilePanel.Visible = (Pointer == 3 ) ? true : false;
this.DestinationPanel.Visible = (Pointer == 4 ) ? true : false;
this.ProgressPanel.Visible = (Pointer == 5 ) ? true : false;
this.FinishPanel.Visible = (Pointer == 6 ) ? true : false;
if(Pointer == 5)
{
this.Cancel.Enabled = false;
this.Finish.Enabled = false;
this.Next.Enabled = false;
this.Previous.Enabled = false;
this.DoRestore();
}
if(Pointer == 3)
{
this.FillDatabaseList(this.ConnectionString());
}
}
private void Next_Click(object sender, System.EventArgs e)
{
switch(this.Pointer)
{
case 2 :
if(!this.Validate_SQLServer())
return;
break;
case 3:
if(!this.Validate_File())
return;
break;
case 4:
if(!this.Validate_Destination())
return;
break;
}
this.Navigate(true);
}
private void Previous_Click(object sender, System.EventArgs e)
{
this.Navigate(false);
}
private void Finish_Click(object sender, System.EventArgs e)
{
this.Close();
}
private void Cancel_Click(object sender, System.EventArgs e)
{
if(MessageBox.Show("Are you sure want to cancel wizard?","Restore Database",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.No)
return;
this.Close();
}
private void button1_Click(object sender, EventArgs e)
{
string name = Environment.MachineName;
string n = "\\" + name + this.DataBasePath.Text;
MessageBox.Show(n);
}
}
}
推荐答案
this.Icon\")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = \"Form1\";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = \"JET Restore\";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.SQLServerPanel.ResumeLayout(false);
this.SQLServerPanel.PerformLayout();
this.FilePanel.ResumeLayout(false);
this.FilePanel.PerformLayout();
this.ProgressPanel.ResumeLayout(false);
this.WelcomePanel.ResumeLayout(false);
this.DestinationPanel.ResumeLayout(false);
this.DestinationPanel.PerformLayout();
this .panel1.ResumeLayout( false );
this.panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
this.FinishPanel.ResumeLayout(false);
this .ResumeLayout( false );
\t\t}
\t\t#endregion
\t\t/// <summary>
\t\t/// The main entry point for the application.
\t\t/// </summary>
\t\t[STAThread]
\t\tstatic void Main()
\t\t{
\t\t\tApplication.Run(new Form1());
\t\t}
\t\tprivate void Form1_Load(object sender, System.EventArgs e)
\t\t{
\t\t\tthis.Windows.CheckedChanged += new EventHandler(OnChangeAuthenticationType);
\t\t\tthis.SQLServer.CheckedChanged += new EventHandler(OnChangeAuthenticationType);
\t\t\tthis.ExistingDatabase.CheckedChanged += new EventHandler(OnChangeDestination);
\t\t\tthis.NewDatabase.CheckedChanged += new EventHandler(OnChangeDestination);
\t\t\tthis.BackUp.Click += new EventHandler(this.BackUp_Click);
\t\t\tthis.Path.Click += new EventHandler(this.Path_Click);
\t\t\tthis.Navigate(true);
//\t\t\tthis.introductionPage1.IntroductionText = @\"This wizard helps you restore a database in different Microsoft SQL Server database.
//
//
//
//Programmer : Phorozan@gmail.com\";
\t\t}
\t\tprivate void OnChangeAuthenticationType(object sender , EventArgs e)
\t\t{
\t\t\tbool b = !(this.Windows.Checked);
\t\t\tthis.UserName.Enabled = b;
\t\t\tthis.Password.Enabled = b;
\t\t\tthis.label4.Enabled = b;
\t\t\tthis.label5.Enabled = b;
\t\t\tif(((RadioButton)sender).Name == \"SQLServer\" && ((RadioButton)sender).Checked)
\t\t\t{
\t\t\t\tthis.UserName.Focus();
\t\t\t\tthis.UserName.SelectAll();
\t\t\t}
\t\t}
\t\t
\t\tprivate void OnChangeDestination(object sender , EventArgs e)
\t\t{
\t\t\tbool b = !(this.ExistingDatabase.Checked);
\t\t\tthis.DataBaseName.Enabled = b;
\t\t\tthis.DataBasePath.Enabled = b;
\t\t\tthis.label1.Enabled = b;
\t\t\tthis.label2.Enabled = b;
\t\t\tthis.Path.Enabled = b;
\t\t\tthis.DatabaseList.Enabled= !b;
\t\t\tthis.RefreshDatabases.Enabled= !b;
\t\t\tif(((RadioButton)sender).Name == \"NewDatabase\" && ((RadioButton)sender).Checked)
\t\t\t{
\t\t\t\tthis.DataBaseName.Focus();
\t\t\t\tthis.DataBaseName.SelectAll();
\t\t\t}
\t\t\telse
\t\t\t{
\t\t\t\tthis.DatabaseList.Focus();
\t\t\t\tthis.FillDatabaseList(this.ConnectionString());
\t\t\t}
\t\t}
\t\tprivate void Path_Click(object sender, System.EventArgs e)
\t\t{
\t\t\t
\t\t\tFolderBrowserDialog f = new FolderBrowserDialog();
\t\t\tf.ShowNewFolderButton = true;
\t\t\tf.Description = \"Select path to create new database:\";
\t\t\tif(System.IO.Directory.Exists(this.DataBasePath.Text))
\t\t\t\tf.SelectedPath = this.DataBasePath.Text;
\t\t\tif (f.ShowDialog()==DialogResult.OK)
\t\t\t\tthis.DataBasePath.Text = f.SelectedPath;
\t\t\t
\t\t\tf.Dispose();
\t\t}
\t\tprivate string ConnectionString()
\t\t{
\t\t\tstring s;
\t\t\ts = string.Format(\"Server={0};\",this.Server.Text);
\t\t\tif(this.Windows.Checked)
\t\t\t\ts += \"Trusted_Connection=True;\";
\t\t\telse
\t\t\t\ts += string.Format(\"UId={0};PWD={1};\",this.UserName.Text,this.Password.Text);
\t\t\ts+=\"Connect Timeout=1;Max Pool Size=1;Connection Lifetime=10;\";
\t\t\treturn s;
\t\t}
\t\tprivate void FillDatabaseList(string ConnectionString)
\t\t{
\t\t\tthis.DatabaseList.Items.Clear();
\t\t\tSqlConnection\tconnection\t= new SqlConnection(ConnectionString);
\t\t\tSqlCommand\t\tcmd\t\t\t= connection.CreateCommand();
\t\t\tSqlDataReader\treader;
\t\t\tcmd.CommandText = \"Select Name From sysDatabases\";
\t\t\ttry
\t\t\t{
\t\t\t\tconnection.Open();
\t\t\t\treader = cmd.ExecuteReader();
\t\t\t\tstring Database=\"\";
\t\t\t\twhile(reader.Read())
\t\t\t\t{
\t\t\t\t\tDatabase = reader.GetString(0);
\t\t\t\t\tif(Database != \"master\" && Database != \"model\" && Database != \"msdb\" && Database != \"tempdb\" )
\t\t\t\t\t\tthis.DatabaseList.Items.Add(Database);
\t\t\t\t}
\t\t\t\t
\t\t\t\treader.Close();
\t\t\t\t(reader as IDisposable).Dispose();
\t\t\t\t
\t\t\t}
\t\t\tcatch(SqlException ex)
\t\t\t{
\t\t\t\tMessageBox.Show(ex.ToString(),\"SQL Server Error\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t}
\t\t\tcatch(Exception ex)
\t\t\t{
\t\t\t\tMessageBox.Show(ex.ToString(),\"Error\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t}
\t\t\tfinally
\t\t\t{
\t\t\t\tif(connection.State == ConnectionState.Open)
\t\t\t\t\tconnection.Close();
\t\t\t
\t\t\t\tcmd.Dispose();
\t\t\t\tconnection.Dispose();
\t\t\t\tif(this.DatabaseList.Items.Count>0)
\t\t\t\t\tthis.DatabaseList.SelectedIndex = 0;
\t\t\t}
\t\t}
\t\tprivate void RefreshDatabases_Click(object sender, System.EventArgs e)
\t\t{
\t\t\tthis.FillDatabaseList(this.ConnectionString());
\t\t}
//\t\tprivate bool CheckInput()
//\t\t{
//\t\t\tif (this.Server.Text == \"\")
//\t\t\t{
//\t\t\t\tMessageBox.Show(\"Specify SQL Server .\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
//\t\t\t\tthis.Server.Focus();
//
//\t\t\t\treturn false;
//\t\t\t}
//
//\t\t\tif (this.UserName.Text == \"\" && this.SQLServer.Checked == true)
//\t\t\t{
//\t\t\t\tMessageBox.Show(\"Enter User Name.\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
//\t\t\t\tthis.UserName.Focus();
//
//\t\t\t\treturn false;
//\t\t\t}
//
//\t\t\tif(!System.IO.File.Exists(this.BackUpFile.Text))
//\t\t\t{
//\t\t\t\tMessageBox.Show(\"Select BackUp File.\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
//\t\t\t\tthis.UserName.Focus();
//
//\t\t\t\treturn false;
//\t\t\t}
//\t\t\tif (this.ExistingDatabase.Checked)
//\t\t\t{
//
//\t\t\t\tif(this.DatabaseList.SelectedIndex < 0)
//\t\t\t\t{
//\t\t\t\t\tMessageBox.Show(\"Select Database.\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
//\t\t\t\t\tthis.DatabaseList.Focus();
//
//\t\t\t\t\treturn false;
//\t\t\t\t}
//\t\t\t}
//\t\t\telse
//\t\t\t{
//\t\t\t\tif(this.DataBaseName.Text==\"\")
//\t\t\t\t{
//\t\t\t\t\tMessageBox.Show(\"Enter Database Name.\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
//\t\t\t\t\tthis.DataBaseName.Focus();
//
//\t\t\t\t\treturn false;
//\t\t\t\t}
//\t\t\t\tif(!System.IO.Directory.Exists(this.DataBasePath.Text))
//\t\t\t\t{
//\t\t\t\t\tMessageBox.Show(\"Enter a valid path.\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
//
//\t\t\t\t\tthis.DataBasePath.Focus();
//
//\t\t\t\t\treturn false;
//\t\t\t\t}
//\t\t\t}
//\t\t\t
//\t\t\treturn true;
//\t\t}
\t\t
\t\tprivate bool Get FileRestored(string ConnectionString,string DatabaseName,ref string DataFile , ref string LogFile)
\t\t{
\t\t\tSqlConnection\tconnection\t= new SqlConnection(ConnectionString);
\t\t\tSqlCommand\t\tcmd\t\t\t= connection.CreateCommand();
\t\t\tSqlDataReader\treader;
\t\t\t
\t\t\tcmd.CommandText = this.PrepareCommand(\"Restore.sql\",DatabaseName);
\t\t\ttry
\t\t\t{
\t\t\t\tconnection.Open();
\t\t\t\treader = cmd.ExecuteReader();
\t\t\t\twhile(reader.Read())
\t\t\t\t{
\t\t\t\t\tif(reader.GetString(2)==\"D\")
\t\t\t\t\t\tDataFile = reader.GetString(0);
\t\t\t\t\telse
\t\t\t\t\t\tLogFile = reader.GetString(0);
\t\t\t\t}
\t\t\t\treader.Close();
\t\t\t\t(reader as IDisposable).Dispose();
\t\t\t}
\t\t\tcatch(SqlException ex)
\t\t\t{
\t\t\t\tMessageBox.Show(ex.ToString(),\"SQL Server Error\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t\treturn false;
\t\t\t}
\t\t\tcatch(Exception ex)
\t\t\t{
\t\t\t\tMessageBox.Show(ex.ToString(),\"Error\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t\treturn false;
\t\t\t}
\t\t\tfinally
\t\t\t{
\t\t\t\tif(connection.State == ConnectionState.Open)
\t\t\t\t\tconnection.Close();
\t\t\t
\t\t\t\tcmd.Dispose();
\t\t\t\tconnection.Dispose();
\t\t\t}
\t\t\treturn true;
\t\t}
\t\tprivate string PrepareCommand(string Name , string DatabaseName)
\t\t{
\t\t\tstring Commands = this.LoadSQLFromAssembly(Name);
\t\t\tif (Commands == null)
\t\t\t\treturn null;
\t\t\tCommands = Commands.Replace(\"%Database%\" , DatabaseName);
\t\t\tCommands = Commands.Replace(\"%DatabasePath%\" , this.DataBasePath.Text + \"\\\");
\t\t\tCommands = Commands.Replace(\"%BackUpPath%\" , this.BackUpFile.Text);
\t\t\treturn Commands;
\t\t}
\t\tprivate bool ExecuteSQLCommand(string ConnectionString , string Commands)
\t\t{
\t\t\tSqlConnection\tconnection\t= new SqlConnection(ConnectionString);
\t\t\tSqlCommand\t\tcmd\t\t\t= connection.CreateCommand();
\t\t\tcmd.CommandText = Commands;
\t\t\ttry
\t\t\t{
\t\t\t\tconnection.Open();
\t\t\t\tcmd.ExecuteNonQuery();
\t\t\t}
\t\t\tcatch(SqlException ex)
\t\t\t{
\t\t\t\tMessageBox.Show(ex.ToString(),\"SQL Server Error\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t\treturn false;
\t\t\t}
\t\t\tcatch(Exception ex)
\t\t\t{
\t\t\t\tMessageBox.Show(ex.ToString(),\"Error\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t\treturn false;
\t\t\t}
\t\t\tfinally
\t\t\t{
\t\t\t\tif(connection.State == ConnectionState.Open)
\t\t\t\t\tconnection.Close();
\t\t\t
\t\t\t\tcmd.Dispose();
\t\t\t\tconnection.Dispose();
\t\t\t}
\t\t\treturn true;
\t\t}
\t\tprivate string LoadSQLFromAssembly (string Name)
\t\t{
\t\t\tSystem.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream(this.GetType(),\"SQL.\" + Name);
\t\t\tif(stream == null)
\t\t\t{
\t\t\t\tMessageBox.Show(\"Internal Error occured! Close Application & try again.\",\"Error\",MessageBoxButtons.OK,MessageBoxIcon.Error);
\t\t\t\treturn null;
\t\t\t}
\t\t\tSystem.IO.StreamReader reader= new System.IO.StreamReader(stream);
\t\t\tif (reader == null)
\t\t\t{
\t\t\t\tMessageBox.Show(\"Internal Error occured! Close Application & try again.\",\"Error\",MessageBoxButtons.OK,MessageBoxIcon.Error);
\t\t\t\treturn null;
\t\t\t}
\t\t\tstring s = reader.ReadToEnd();
\t\t\treader.Close();
\t\t\treturn s;
\t\t\t\t\t\t\t\t
\t\t}
\t\tprivate void BackUp_Click(object sender, System.EventArgs e)
\t\t{
\t\t\tOpenFileDialog f = new OpenFileDialog();
\t\t\tf.Filter = \"SQL Server BackUp Files|*.bak|All Files|*.*\";
\t\t\tf.Title = \"Open Backup File\";
\t\t\tif(System.IO.File.Exists(this.BackUpFile.Text))
\t\t\t\tf.FileName = this.BackUpFile.Text;
\t\t\tif(f.ShowDialog()==DialogResult.OK)
\t\t\t{
\t\t\t\tthis.BackUpFile.Text = f.FileName;
\t\t\t}
\t\t\tf.Dispose();
\t\t}
#region Validateion
\t\tprivate bool Validate_File()
\t\t{
\t\t\tif(!System.IO.File.Exists(this.BackUpFile.Text))
\t\t\t{
\t\t\t\tMessageBox.Show(\"Select BackUp File.\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t\tthis.UserName.Focus();
\t\t\t\treturn false;
\t\t\t}
\t\t\treturn true;
\t\t}
\t\tprivate bool Validate_SQLServer()
\t\t{
\t\t\tSqlConnection\tconnection\t= new SqlConnection(this.ConnectionString());
\t\t\t
\t\t\ttry
\t\t\t{
\t\t\t\tconnection.Open();
\t\t\t\tconnection.Close();
\t\t\t\treturn true;
\t\t\t}
\t\t\tcatch(Exception ex)
\t\t\t{
\t\t\t\tMessageBox.Show(ex.Message , \"Error\",MessageBoxButtons.OK,MessageBoxIcon.Error);
\t\t\t\treturn false;
\t\t\t}
\t\t\tfinally
\t\t\t{
\t\t\t\tconnection.Dispose();
\t\t\t}
\t\t\t
\t\t\t
\t\t}
\t\tprivate bool Validate_Destination()
\t\t{
\t\t\tif (this.ExistingDatabase.Checked)
\t\t\t{
\t\t\t\tif(this.DatabaseList.SelectedIndex < 0)
\t\t\t\t{
\t\t\t\t\tMessageBox.Show(\"Select Database.\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t\t\tthis.DatabaseList.Focus();
\t\t\t\t\treturn false;
\t\t\t\t}
\t\t\t}
\t\t\telse
\t\t\t{
\t\t\t\tif(this.DataBaseName.Text==\"\")
\t\t\t\t{
\t\t\t\t\tMessageBox.Show(\"Enter Database Name.\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t\t\tthis.DataBaseName.Focus();
\t\t\t\t\treturn false;
\t\t\t\t}
\t\t\t\tif(this.DatabaseList.Items.Contains(this.DataBaseName.Text))
\t\t\t\t{
\t\t\t\t\tMessageBox.Show(\"Database already exists!\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t\t\tthis.DataBaseName.Focus();
\t\t\t\t\treturn false;
\t\t\t\t}
\t\t\t\tif(!System.IO.Directory.Exists(this.DataBasePath.Text))
\t\t\t\t{
\t\t\t\t\tMessageBox.Show(\"Enter a valid path.\",\"User\",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
\t\t\t\t\tthis.DataBasePath.Focus();
\t\t\t\t\treturn false;
\t\t\t\t}
\t\t\t}
\t\t\treturn true;
\t\t}
#endregion
\t\tprivate void DoRestore()
\t\t{
\t\t\t
\t\t\tthis.StatusLabel.Text = \"Gathering information about operation...\";
\t\t\tthis.SetProgressBar(0);
\t\t\tstring Commands , DataFile=\"\" , LogFile =\"\" , DatabaseName =\"\";
\t\t\tif(this.NewDatabase.Checked)
\t\t\t{
\t\t\t
\t\t\t\tCommands = this.PrepareCommand(\"CreateNewDatabase.sql\" , this.DataBaseName.Text);
\t\t\t\tif(Commands == null)
\t\t\t\t{
\t\t\t\t\tthis.FinsihLabel.Text = \"Wizard can not do operation!\";
\t\t\t\t\tthis.Navigate(true);
\t\t\t\t\treturn;
\t\t\t\t}
\t\t\t\tthis.StatusLabel.Text = string.Format( \"Creating new database with name {0} ...\" , this.DataBaseName.Text) ;
\t\t\t\tthis.SetProgressBar(10);
\t\t\t\tif(this.ExecuteSQLCommand(this.ConnectionString() , Commands))
\t\t\t\t{
\t\t\t\t\tthis.StatusLabel.Text = \"Database Created Successfully!\" ;
\t\t\t\t}
\t\t\t\telse
\t\t\t\t{
\t\t\t\t\treturn;
\t\t\t\t}
\t\t\t\tthis.SetProgressBar(40);
\t\t\t\tDatabaseName = this.DataBaseName.Text;
\t\t\t}
\t\t\telse
\t\t\t{
\t\t\t\tthis.StatusLabel.Text = \"\" ;
\t\t\t\tDatabaseName = this.DatabaseList.SelectedItem.ToString();
\t\t\t}
\t\t\tthis.StatusLabel.Text += string.Format( \"\nLoading {0} ...\" ,@\"\\BLR_11285_OCR1\ E:\Testdb.bak\") ;
\t\t\tthis.SetProgressBar(50);
\t\t\tif (this.GetFileRestored(this.ConnectionString(),DatabaseName,ref DataFile , ref LogFile))
\t\t\t{
\t\t\t\tthis.StatusLabel.Text = \"Generating SQL scripts ...\" ;
\t\t\t\tCommands = this.PrepareCommand(\"RestoreFinall.sql\" , DatabaseName);
\t\t\t\tif(Commands == null)
\t\t\t\t{
\t\t\t\t\tthis.FinsihLabel.Text = \"Wizard can not do operation!\";
\t\t\t\t\tthis.Navigate(true);
\t\t\t\t\treturn;
\t\t\t\t}
\t\t\t\tthis.StatusLabel.Text = \"Finalizing SQL scripts ...\" ;
\t\t\t\tthis.SetProgressBar(60);
\t\t\t\tCommands = Commands.Replace(\"%OldData%\" , DataFile).Replace(\"%OldLog%\",LogFile);
\t\t\t\tthis.SetProgressBar(70);
\t\t\t\tthis.StatusLabel.Text = \"Finalizing operation ...\" ;
\t\t\t\tthis.ExecuteSQLCommand(this.ConnectionString() , Commands);
\t\t\t\tthis.StatusLabel.Text = \"Operation successfully done.\" ;
\t\t\t\tthis.SetProgressBar(100);
\t\t\t}
\t\t\tthis.FinsihLabel.Text = string.Format (\"Restore wizard successfully finished. Result : \n\nFile {0} successfully restore to {1}.\",this.BackUpFile.Text , DatabaseName);
\t\t\tthis.Navigate(true);
\t\t}
\t\tprivate void SetProgressBar(int value)
\t\t{
\t\t\tthis.pbar.Value = value;
\t\t\tApplication.DoEvents();
\t\t}
\t\tprivate int Pointer = 0;
\t\tprivate const int MaxPages = 6;
\t\tprivate void Navigate(bool forward)
\t\t{
\t\t\tPointer = forward ? Pointer + 1 : ((Pointer == MaxPages) ? Pointer - 2 : Pointer - 1);
\t\t\tthis.Previous.Enabled = (Pointer == 1) ? false : true ;
\t\t\tthis.Next.Enabled = (Pointer == MaxPages) ? false : true ;
\t\t\tthis.Finish.Enabled = !this.Next.Enabled;
\t\t\tthis.Cancel.Enabled = this.Next.Enabled;
\t\t\tthis.WelcomePanel.Visible = (Pointer == 1 ) ? true : false ;
\t\t\tthis.SQLServerPanel.Visible = (Pointer == 2 ) ? true : false ;
\t\t\tthis.FilePanel.Visible = (Pointer == 3 ) ? true : false ;
\t\t\tthis.DestinationPanel.Visible = (Pointer == 4 ) ? true : false ;
\t\t\tthis.ProgressPanel.Visible = (Pointer == 5 ) ? true : false ;
\t\t\tthis.FinishPanel.Visible = (Pointer == 6 ) ? true : false ;
\t\t\tif(Pointer == 5)
\t\t\t{
\t\t\t\tthis.Cancel.Enabled = false;
\t\t\t\tthis.Finish.Enabled = false;
\t\t\t\tthis.Next.Enabled = false;
\t\t\t\tthis.Previous.Enabled = false;
\t\t\t\tthis.DoRestore();
\t\t\t}
\t\t\tif(Pointer == 3)
\t\t\t{
\t\t\t\tthis.FillDatabaseList(this.ConnectionString());
\t\t\t}
\t\t\t
\t\t}
\t\tprivate void Next_Click(object sender, System.EventArgs e)
\t\t{
\t\t\tswitch(this.Pointer)
\t\t\t{
\t\t\t\tcase 2 :
\t\t\t\t\tif(!this.Validate_SQLServer())
\t\t\t\t\t\treturn;
break ;
\t\t\t\tcase 3:
\t\t\t\t\tif(!this.Validate_File())
\t\t\t\t\t\treturn;
break ;
\t\t\t\tcase 4:
\t\t\t\t\tif(!this.Validate_Destination())
\t\t\t\t\t\treturn;
break ;
\t\t\t}
\t\t\tthis.Navigate(true);
\t\t}
\t\tprivate void Previous_Click(object sender, System.EventArgs e)
\t\t{
\t\t\tthis.Navigate(false);
\t\t}
\t\tprivate void Finish_Click(object sender, System.EventArgs e)
\t\t{
\t\t\tthis.Close();
\t\t}
\t\tprivate void Cancel_Click(object sender, System.EventArgs e)
\t\t{
\t\t\tif(MessageBox.Show(\"Are you sure want to cancel wizard?\",\"Restore Database\",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.No)
\t\t\t\treturn;
\t\t\tthis.Close();
\t\t}
private void button1_Click(object sender, EventArgs e)
{
string name = Environment.MachineName;
string n = \"\\\" + name + this.DataBasePath.Text;
MessageBox.Show(n);
}
\t}
}
this.Icon"))); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "JET Restore"; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit(); this.SQLServerPanel.ResumeLayout(false); this.SQLServerPanel.PerformLayout(); this.FilePanel.ResumeLayout(false); this.FilePanel.PerformLayout(); this.ProgressPanel.ResumeLayout(false); this.WelcomePanel.ResumeLayout(false); this.DestinationPanel.ResumeLayout(false); this.DestinationPanel.PerformLayout(); this.panel1.ResumeLayout(false); this.panel1.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit(); this.FinishPanel.ResumeLayout(false); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { this.Windows.CheckedChanged += new EventHandler(OnChangeAuthenticationType); this.SQLServer.CheckedChanged += new EventHandler(OnChangeAuthenticationType); this.ExistingDatabase.CheckedChanged += new EventHandler(OnChangeDestination); this.NewDatabase.CheckedChanged += new EventHandler(OnChangeDestination); this.BackUp.Click += new EventHandler(this.BackUp_Click); this.Path.Click += new EventHandler(this.Path_Click); this.Navigate(true); // this.introductionPage1.IntroductionText = @"This wizard helps you restore a database in different Microsoft SQL Server database. // // // //Programmer : Phorozan@gmail.com"; } private void OnChangeAuthenticationType(object sender , EventArgs e) { bool b = !(this.Windows.Checked); this.UserName.Enabled = b; this.Password.Enabled = b; this.label4.Enabled = b; this.label5.Enabled = b; if(((RadioButton)sender).Name == "SQLServer" && ((RadioButton)sender).Checked) { this.UserName.Focus(); this.UserName.SelectAll(); } } private void OnChangeDestination(object sender , EventArgs e) { bool b = !(this.ExistingDatabase.Checked); this.DataBaseName.Enabled = b; this.DataBasePath.Enabled = b; this.label1.Enabled = b; this.label2.Enabled = b; this.Path.Enabled = b; this.DatabaseList.Enabled= !b; this.RefreshDatabases.Enabled= !b; if(((RadioButton)sender).Name == "NewDatabase" && ((RadioButton)sender).Checked) { this.DataBaseName.Focus(); this.DataBaseName.SelectAll(); } else { this.DatabaseList.Focus(); this.FillDatabaseList(this.ConnectionString()); } } private void Path_Click(object sender, System.EventArgs e) { FolderBrowserDialog f = new FolderBrowserDialog(); f.ShowNewFolderButton = true; f.Description = "Select path to create new database:"; if(System.IO.Directory.Exists(this.DataBasePath.Text)) f.SelectedPath = this.DataBasePath.Text; if (f.ShowDialog()==DialogResult.OK) this.DataBasePath.Text = f.SelectedPath; f.Dispose(); } private string ConnectionString() { string s; s = string.Format("Server={0};",this.Server.Text); if(this.Windows.Checked) s += "Trusted_Connection=True;"; else s += string.Format("UId={0};PWD={1};",this.UserName.Text,this.Password.Text); s+="Connect Timeout=1;Max Pool Size=1;Connection Lifetime=10;"; return s; } private void FillDatabaseList(string ConnectionString) { this.DatabaseList.Items.Clear(); SqlConnection connection = new SqlConnection(ConnectionString); SqlCommand cmd = connection.CreateCommand(); SqlDataReader reader; cmd.CommandText = "Select Name From sysDatabases"; try { connection.Open(); reader = cmd.ExecuteReader(); string Database=""; while(reader.Read()) { Database = reader.GetString(0); if(Database != "master" && Database != "model" && Database != "msdb" && Database != "tempdb" ) this.DatabaseList.Items.Add(Database); } reader.Close(); (reader as IDisposable).Dispose(); } catch(SqlException ex) { MessageBox.Show(ex.ToString(),"SQL Server Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); } catch(Exception ex) { MessageBox.Show(ex.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); } finally { if(connection.State == ConnectionState.Open) connection.Close(); cmd.Dispose(); connection.Dispose(); if(this.DatabaseList.Items.Count>0) this.DatabaseList.SelectedIndex = 0; } } private void RefreshDatabases_Click(object sender, System.EventArgs e) { this.FillDatabaseList(this.ConnectionString()); } // private bool CheckInput() // { // if (this.Server.Text == "") // { // MessageBox.Show("Specify SQL Server .","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); // // this.Server.Focus(); // // return false; // } // // if (this.UserName.Text == "" && this.SQLServer.Checked == true) // { // MessageBox.Show("Enter User Name.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); // // this.UserName.Focus(); // // return false; // } // // if(!System.IO.File.Exists(this.BackUpFile.Text)) // { // MessageBox.Show("Select BackUp File.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); // // this.UserName.Focus(); // // return false; // } // if (this.ExistingDatabase.Checked) // { // // if(this.DatabaseList.SelectedIndex < 0) // { // MessageBox.Show("Select Database.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); // // this.DatabaseList.Focus(); // // return false; // } // } // else // { // if(this.DataBaseName.Text=="") // { // MessageBox.Show("Enter Database Name.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); // // this.DataBaseName.Focus(); // // return false; // } // if(!System.IO.Directory.Exists(this.DataBasePath.Text)) // { // MessageBox.Show("Enter a valid path.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); // // this.DataBasePath.Focus(); // // return false; // } // } // // return true; // } private bool GetFileRestored(string ConnectionString,string DatabaseName,ref string DataFile , ref string LogFile) { SqlConnection connection = new SqlConnection(ConnectionString); SqlCommand cmd = connection.CreateCommand(); SqlDataReader reader; cmd.CommandText = this.PrepareCommand("Restore.sql",DatabaseName); try { connection.Open(); reader = cmd.ExecuteReader(); while(reader.Read()) { if(reader.GetString(2)=="D") DataFile = reader.GetString(0); else LogFile = reader.GetString(0); } reader.Close(); (reader as IDisposable).Dispose(); } catch(SqlException ex) { MessageBox.Show(ex.ToString(),"SQL Server Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); return false; } catch(Exception ex) { MessageBox.Show(ex.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); return false; } finally { if(connection.State == ConnectionState.Open) connection.Close(); cmd.Dispose(); connection.Dispose(); } return true; } private string PrepareCommand(string Name , string DatabaseName) { string Commands = this.LoadSQLFromAssembly(Name); if (Commands == null) return null; Commands = Commands.Replace("%Database%" , DatabaseName); Commands = Commands.Replace("%DatabasePath%" , this.DataBasePath.Text + "\\"); Commands = Commands.Replace("%BackUpPath%" , this.BackUpFile.Text); return Commands; } private bool ExecuteSQLCommand(string ConnectionString , string Commands) { SqlConnection connection = new SqlConnection(ConnectionString); SqlCommand cmd = connection.CreateCommand(); cmd.CommandText = Commands; try { connection.Open(); cmd.ExecuteNonQuery(); } catch(SqlException ex) { MessageBox.Show(ex.ToString(),"SQL Server Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); return false; } catch(Exception ex) { MessageBox.Show(ex.ToString(),"Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); return false; } finally { if(connection.State == ConnectionState.Open) connection.Close(); cmd.Dispose(); connection.Dispose(); } return true; } private string LoadSQLFromAssembly (string Name) { System.IO.Stream stream = this.GetType().Assembly.GetManifestResourceStream(this.GetType(),"SQL." + Name); if(stream == null) { MessageBox.Show("Internal Error occured! Close Application & try again.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error); return null; } System.IO.StreamReader reader= new System.IO.StreamReader(stream); if (reader == null) { MessageBox.Show("Internal Error occured! Close Application & try again.","Error",MessageBoxButtons.OK,MessageBoxIcon.Error); return null; } string s = reader.ReadToEnd(); reader.Close(); return s; } private void BackUp_Click(object sender, System.EventArgs e) { OpenFileDialog f = new OpenFileDialog(); f.Filter = "SQL Server BackUp Files|*.bak|All Files|*.*"; f.Title = "Open Backup File"; if(System.IO.File.Exists(this.BackUpFile.Text)) f.FileName = this.BackUpFile.Text; if(f.ShowDialog()==DialogResult.OK) { this.BackUpFile.Text = f.FileName; } f.Dispose(); } #region Validateion private bool Validate_File() { if(!System.IO.File.Exists(this.BackUpFile.Text)) { MessageBox.Show("Select BackUp File.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); this.UserName.Focus(); return false; } return true; } private bool Validate_SQLServer() { SqlConnection connection = new SqlConnection(this.ConnectionString()); try { connection.Open(); connection.Close(); return true; } catch(Exception ex) { MessageBox.Show(ex.Message , "Error",MessageBoxButtons.OK,MessageBoxIcon.Error); return false; } finally { connection.Dispose(); } } private bool Validate_Destination() { if (this.ExistingDatabase.Checked) { if(this.DatabaseList.SelectedIndex < 0) { MessageBox.Show("Select Database.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); this.DatabaseList.Focus(); return false; } } else { if(this.DataBaseName.Text=="") { MessageBox.Show("Enter Database Name.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); this.DataBaseName.Focus(); return false; } if(this.DatabaseList.Items.Contains(this.DataBaseName.Text)) { MessageBox.Show("Database already exists!","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); this.DataBaseName.Focus(); return false; } if(!System.IO.Directory.Exists(this.DataBasePath.Text)) { MessageBox.Show("Enter a valid path.","User",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); this.DataBasePath.Focus(); return false; } } return true; } #endregion private void DoRestore() { this.StatusLabel.Text = "Gathering information about operation..."; this.SetProgressBar(0); string Commands , DataFile="" , LogFile ="" , DatabaseName =""; if(this.NewDatabase.Checked) { Commands = this.PrepareCommand("CreateNewDatabase.sql" , this.DataBaseName.Text); if(Commands == null) { this.FinsihLabel.Text = "Wizard can not do operation!"; this.Navigate(true); return; } this.StatusLabel.Text = string.Format( "Creating new database with name {0} ..." , this.DataBaseName.Text) ; this.SetProgressBar(10); if(this.ExecuteSQLCommand(this.ConnectionString() , Commands)) { this.StatusLabel.Text = "Database Created Successfully!" ; } else { return; } this.SetProgressBar(40); DatabaseName = this.DataBaseName.Text; } else { this.StatusLabel.Text = "" ; DatabaseName = this.DatabaseList.SelectedItem.ToString(); } this.StatusLabel.Text += string.Format( "\nLoading {0} ..." ,@"\\BLR_11285_OCR1\ E:\Testdb.bak") ; this.SetProgressBar(50); if (this.GetFileRestored(this.ConnectionString(),DatabaseName,ref DataFile , ref LogFile)) { this.StatusLabel.Text = "Generating SQL scripts ..." ; Commands = this.PrepareCommand("RestoreFinall.sql" , DatabaseName); if(Commands == null) { this.FinsihLabel.Text = "Wizard can not do operation!"; this.Navigate(true); return; } this.StatusLabel.Text = "Finalizing SQL scripts ..." ; this.SetProgressBar(60); Commands = Commands.Replace("%OldData%" , DataFile).Replace("%OldLog%",LogFile); this.SetProgressBar(70); this.StatusLabel.Text = "Finalizing operation ..." ; this.ExecuteSQLCommand(this.ConnectionString() , Commands); this.StatusLabel.Text = "Operation successfully done." ; this.SetProgressBar(100); } this.FinsihLabel.Text = string.Format ("Restore wizard successfully finished. Result : \n\nFile {0} successfully restore to {1}.",this.BackUpFile.Text , DatabaseName); this.Navigate(true); } private void SetProgressBar(int value) { this.pbar.Value = value; Application.DoEvents(); } private int Pointer = 0; private const int MaxPages = 6; private void Navigate(bool forward) { Pointer = forward ? Pointer + 1 : ((Pointer == MaxPages) ? Pointer - 2 : Pointer - 1); this.Previous.Enabled = (Pointer == 1) ? false : true; this.Next.Enabled = (Pointer == MaxPages) ? false : true; this.Finish.Enabled = !this.Next.Enabled; this.Cancel.Enabled = this.Next.Enabled; this.WelcomePanel.Visible = (Pointer == 1 ) ? true : false; this.SQLServerPanel.Visible = (Pointer == 2 ) ? true : false; this.FilePanel.Visible = (Pointer == 3 ) ? true : false; this.DestinationPanel.Visible = (Pointer == 4 ) ? true : false; this.ProgressPanel.Visible = (Pointer == 5 ) ? true : false; this.FinishPanel.Visible = (Pointer == 6 ) ? true : false; if(Pointer == 5) { this.Cancel.Enabled = false; this.Finish.Enabled = false; this.Next.Enabled = false; this.Previous.Enabled = false; this.DoRestore(); } if(Pointer == 3) { this.FillDatabaseList(this.ConnectionString()); } } private void Next_Click(object sender, System.EventArgs e) { switch(this.Pointer) { case 2 : if(!this.Validate_SQLServer()) return; break; case 3: if(!this.Validate_File()) return; break; case 4: if(!this.Validate_Destination()) return; break; } this.Navigate(true); } private void Previous_Click(object sender, System.EventArgs e) { this.Navigate(false); } private void Finish_Click(object sender, System.EventArgs e) { this.Close(); } private void Cancel_Click(object sender, System.EventArgs e) { if(MessageBox.Show("Are you sure want to cancel wizard?","Restore Database",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.No) return; this.Close(); } private void button1_Click(object sender, EventArgs e) { string name = Environment.MachineName; string n = "\\" + name + this.DataBasePath.Text; MessageBox.Show(n); } } }
这篇关于C#Sql数据库远程恢复无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文