Merge branch 'master' of http://www.scserverddns.top:13000/sc/formula_manage
This commit is contained in:
commit
52f788088b
1 file changed
+184
-9
+184
-9
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -32,6 +33,7 @@ namespace formula_manage.Windows
|
||||
bool SQLBAK;
|
||||
bool SQLTIME;
|
||||
int Time;
|
||||
int i;
|
||||
string SQLIP;
|
||||
string SQLNAME;
|
||||
bool SQLMOD;
|
||||
@@ -46,7 +48,9 @@ namespace formula_manage.Windows
|
||||
|
||||
SQLIP = Configini.IniReadvalue("SQL_SERVER", "SQL1"); //读配置文件
|
||||
SQLNAME = Configini.IniReadvalue("SQL_SERVER", "SQL2");
|
||||
SQLMOD = Boolean.Parse( Configini.IniReadvalue("SQL_SERVER", "SQL3"));
|
||||
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");
|
||||
|
||||
@@ -109,26 +113,197 @@ namespace formula_manage.Windows
|
||||
|
||||
string Connstr_SC;
|
||||
|
||||
private void Button_Click_1(object sender, RoutedEventArgs e)
|
||||
private bool Backup(string dbName, string filePath) // 备份数据库
|
||||
{
|
||||
if (SQLMOD) //连接数据库测试
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void Button_Click_2(object sender, RoutedEventArgs e)
|
||||
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("还原失败");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user