-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMysqlHandler.cs
67 lines (67 loc) · 3.11 KB
/
MysqlHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using System;
using System.Windows.Forms;
using System.Diagnostics;
using meh = WCell.DatabaseImportSystem.Properties.Settings;
using MySql.Data.MySqlClient;
namespace WCell.DatabaseImportSystem
{
class MysqlHandler
{
public static Process Mysql;
public static void ImportLargeSql(string filename)
{
try
{
var args = string.Format("/c mysql.exe --host={0} --user {1} --port 3306 {2} < {3}", meh.Default.MysqlHost, meh.Default.MysqlUsername, meh.Default.MysqlDatabase,filename);
if(!string.IsNullOrEmpty(meh.Default.MysqlPassword))
args = string.Format("/c mysql.exe -host={0} --user {1} --password {2} --port 3306 {3} < {4}", meh.Default.MysqlHost, meh.Default.MysqlUsername, meh.Default.MysqlPassword, meh.Default.MysqlDatabase,filename);
Mysql = new Process
{
StartInfo =
{
FileName = "cmd.exe",
Arguments = args,
RedirectStandardInput = false,
RedirectStandardOutput = false,
UseShellExecute = false,
CreateNoWindow = true
}
};
Mysql.Start();
MessageBox.Show(@"Importing Data, please wait...");
Mysql.WaitForExit();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
LogWriter.WriteLine(ex.Data + ex.Message + ex.StackTrace);
}
}
public static void Command(string command)
{
try
{
var connstring = new MySqlConnectionStringBuilder
{
Server = meh.Default.MysqlHost,
UserID = meh.Default.MysqlUsername,
Password = meh.Default.MysqlPassword,
Database = meh.Default.MysqlDatabase
};
var connection = new MySqlConnection(connstring.ToString());
LogWriter.WriteLine(@"opening connection to mysql");
connection.Open();
LogWriter.WriteLine(@"connection successful!");
var sqlcommand = new MySqlCommand(command, connection);
LogWriter.WriteLine(@"Running sql command");
sqlcommand.ExecuteNonQuery();
LogWriter.WriteLine(@"Sql Command finished.");
}
catch (Exception ex)
{
MessageBox.Show(@"An Error occured while importing the file!, log output should have more info.");
LogWriter.WriteLine(ex.Data + ex.StackTrace + ex.Message);
}
}
}
}