using System ;
using System.Collections.Generic ;
using System.Data.SqlClient ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Data ;
using System.Windows.Documents ;
using System.Windows.Forms ;
using System.Windows.Input ;
using System.Windows.Media ;
using System.Windows.Media.Imaging ;
using System.Windows.Shapes ;
namespace formula_manage.Windows
{
/// <summary>
/// SQL_BAK.xaml 的交互逻辑
/// </summary>
public partial class SQL_BAK : Window
{
public SQL_BAK ( )
{
WindowStartupLocation = WindowStartupLocation . CenterScreen ;
InitializeComponent ( ) ;
this . Closing + = Window_Closing ; //添加窗口关闭事件
}
bool SQLBAK ;
bool SQLTIME ;
int Time ;
int i ;
string SQLIP ;
string SQLNAME ;
bool SQLMOD ;
string SQLUSER ;
string SQLPASS ;
public string INIPath = Convert . ToString ( System . AppDomain . CurrentDomain . BaseDirectory ) + "formula.ini" ;
private void Window_SQLBAK ( object sender , RoutedEventArgs e ) //打开页面
{
UserClass . IniFile . IniFiles Configini = new UserClass . IniFile . IniFiles ( INIPath ) ; //读配置文件
SQLIP = Configini . IniReadvalue ( "SQL_SERVER" , "SQL1" ) ; //读配置文件
SQLNAME = Configini . IniReadvalue ( "SQL_SERVER" , "SQL2" ) ;
int . TryParse ( Configini . IniReadvalue ( "SQL_SERVER" , "SQL3" ) , out i ) ; //读数据库状态配置文件
if ( i = = 0 ) SQLMOD = false ;
else SQLMOD = true ;
SQLUSER = Configini . IniReadvalue ( "SQL_SERVER" , "SQL4" ) ;
SQLPASS = Configini . IniReadvalue ( "SQL_SERVER" , "SQL5" ) ;
SQLBAK = Boolean . Parse ( Configini . IniReadvalue ( "SQL_BAK" , "S1" ) ) ;
SQL_DISPEN_bak . Text = Configini . IniReadvalue ( "SQL_BAK" , "S2" ) ;
SQLTIME = Boolean . Parse ( Configini . IniReadvalue ( "SQL_BAK" , "S3" ) ) ;
int . TryParse ( Configini . IniReadvalue ( "SQL_BAK" , "S4" ) , out Time ) ;
if ( Time = = 0 ) SQL_TIME . Text = "日" ;
else if ( Time = = 1 ) SQL_TIME . Text = "周" ;
else if ( Time = = 2 ) SQL_TIME . Text = "月" ;
else if ( Time = = 3 ) SQL_TIME . Text = "季" ;
else if ( Time = = 4 ) SQL_TIME . Text = "年" ;
SQL_bak . IsChecked = SQLBAK ;
SQL_DISPEN_bak . IsEnabled = SQLBAK ;
SQL_DISPEN_BUT . IsEnabled = SQLBAK ;
SQL_bak_time . IsChecked = SQLTIME ;
SQL_TIME . IsEnabled = SQLTIME ;
}
private void SQLBAK_Checked ( object sender , RoutedEventArgs e ) //自定义备份地址状态
{
SQLBAK = SQL_bak . IsChecked . GetValueOrDefault ( ) ;
SQL_DISPEN_bak . IsEnabled = SQLBAK ;
SQL_DISPEN_BUT . IsEnabled = SQLBAK ;
}
private void SQL_bak_time_Checked ( object sender , RoutedEventArgs e ) //自动保存
{
SQLTIME = SQL_bak_time . IsChecked . GetValueOrDefault ( ) ;
SQL_TIME . IsEnabled = SQLTIME ;
}
private void Button_Click ( object sender , RoutedEventArgs e ) //SQL_DISPEN_bak路径选择按钮
{
FolderBrowserDialog Dilog = new FolderBrowserDialog ( ) ;
Dilog . RootFolder = Environment . SpecialFolder . MyComputer ; //打开根目录
Dilog . ShowNewFolderButton = false ; //不显示新建文件夹按钮
//
if ( Dilog . ShowDialog ( ) = = System . Windows . Forms . DialogResult . OK )
{
SQL_DISPEN_bak . Text = Dilog . SelectedPath ; //返回选择的字符串
}
}
private void Window_Closing ( object sender , System . ComponentModel . CancelEventArgs e ) //窗口关闭事件
{
UserClass . IniFile . IniFiles Configini = new UserClass . IniFile . IniFiles ( INIPath ) ;
Configini . IniWritevalue ( "SQL_BAK" , "S1" , SQL_bak . IsChecked . GetValueOrDefault ( ) . ToString ( ) ) ; //保存自定义备份地址配置
Configini . IniWritevalue ( "SQL_BAK" , "S2" , SQL_DISPEN_bak . Text ) ; //保存备份地址
Configini . IniWritevalue ( "SQL_BAK" , "S3" , SQL_bak_time . IsChecked . GetValueOrDefault ( ) . ToString ( ) ) ; //保存自动备份配置
if ( SQL_TIME . Text = = "日" ) Time = 0 ;
else if ( SQL_TIME . Text = = "周" ) Time = 1 ;
else if ( SQL_TIME . Text = = "月" ) Time = 2 ;
else if ( SQL_TIME . Text = = "季" ) Time = 3 ;
else if ( SQL_TIME . Text = = "年" ) Time = 4 ;
Configini . IniWritevalue ( "SQL_BAK" , "S4" , Time . ToString ( ) ) ; //保存自动备份周期
}
string Connstr_SC ;
private bool Backup ( string dbName , string filePath ) // 备份数据库
{
if ( ! SQLMOD ) //连接数据库测试
{
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";Trusted_Connection=SSPI" ;
}
else
{
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";User ID=" + SQLUSER + ";Password=" + SQLPASS ;
}
SqlConnection connection = new SqlConnection ( ) ;
connection . ConnectionString = Connstr_SC ;
connection . Open ( ) ;
SqlCommand command = new SqlCommand ( ) ;
command . Connection = connection ;
command . CommandText = string . Format ( "backup database {0} to disk = '{1}'" , dbName , filePath ) ;
try
{
command . ExecuteNonQuery ( ) ;
return true ;
}
catch
{
return false ;
}
finally
{
connection . Close ( ) ;
}
}
private bool Restore ( string dbName , string filePath ) // 还原数据库
{
if ( ! SQLMOD ) //连接数据库测试
{
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";Trusted_Connection=SSPI" ;
}
else
{
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";User ID=" + SQLUSER + ";Password=" + SQLPASS ;
}
SqlConnection connection = new SqlConnection ( ) ;
connection . ConnectionString = Connstr_SC ;
connection . Open ( ) ;
SqlCommand command = new SqlCommand ( ) ;
command . Connection = connection ;
command . CommandText = string . Format ( "select spid from sysprocesses,sysdatabases where sysprocesses.dbid=sysdatabases.dbid and sysdatabases.Name='{0}'" , dbName ) ;
// 获取当前所有连接进程
List < short > list = new List < short > ( ) ;
try
{
SqlDataReader reader = command . ExecuteReader ( ) ;
while ( reader . Read ( ) )
{
list . Add ( reader . GetInt16 ( 0 ) ) ;
}
reader . Close ( ) ;
}
catch
{
return false ;
}
finally
{
connection . Close ( ) ;
}
// 杀死当前所有连接进程
try
{
for ( int i = 0 ; i < list . Count ; i + + )
{
connection . Open ( ) ;
command = new SqlCommand ( string . Format ( "kill {0}" , list [ i ] . ToString ( ) ) , connection ) ;
command . ExecuteNonQuery ( ) ;
connection . Close ( ) ;
}
}
catch
{
return false ;
}
finally
{
connection . Close ( ) ;
}
// 还原数据库
connection . Open ( ) ;
command . CommandText = string . Format ( "restore database {0} from disk = '{1}' with replace" , dbName , filePath ) ;
try
{
command . ExecuteNonQuery ( ) ;
}
catch
{
return false ;
}
finally
{
connection . Close ( ) ;
}
return true ;
}
private async void Button_Click_1 ( object sender , RoutedEventArgs e )
{
bool ok ;
if ( ! SQLMOD ) //连接数据库测试
{
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";Trusted_Connection=SSPI" ;
}
else
{
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";User ID=" + SQLUSER + ";Password=" + SQLPASS ;
}
try
{
SqlConnection conn_SC = new SqlConnection ( Connstr_SC ) ;
await conn_SC . OpenAsync ( ) ; //连接数据库
conn_SC . Close ( ) ;
}
catch ( Exception )
{
System . Windows . MessageBox . Show ( "数据库错误" , "ERR" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ; //连接失败提示
return ;
}
if ( ( bool ) SQL_bak . IsChecked )
{
ok = Backup ( SQLNAME , SQL_DISPEN_bak . Text + "\\" + SQLNAME + ".bak" ) ; //指定地址备份
}
else
{
ok = Backup ( SQLNAME , Convert . ToString ( Environment . SpecialFolder . MyDocuments ) + SQLNAME + ".bak" ) ; //默认备份到文档文件夹
}
if ( ok )
{
System . Windows . MessageBox . Show ( "备份成功" ) ;
}
else
{
System . Windows . MessageBox . Show ( "备份失败" ) ;
}
}
private async void Button_Click_2 ( object sender , RoutedEventArgs e )
{
bool ok ;
if ( ! SQLMOD ) //连接数据库测试
{
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";Trusted_Connection=SSPI" ;
}
else
{
Connstr_SC = "server=" + SQLIP + ";database=" + SQLNAME + ";User ID=" + SQLUSER + ";Password=" + SQLPASS ;
}
try
{
SqlConnection conn_SC = new SqlConnection ( Connstr_SC ) ;
await conn_SC . OpenAsync ( ) ; //连接数据库
conn_SC . Close ( ) ;
}
catch ( Exception )
{
System . Windows . MessageBox . Show ( "数据库错误" , "ERR" , MessageBoxButton . OK , MessageBoxImage . Exclamation ) ; //连接失败提示
return ;
}
if ( ( bool ) SQL_bak . IsChecked )
{
ok = Backup ( SQLNAME , SQL_DISPEN_bak . Text + "\\" + SQLNAME + ".bak" ) ; //指定地址备份
}
else
{
ok = Backup ( SQLNAME , Convert . ToString ( Environment . SpecialFolder . MyDocuments ) + SQLNAME + ".bak" ) ; //默认备份到文档文件夹
}
if ( ok )
{
System . Windows . MessageBox . Show ( "还原成功" ) ;
}
else
{
System . Windows . MessageBox . Show ( "还原失败" ) ;
}
}
}
}