/// <summary> /// 创建数据库表 /// </summary> private void CreateTables() { try { InitFromFile(); if (listSQL.Count > 0) { for (int i = 0; i < listSQL.Count; i++) { string sql = "create table if not exists " + listSQL[i].tableName + "( " + listSQL[i].columnName[0] + " " + listSQL[i].dataTypes[0] + " not null primary key auto_increment,"; for (int j = 1; j < listSQL[i].columnName.Length; j++) { sql += listSQL[i].columnName[j] + " " + listSQL[i].dataTypes[j] + " ,"; } if (!listSQL[i].unique.Equals("")) { sql += listSQL[i].unique + " );"; } else { sql = sql.Remove(sql.Length - 1, 1); sql += " );"; } DataToolsQueueMsg queuein_SQL = new DataToolsQueueMsg(); queuein_SQL.type = DataBaseType.ExecuteSQL; queuein_SQL.sql = sql; ListQueue.Enqueue(queuein_SQL); } } } catch (Exception ex) { DataToolsUIMsg msg = new DataToolsUIMsg(); msg.type = "exception"; msg.ex = ex; OnUiShowMessage(msg); } }
/// <summary> /// 发布事件 /// </summary> /// <param name="message"></param> protected void OnUiShowMessage(DataToolsUIMsg message) { SendMessageEvent?.Invoke(message); }
/// <summary> /// 队列处理线程,不停进行队列处理,直到通知线程结束 /// </summary> private void ThreadReadQueue() { while (!STOP_THREAD) { if (ListQueue.Count > 0) { try { //从队列中取出 DataToolsQueueMsg queueinfo = ListQueue.Dequeue(); switch (queueinfo.type) { case DataBaseType.Insert: myDataHelper.Insert(queueinfo.message, queueinfo.values); break; case DataBaseType.Delete: myDataHelper.Delete(queueinfo.message, queueinfo.whereName, queueinfo.whereValue); break; case DataBaseType.Update: myDataHelper.Update(queueinfo.message, queueinfo.parameters, queueinfo.values, queueinfo.whereName, queueinfo.whereValue); break; case DataBaseType.Query: DataToolsUIMsg msg = new DataToolsUIMsg(); msg.dataSet = myDataHelper.Query(queueinfo.message, queueinfo.parameters, queueinfo.whereName, queueinfo.whereValue); msg.type = queueinfo.marks; OnUiShowMessage(msg); break; case DataBaseType.CreateTable: myDataHelper.CreateTable(queueinfo.message, queueinfo.parameters); break; case DataBaseType.CreateDataBase: myDataHelper.CreateDataBase(queueinfo.message, queueinfo.parameters); break; case DataBaseType.ExecuteSQL: myDataHelper.ExecuteSQL(queueinfo.sql); break; case DataBaseType.ExecuteSQLToDataSet: DataToolsUIMsg resultExecute = new DataToolsUIMsg(); resultExecute.type = queueinfo.marks; resultExecute.dataSet = myDataHelper.ExecuteSQLToDataSet(queueinfo.sql); OnUiShowMessage(resultExecute); break; case DataBaseType.ExecuteSQLFromDataBase: DataToolsUIMsg resultExecute_data = new DataToolsUIMsg(); resultExecute_data.type = queueinfo.marks; resultExecute_data.dataSet = myDataHelper.ExecuteSQLToDataSet_FromDataBase(queueinfo.whereName, queueinfo.sql); OnUiShowMessage(resultExecute_data); break; case DataBaseType.UpdateDataTable: myDataHelper.BatchUpdate(queueinfo.dataTable); break; case DataBaseType.InsertDataTable: myDataHelper.BulkInsert(queueinfo.dataTable); break; case DataBaseType.GetColumnName: //获取表中的字段 //DataToolsUIMsg resultColumn = new DataToolsUIMsg(); //resultColumn.valuesList = myDataHelper.GetColumnName(queueinfo.message); //resultColumn.type = queueinfo.marks; //OnUiShowMessage(resultColumn); break; case DataBaseType.InsertOrUpdate: myDataHelper.InsertOrUpdate(queueinfo.message, queueinfo.values); break; case DataBaseType.ShowTables: DataToolsUIMsg resultTables = new DataToolsUIMsg(); resultTables.type = queueinfo.marks; resultTables.dataSet = myDataHelper.GetAllTables(queueinfo.whereName); OnUiShowMessage(resultTables); break; case DataBaseType.ShowDataBase: //显示所有的数据库列表 DataToolsUIMsg resultDatas = new DataToolsUIMsg(); resultDatas.type = queueinfo.marks; resultDatas.dataSet = myDataHelper.GetAllDataBase(); OnUiShowMessage(resultDatas); break; case DataBaseType.ShowTableColumns: //显示数据表中的所有列名 DataToolsUIMsg resultColumns = new DataToolsUIMsg(); resultColumns.type = queueinfo.marks; resultColumns.dataSet = myDataHelper.GetAllTableColumns(queueinfo.whereName, queueinfo.whereValue); OnUiShowMessage(resultColumns); break; } } catch (Exception ex) { DataToolsUIMsg msg = new DataToolsUIMsg(); msg.type = "exception"; msg.message = ex.ToString(); OnUiShowMessage(msg); } } } }
/// <summary> /// 从文件初始化建表语句 /// </summary> public void InitFromFile() { if (File.Exists(filePath)) { FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None); StreamReader rs = new StreamReader(fs); try { while (!rs.EndOfStream) { string temp = rs.ReadLine().Replace(" ", ""); if (!temp.Contains("!") && !temp.Equals("")) { SQLStruct sqlStruct = new SQLStruct(); sqlStruct.tableName = temp; temp = rs.ReadLine().Replace(" ", "");; string[] str = System.Text.RegularExpressions.Regex.Split(temp, @"[;]+", System.Text.RegularExpressions.RegexOptions.IgnoreCase); sqlStruct.columnName = new string[str.Length]; for (int i = 0; i < str.Length; i++) { sqlStruct.columnName[i] = str[i]; } temp = rs.ReadLine().Replace(" ", "");; string[] str2 = System.Text.RegularExpressions.Regex.Split(temp, @"[;]+", System.Text.RegularExpressions.RegexOptions.IgnoreCase); sqlStruct.dataTypes = new string[str2.Length]; for (int i = 0; i < str2.Length; i++) { sqlStruct.dataTypes[i] = str2[i]; } temp = rs.ReadLine().Replace(" ", "");; if (!temp.Contains("*")) { sqlStruct.unique = temp; } else { sqlStruct.unique = ""; } listSQL.Add(sqlStruct); } } } catch (Exception ex) { DataToolsUIMsg msg = new DataToolsUIMsg(); msg.type = "exception"; msg.message = ex.ToString(); OnUiShowMessage(msg); } finally { rs.Close(); fs.Close(); } } }