123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873 |
- using MySql.Data.MySqlClient;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Data;
- using System.Data.OleDb;
- using System.Data.SqlClient;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace BaseLibDataProcess
- {
- public class ClsMysql
- {
- private readonly string IP = null;
- private readonly string User = null;
- private readonly string Password = null;
- private readonly string dbName = null;
- private readonly string Port = null;
- private readonly string Charset = "utf8";
- public ClsMysql(string ip, string port, string user, string pwd, string dbname,string charset= "utf8")
- {
- IP = ip;
- Port = port;
- User = user;
- Password = pwd;
- dbName = dbname;
- Charset = charset;
- }
- public bool CreateDataBase()
- {
- try
- {
- if (!ExistDatabase(dbName.ToLower()))
- {
- if (Create_database(dbName.ToLower()))
- {
- return true;
- }
- }
- else
- {
- return true;
- }
- return false;
- }
- catch (Exception ex)
- {
- MessageBox.Show($"创建数据库{dbName}失败\r\nFailed to create database\r\n{ex.Message}", "Error");
- return false;
- }
- }
- public MySqlConnection GetMySqlConnector(string m_userid, string m_password)
- {
- //string strcon = string.Format("Data Source={2};Persist Security Info=yes;UserId={0}; PWD={1};port={3}", m_userid, m_password, IP, Port,dbName);
- string strcon = $"Data Source={IP};Persist Security Info=yes;UserId={m_userid}; PWD={m_password};port={Port}";
- MySqlConnection con = new MySqlConnection(strcon);
- return con;
- }
- public bool ExistDatabase(string basename)
- {
- bool flag = false;
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- MySqlDataReader dr = null;
- StringBuilder str_builder = new StringBuilder();
- string str = "select * from information_schema.SCHEMATA where SCHEMA_NAME=";
- str_builder.Append(str);
- str_builder.Append("'");
- str_builder.Append(basename);
- str_builder.Append("'");
- string sql_str = str_builder.ToString();
- try
- {
- con = GetMySqlConnector(User, Password);
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(sql_str, con);
- dr = cmd.ExecuteReader();
- while (dr.Read())
- {
- if (dr.GetString(1).ToString() == basename)
- {
- flag = true;
- break;
- }
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"{ex.Message}", "Database Error");
- }
- finally
- {
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (con != null && con.State != ConnectionState.Open)
- {
- con.Close();
- }
- con.Dispose();
- }
- return flag;
- }
- public bool Create_database(string basename)
- {
- bool result = false;
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- string sql_str = "Create Database " + basename;
- try
- {
- con = GetMySqlConnector(User, Password);
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(sql_str, con);
- cmd.ExecuteNonQuery();
- result = true;
- }
- catch (Exception ex)
- {
- MessageBox.Show($"创建数据库失败\r\n{ex.Message}","Error");
- result = false;
- }
- finally
- {
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (con != null && con.State != ConnectionState.Open)
- {
- con.Close();
- }
- con.Dispose();
- }
- return result;
- }
- public MySqlConnection GetMySqlConnector()
- {
- string m_str_con = string.Format("server={0};user id={1}; password={2};database={3};port={4};charset={5}", IP, User, Password, dbName, Port, Charset);
- MySqlConnection con = new MySqlConnection(m_str_con);
- return con;
- }
- public bool DeleteRow(string strTable,string strItem,string strValue)
- {
- bool result = false;
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- try
- {
- StringBuilder str_builder = new StringBuilder();
- str_builder.Append("delete from ");
- str_builder.Append(strTable);
- str_builder.Append($" where {strItem}=");
- str_builder.Append("'");
- str_builder.Append(strValue);
- str_builder.Append("'");
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(str_builder.ToString(), con);
- cmd.ExecuteNonQuery();
- result = true;
- }
- catch(Exception ex)
- {
- MessageBox.Show($"删除数据库数据失败\r\nFailed to delete the data of database\r\n{ex.Message}", "Error");
- }
- finally
- {
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (con != null && con.State != ConnectionState.Open)
- {
- con.Close();
- }
- con.Dispose();
- }
- return result;
- }
- public int Getrecords(string m_str)
- {
- int result = -1;
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- try
- {
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(m_str, con);
- result = Convert.ToInt32(cmd.ExecuteScalar());
- }
- catch
- {
- }
- finally
- {
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (con != null && con.State != ConnectionState.Open)
- {
- con.Close();
- }
- con.Dispose();
- }
- return result;
- }
- //public bool InsertData(string tablename, Dictionary<int, ContentInfo> _dic)
- //{
- // bool result = false;
- // MySqlConnection con = null;
- // MySqlCommand cmd = null;
- // try
- // {
- // int m = _dic.Count;
- // StringBuilder str_builder = new StringBuilder();
- // str_builder.Append("Insert into ");
- // str_builder.Append(tablename);
- // str_builder.Append(" (");
- // for (int i = 0; i < m; i++)
- // {
- // ContentInfo m_contentInfo = null;
- // if (_dic.TryGetValue(i, out m_contentInfo))
- // {
- // str_builder.Append(m_contentInfo.str_field);
- // if (i < (m - 1))
- // {
- // str_builder.Append(",");
- // }
- // if (i == (m - 1))
- // {
- // str_builder.Append(") ");
- // }
- // }
- // }
- // str_builder.Append(" values( ");
- // for (int i = 0; i < m; i++)
- // {
- // ContentInfo m_contentInfo = null;
- // if (_dic.TryGetValue(i, out m_contentInfo))
- // {
- // str_builder.Append("'");
- // str_builder.Append(m_contentInfo.obj_content);
- // str_builder.Append("'");
- // if (i == (m - 1))
- // {
- // str_builder.Append(" )");
- // }
- // if (i < (m - 1))
- // {
- // str_builder.Append(",");
- // }
- // }
- // }
- // string m_str = str_builder.ToString();
- // con = GetMySqlConnector();
- // if (con.State == ConnectionState.Closed)
- // {
- // con.Open();
- // }
- // cmd = new MySqlCommand(m_str, con);
- // cmd.ExecuteNonQuery();
- // result = true;
- // }
- // catch (Exception ex)
- // {
- // MessageBox.Show($"插入数据到数据库失败{ex.Message}", "Error");
- // // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:InsertData");
- // }
- // finally
- // {
- // if (cmd != null)
- // {
- // cmd.Dispose();
- // }
- // if (con != null && con.State != ConnectionState.Open)
- // {
- // con.Close();
- // }
- // con.Dispose();
- // }
- // return result;
- //}
- public bool InsertData(string tablename, string strItem,string strValue)
- {
- bool result = false;
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- try
- {
- StringBuilder str_builder = new StringBuilder();
- str_builder.Append("Insert into ");
- str_builder.Append(tablename);
- str_builder.Append(" (");
- str_builder.Append(strItem);
- str_builder.Append(") ");
- str_builder.Append(" values( ");
- for (int i = 0; i < strValue.Split(',').Length; i++)
- {
- if (i== strValue.Split(',').Length-1)
- {
- str_builder.Append($"'{strValue.Split(',')[i]}'");
- }
- else
- {
- str_builder.Append($"'{strValue.Split(',')[i]}',");
- }
-
- }
- str_builder.Append(" )");
- string m_str = str_builder.ToString();
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(m_str, con);
- cmd.ExecuteNonQuery();
- result = true;
- }
- catch (Exception ex)
- {
- MessageBox.Show($"插入数据到数据库失败\r\nFailed to insert data to database\r\n{ex.Message}", "Error");
- // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:InsertData");
- }
- finally
- {
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (con != null && con.State != ConnectionState.Open)
- {
- con.Close();
- }
- con.Dispose();
- }
- return result;
- }
- //public bool UpdateData(string tablename, Dictionary<int, ContentInfo> _dic, ContentInfo _condition)
- //{
- // bool result = false;
- // MySqlConnection con = null;
- // MySqlCommand cmd = null;
- // try
- // {
- // int m = _dic.Count;
- // StringBuilder str_builder = new StringBuilder();
- // str_builder.Append("update ");
- // str_builder.Append(tablename);
- // str_builder.Append(" set ");
- // for (int i = 0; i < m; i++)
- // {
- // ContentInfo m_contentInfo = null;
- // if (_dic.TryGetValue(i, out m_contentInfo))
- // {
- // str_builder.Append(m_contentInfo.str_field);
- // str_builder.Append("=");
- // str_builder.Append("'");
- // str_builder.Append(m_contentInfo.obj_content);
- // str_builder.Append("'");
- // if (i < (m - 1))
- // {
- // str_builder.Append(",");
- // }
- // }
- // }
- // str_builder.Append(" where ");
- // str_builder.Append(_condition.str_field);
- // str_builder.Append("=");
- // str_builder.Append("'");
- // str_builder.Append(_condition.obj_content);
- // str_builder.Append("'");
- // string m_str = str_builder.ToString();
- // con = GetMySqlConnector();
- // if (con.State == ConnectionState.Closed)
- // {
- // con.Open();
- // }
- // cmd = new MySqlCommand(m_str, con);
- // cmd.ExecuteNonQuery();
- // result = true;
- // }
- // catch
- // {
- // // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:UpdateData");
- // }
- // finally
- // {
- // if (cmd != null)
- // {
- // cmd.Dispose();
- // }
- // if (con != null && con.State != ConnectionState.Open)
- // {
- // con.Close();
- // }
- // con.Dispose();
- // }
- // return result;
- //}
- public bool UpdateData(string tablename, string strItem,string strValue, string strKeyItem,string strKeyValue)
- {
- if (strItem.Split(',').Length!= strValue.Split(',').Length)
- {
- MessageBox.Show("更新数据库数据出错\r\nFailed to update database", "Error");
- }
- bool result = false;
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- try
- {
- StringBuilder str_builder = new StringBuilder();
- str_builder.Append("update ");
- str_builder.Append(tablename);
- str_builder.Append(" set ");
- for (int i = 0; i < strItem.Split(',').Length; i++)
- {
- str_builder.Append(strItem.Split(',')[i]);
- str_builder.Append("=");
- str_builder.Append("'");
- str_builder.Append(strValue.Split(',')[i]);
- str_builder.Append("'");
- if (i < (strItem.Split(',').Length - 1))
- {
- str_builder.Append(",");
- }
- }
- str_builder.Append(" where ");
- str_builder.Append(strKeyItem);
- str_builder.Append("=");
- str_builder.Append("'");
- str_builder.Append(strKeyValue);
- str_builder.Append("'");
- string m_str = str_builder.ToString();
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(m_str, con);
- cmd.ExecuteNonQuery();
- result = true;
- }
- catch(Exception ex)
- {
- MessageBox.Show($"更新数据库数据出错\r\nFailed to update database\r\n{ex.Message}", "Error");
- }
- finally
- {
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (con != null && con.State != ConnectionState.Open)
- {
- con.Close();
- }
- con.Dispose();
- }
- return result;
- }
- public DataTable InquireData(string strTable,string strItem,string strValue)
- {
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- MySqlDataReader dr = null;
- DataTable dt = new DataTable();
- try
- {
- StringBuilder str_builder = new StringBuilder();
- str_builder.Append("select * from ");
- str_builder.Append(strTable);
- str_builder.Append($" where {strItem}=");
- str_builder.Append("'");
- str_builder.Append(strValue);
- str_builder.Append("'");
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(str_builder.ToString(), con);
- dr = cmd.ExecuteReader();
- for (int i = 0; i < dr.FieldCount; i++)
- {
- dt.Columns.Add(dr.GetName(i));
- }
- while (dr.HasRows && dr.Read())
- {
- DataRow datarow = dt.Rows.Add();
- for (int j = 0; j < dt.Columns.Count; j++)
- {
- datarow[j] = dr[j];
- }
- }
- }
- catch
- {
- // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:ExeQuery");
- }
- finally
- {
- if (dr != null)
- {
- dr.Dispose();
- }
- if (con.State == ConnectionState.Open)
- {
- con.Close();
- }
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (dt != null)
- {
- dt.Dispose();
- }
- con.Dispose();
- }
- return dt;
- }
- public bool CreateTable(string str_tablename, string[] strfields, ushort keyIndex,ushort timeIndex=0)
- {
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- try
- {
- StringBuilder str_builder = new StringBuilder();
- str_builder.Append("Create table if not exists ");
- str_builder.Append(str_tablename);
- str_builder.Append(" ( ");
- for (int i = 0; i < strfields.Length; i++)
- {
- str_builder.Append(strfields[i]);
- str_builder.Append(" ");
- if (i == keyIndex)
- {
- str_builder.Append("varchar(100) character set utf8 primary key ");
- }
- else if (i == timeIndex)
- {
- str_builder.Append("datetime ");
- }
- else
- {
- str_builder.Append("varchar(100) character set utf8");
- }
- if (i == strfields.Length - 1)
- {
- str_builder.Append(" )");
- }
- else
- {
- str_builder.Append(", ");
- }
- }
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(str_builder.ToString(), con);
- cmd.ExecuteNonQuery();
- return true;
- // str_builder.Append("(MyRepairOrderID int auto_increment primary key,");
- //str_builder.Append("RepairOrderCode varchar(100) character set utf8 unique ,");
- //str_builder.Append("OrderCode varchar(100) character set utf8 not null,");
- //str_builder.Append("approvedname varchar(100) character set utf8,");
- //str_builder.Append("sendname varchar(100) character set utf8,");
- //str_builder.Append("state varchar(100) character set utf8,");
- //str_builder.Append("standnum varchar(100) not null,");
- //str_builder.Append("m_date date not null,");
- //str_builder.Append("lineside int");
- //str_builder.Append(" )");
- }
- catch (Exception ex)
- {
- MessageBox.Show($"创建数据库表格失败{ex.Message}", "Database Error");
- return false;
- }
- }
- public bool ExistTable(string tablename)
- {
- // string str = "select table_name from information_schema.tables where table_name='table_process'";
- StringBuilder str_builder = new StringBuilder();
- str_builder.Append("select table_name from information_schema.tables where table_name=");
- str_builder.Append("'");
- str_builder.Append(tablename);
- str_builder.Append("' and Table_SCHEMA='");
- str_builder.Append(dbName);
- str_builder.Append("'");
- string m_sql = str_builder.ToString();
- bool FLAG = false;
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- MySqlDataReader dr = null;
- try
- {
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(m_sql, con);
- dr = cmd.ExecuteReader();
- if (dr.Read() && dr.HasRows)
- {
- if (dr["table_name"].ToString() == tablename.ToLower())
- FLAG = true;
- }
- }
- catch
- {
- //Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:IS_EXIST_TABLE");
- FLAG = false;
- }
- finally
- {
- if (con.State == ConnectionState.Open)
- {
- con.Close();
- }
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (dr != null)
- {
- dr.Dispose();
- }
- con.Dispose();
- }
- return FLAG;
- }
- public bool ExistField(string tablename, string field)
- {
- bool result = false;
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- MySqlDataReader dr = null;
- try
- {
- if (ExistTable(tablename))
- {
- StringBuilder str_builder = new StringBuilder();
- str_builder.Append("describe ");
- str_builder.Append(tablename);
- string m_sql = str_builder.ToString();
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(m_sql, con);
- dr = cmd.ExecuteReader();
- while (dr.Read())
- {
- if (dr.GetString("Field").ToString() != null)
- {
- if (dr.GetString("Field").ToString() == field)
- {
- result = true;
- break;
- }
- }
- }
- }
- }
- catch
- {
- // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:IS_EXIST_FILED");
- }
- finally
- {
- if (con.State == ConnectionState.Open)
- {
- con.Close();
- }
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (dr != null)
- {
- dr.Dispose();
- }
- con.Dispose();
- }
- return result;
- }
- public bool Add_Column(string tablename, string field)
- {
- bool result = false;
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- StringBuilder str_builder = new StringBuilder();
- str_builder.Append("alter table ");
- str_builder.Append(tablename);
- str_builder.Append(" add Column ");
- str_builder.Append(field);
- str_builder.Append(" varchar(100)");
- string m_sql = str_builder.ToString();
- try
- {
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(m_sql, con);
- cmd.ExecuteNonQuery();
- result = true;
- }
- catch
- {
- // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:Add_Column");
- }
- finally
- {
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (con != null && con.State != ConnectionState.Open)
- {
- con.Close();
- }
- con.Dispose();
- }
- return result;
- }
- public DataTable GetDataBaseDT(string dtname)
- {
- DataTable dt = new DataTable();
- MySqlConnection con = null;
- MySqlDataAdapter sda = null;
- string sql_str = "select* from "+ dtname;
- try
- {
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- sda = new MySqlDataAdapter(sql_str, con);
- sda.Fill(dt);
- return dt;
- }
- catch (Exception ex)
- {
- MessageBox.Show($"获取数据库数据失败{ex.Message}", "Error");
- return null;
- }
- finally
- {
- if (sda != null)
- {
- sda.Dispose();
- }
- if (con != null && con.State != ConnectionState.Open)
- {
- con.Close();
- }
- con.Dispose();
- }
- }
- public void Get_recordsCount(ref int m, string tablename, ContentInfo m_contentinfo)
- {
- if (ExistTable(tablename))
- {
- StringBuilder str_builder = new StringBuilder();
- str_builder.Append("select count(*) from ");
- str_builder.Append(tablename);
- if (m_contentinfo != null)
- {
- str_builder.Append(" where ");
- str_builder.Append(m_contentinfo.str_field);
- str_builder.Append("=");
- str_builder.Append("'");
- str_builder.Append(m_contentinfo.obj_content);
- str_builder.Append("'");
- }
- string m_sql = str_builder.ToString();
- MySqlConnection con = null;
- MySqlCommand cmd = null;
- try
- {
- con = GetMySqlConnector();
- if (con.State == ConnectionState.Closed)
- {
- con.Open();
- }
- cmd = new MySqlCommand(m_sql, con);
- m = Convert.ToInt32(cmd.ExecuteScalar());
- }
- catch
- {
- // Gmodule.Update_exceptionmessage(string.Format("{0:mm:ss}", DateTime.Now) + "类:mySQL,方法:Get_recordsCount");
- }
- finally
- {
- if (cmd != null)
- {
- cmd.Dispose();
- }
- if (con != null && con.State != ConnectionState.Open)
- {
- con.Close();
- }
- con.Dispose();
- }
- }
- }
- }
- public class ContentInfo
- {
- public string str_field;
- public object obj_content;
- }
- }
|