|
|
|
@ -25,6 +25,7 @@ namespace formula_manage.Windows |
|
|
|
{ |
|
|
|
public SQL_BAK() |
|
|
|
{ |
|
|
|
System.Windows.MessageBox.Show("注意此功能仅作用与本地数据库(Dispensing)对于远端数据库的操作可能失败如果需要继续操作单击确定", "SQL", MessageBoxButton.OK, MessageBoxImage.Exclamation); //连接失败提示
|
|
|
|
WindowStartupLocation = WindowStartupLocation.CenterScreen; |
|
|
|
InitializeComponent(); |
|
|
|
this.Closing += Window_Closing; //添加窗口关闭事件
|
|
|
|
@ -87,7 +88,7 @@ namespace formula_manage.Windows |
|
|
|
private void Button_Click(object sender, RoutedEventArgs e) //SQL_DISPEN_bak路径选择按钮
|
|
|
|
{ |
|
|
|
FolderBrowserDialog Dilog = new FolderBrowserDialog(); |
|
|
|
Dilog.RootFolder = Environment.SpecialFolder.MyComputer; //打开根目录
|
|
|
|
Dilog.SelectedPath = SQL_DISPEN_bak.Text; //打开目录
|
|
|
|
Dilog.ShowNewFolderButton = false; //不显示新建文件夹按钮
|
|
|
|
//
|
|
|
|
if (Dilog.ShowDialog() == System.Windows.Forms.DialogResult.OK ) |
|
|
|
@ -155,8 +156,8 @@ namespace formula_manage.Windows |
|
|
|
{ |
|
|
|
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";User ID=" + SQLUSER + ";Password=" + SQLPASS; |
|
|
|
} |
|
|
|
SqlConnection connection = new SqlConnection(); |
|
|
|
connection.ConnectionString = Connstr_SC; |
|
|
|
SqlConnection connection = new SqlConnection(Connstr_SC); |
|
|
|
// connection.ConnectionString = Connstr_SC;
|
|
|
|
connection.Open(); |
|
|
|
SqlCommand command = new SqlCommand(); |
|
|
|
command.Connection = connection; |
|
|
|
@ -180,28 +181,28 @@ namespace formula_manage.Windows |
|
|
|
{ |
|
|
|
connection.Close(); |
|
|
|
} |
|
|
|
// 杀死当前所有连接进程
|
|
|
|
try |
|
|
|
{ |
|
|
|
// 停止当前所有连接进程
|
|
|
|
// try
|
|
|
|
// {
|
|
|
|
for (int i = 0; i < list.Count; i++) |
|
|
|
{ |
|
|
|
connection.Open(); |
|
|
|
command = new SqlCommand(string.Format("kill {0}", list[i].ToString()), connection); |
|
|
|
command = new SqlCommand(string.Format("use master kill {0}", list[i].ToString()), connection); |
|
|
|
command.ExecuteNonQuery(); |
|
|
|
connection.Close(); |
|
|
|
} |
|
|
|
} |
|
|
|
catch |
|
|
|
{ |
|
|
|
return false; |
|
|
|
} |
|
|
|
finally |
|
|
|
{ |
|
|
|
connection.Close(); |
|
|
|
} |
|
|
|
// }
|
|
|
|
// catch
|
|
|
|
// {
|
|
|
|
// return false;
|
|
|
|
// }
|
|
|
|
// finally
|
|
|
|
// {
|
|
|
|
// connection.Close();
|
|
|
|
// }
|
|
|
|
// 还原数据库
|
|
|
|
connection.Open(); |
|
|
|
command.CommandText = string.Format("restore database {0} from disk = '{1}' with replace", dbName, filePath); |
|
|
|
command.CommandText = string.Format("use master restore database {0} from disk = '{1}' with replace", dbName, filePath); |
|
|
|
try |
|
|
|
{ |
|
|
|
command.ExecuteNonQuery(); |
|
|
|
@ -220,6 +221,7 @@ namespace formula_manage.Windows |
|
|
|
private async void Button_Click_1(object sender, RoutedEventArgs e) |
|
|
|
{ |
|
|
|
bool ok; |
|
|
|
System.DateTime.Now.ToString("yyyy-MM-dd_HH_mm_ss"); |
|
|
|
|
|
|
|
if (!SQLMOD) //连接数据库测试
|
|
|
|
{ |
|
|
|
@ -244,11 +246,11 @@ namespace formula_manage.Windows |
|
|
|
|
|
|
|
if ((bool)SQL_bak.IsChecked) |
|
|
|
{ |
|
|
|
ok = Backup(SQLNAME, SQL_DISPEN_bak.Text + "\\" + SQLNAME + ".bak"); //指定地址备份
|
|
|
|
ok = Backup(SQLNAME, SQL_DISPEN_bak.Text + "\\" + SQLNAME + System.DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".bak"); //指定地址备份
|
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
ok = Backup(SQLNAME, Convert.ToString(Environment.SpecialFolder.MyDocuments) + SQLNAME + ".bak"); //默认备份到文档文件夹
|
|
|
|
ok = Backup(SQLNAME, "C:\\SUNLIGHT\\" + SQLNAME + System.DateTime.Now.ToString("yyyy_MM_dd_HHmmss") + ".bak"); //默认备份到文档文件夹
|
|
|
|
} |
|
|
|
|
|
|
|
if (ok) |
|
|
|
@ -263,8 +265,6 @@ namespace formula_manage.Windows |
|
|
|
|
|
|
|
private async void Button_Click_2(object sender, RoutedEventArgs e) |
|
|
|
{ |
|
|
|
bool ok; |
|
|
|
|
|
|
|
if (!SQLMOD) //连接数据库测试
|
|
|
|
{ |
|
|
|
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";Trusted_Connection=SSPI"; |
|
|
|
@ -286,7 +286,33 @@ namespace formula_manage.Windows |
|
|
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
OpenFileDialog openFileDialog = new OpenFileDialog(); |
|
|
|
openFileDialog.Title = "还原数据库"; |
|
|
|
openFileDialog.Filter = "备份文件(*.bak)|*.bak"; |
|
|
|
//openFileDialog.RestoreDirectory = true;
|
|
|
|
if ((bool)SQL_bak.IsChecked) |
|
|
|
{ |
|
|
|
openFileDialog.InitialDirectory = SQL_DISPEN_bak.Text; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
openFileDialog.InitialDirectory = "C:\\SUNLIGHT\\"; |
|
|
|
} |
|
|
|
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) |
|
|
|
{ |
|
|
|
bool ok = Restore(SQLNAME, openFileDialog.FileName); |
|
|
|
if (ok) |
|
|
|
{ |
|
|
|
System.Windows.MessageBox.Show("还原成功"); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
System.Windows.MessageBox.Show("还原失败"); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* if ((bool)SQL_bak.IsChecked) |
|
|
|
{ |
|
|
|
ok = Backup(SQLNAME, SQL_DISPEN_bak.Text + "\\" + SQLNAME + ".bak"); //指定地址备份
|
|
|
|
} |
|
|
|
@ -302,8 +328,8 @@ namespace formula_manage.Windows |
|
|
|
else |
|
|
|
{ |
|
|
|
System.Windows.MessageBox.Show("还原失败"); |
|
|
|
} */ |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|