private void target_BeforeExpand(object sender, TreeViewCancelEventArgs e) { if (e.Node.Nodes.Count == 1 && e.Node.LastNode.Text == "...reading") { e.Node.Nodes.Clear(); targetStruct = db.getAllFieldsStruct(e.Node.Text); for (int i = 0; i < targetStruct.Count; i++) { TreeNode subTn = new TreeNode(); subTn.Text = targetStruct[i].name; subTn.ToolTipText = targetStruct[i].realType; if (targetStruct[i].Key != "") { if (targetStruct[i].Key != "PRI") { subTn.ImageIndex = 2; subTn.SelectedImageIndex = 2; } else { subTn.ImageIndex = 3; subTn.SelectedImageIndex = 3; } } e.Node.Nodes.Add(subTn); } } }
private void init(MysqlHandler db, string source) { this.tableName = source; if (!db.isConnected()) { disConnect = true; db.connect(); } sourceStruct = db.getAllFieldsStruct(source); isInit = true; redrawComponents(); }
public String alterTableFromSource(MysqlHandler connection, string sourceTable, MysqlHandler targetConnection, Boolean addOnly) { bool disconnecting = false; bool targetDisconnecting = false; if (!connection.isConnected()) { disconnecting = true; connection.connect(); } if (!targetConnection.isConnected()) { targetDisconnecting = true; targetConnection.connect(); } string createSql = connection.getTableCreationString(sourceTable); List <MysqlStruct> sourceStruct = connection.getAllFieldsStruct(sourceTable); List <MysqlStruct> targetStruct = targetConnection.getAllFieldsStruct(sourceTable); String alterSql = getAlterTableSql(sourceStruct, targetStruct, addOnly); if (alterSql.Length > 0) { alterSql = "ALTER TABLE `" + sourceTable + "` " + alterSql; } if (disconnecting) { connection.disConnect(); } if (targetDisconnecting) { targetConnection.disConnect(); } targetConnection.resetTableList(); return(alterSql); }
public Int64 copyValues(MysqlHandler connection, string sourceTable, Int64 start, MysqlHandler targetConnection) { Int64 count = 0; Int64 refresher = 0; List <MysqlStruct> Struct = connection.getAllFieldsStruct(sourceTable); string addToField = sourceTable + "."; if (showProgress && msg.Visible) { msg.progressBar.Maximum = (int)this.copyRowLimit + 1; } if (connection.isConnected()) { MySql.Data.MySqlClient.MySqlDataReader Reader = connection.sql_select("SELECT * FROM " + sourceTable + " LIMIT " + start + "," + this.copyRowLimit); if (Reader != null) { //if (Reader.RecordsAffected==-1) return 0; while (Reader.Read()) { count++; refresher++; string insertSql = "INSERT INTO " + sourceTable + " SET "; string add = ""; for (int i = 0; i < Reader.FieldCount; i++) { if (refresher > 1000) { if (showProgress && msg.Visible) { msg.msg.Text = "Copying Table " + sourceTable + " block: " + start + "/" + count; msg.progressBar.Value = (int)count; msg.progressBar.Refresh(); msg.Refresh(); msg.Focus(); } refresher = 0; } System.Type fType = Reader.GetFieldType(i); Object chk = null; try { chk = Reader.GetValue(i); } catch (Exception) { //throw; } if (chk != null) { int hash = chk.GetHashCode(); string dd = chk.ToString(); if (chk != null && dd != "") { switch (fType.Name) { case "Int32": case "UInt32": case "Int64": case "UInt64": Int64 val = Reader.GetInt64(i); insertSql += add + addToField + Struct[i].name + " = " + val; break; case "Single": case "Float": float fVal = Reader.GetFloat(i); insertSql += add + addToField + Struct[i].name + " = '" + fVal.ToString().Replace(',', '.') + "'"; break; case "Double": double dVal = Reader.GetDouble(i); insertSql += add + addToField + Struct[i].name + " = '" + dVal.ToString().Replace(',', '.') + "'"; break; case "DateTime": DateTime dt = Reader.GetDateTime(i); String dtValue = zeroVal(dt.Year) + "-" + zeroVal(dt.Month) + "-" + zeroVal(dt.Day) + " " + dt.TimeOfDay; insertSql += add + addToField + Struct[i].name + " = '" + dtValue + "'"; break; case "Boolean": bool bval = Reader.GetBoolean(i); if (bval) { insertSql += add + addToField + Struct[i].name + " = 1"; } else { insertSql += add + addToField + Struct[i].name + " = 0"; } break; default: string sval = Reader.GetString(i).Replace("'", @"\'"); insertSql += add + addToField + Struct[i].name + " = '" + sval + "'"; break; } add = ","; } } } //returnValue += insertSql + ";\n"; targetConnection.sql_update(insertSql); } Reader.Close(); } } return(count); }
public Int64 getCopyStatements(MysqlHandler connection, string sourceTable, Int64 start, MysqlHandler targetConnection, List <string> queryList, bool asFullInsert) { Int64 count = 0; Int64 refresher = 0; List <MysqlStruct> Struct = connection.getAllFieldsStruct(sourceTable); string addToField = sourceTable + "."; if (showProgress && msg.Visible) { msg.progressBar.Maximum = (int)this.copyRowLimit + 1; } if (connection.isConnected()) { MySql.Data.MySqlClient.MySqlDataReader Reader = connection.sql_select("SELECT * FROM " + sourceTable + " LIMIT " + start + "," + this.copyRowLimit); if (Reader != null) { //if (Reader.RecordsAffected==-1) return 0; bool startRow = true; string fullInsert = "INSERT INTO " + sourceTable; string valuesExport = ""; string valuesExportAdd = ""; string fields = ""; string fieldsAdd = "("; string valuesSeperator = ""; while (Reader.Read()) { count++; refresher++; string insertSql = "INSERT INTO " + sourceTable + " SET "; string add = ""; valuesExportAdd = valuesSeperator + "("; for (int i = 0; i < Reader.FieldCount; i++) { if (refresher > 1000) { if (showProgress && msg.Visible) { msg.msg.Text = "get copy statement for Table " + sourceTable + " block: " + start + "/" + count; msg.progressBar.Value = (int)count; msg.progressBar.Refresh(); msg.Refresh(); msg.Focus(); } refresher = 0; } System.Type fType = Reader.GetFieldType(i); Object chk = null; try { chk = Reader.GetValue(i); } catch (Exception) { //throw; } if (asFullInsert) { if (startRow) { fields += fieldsAdd + "`" + Struct[i].name + "`"; fieldsAdd = ","; } } if (chk != null) { int hash = chk.GetHashCode(); string dd = chk.ToString(); if (chk != null && dd != "") { switch (fType.Name) { case "Int32": case "UInt32": case "Int64": case "UInt64": Int64 val = Reader.GetInt64(i); insertSql += add + addToField + Struct[i].name + " = " + val; if (asFullInsert) { valuesExport += valuesExportAdd + val; } break; case "Single": case "Float": float fVal = Reader.GetFloat(i); insertSql += add + addToField + Struct[i].name + " = '" + fVal.ToString().Replace(',', '.') + "'"; if (asFullInsert) { valuesExport += valuesExportAdd + "'" + fVal.ToString().Replace(',', '.') + "'"; } break; case "Double": double dVal = Reader.GetDouble(i); insertSql += add + addToField + Struct[i].name + " = '" + dVal.ToString().Replace(',', '.') + "'"; if (asFullInsert) { valuesExport += valuesExportAdd + "'" + dVal.ToString().Replace(',', '.') + "'"; } break; case "DateTime": DateTime dt = Reader.GetDateTime(i); String dtValue = zeroVal(dt.Year) + "-" + zeroVal(dt.Month) + "-" + zeroVal(dt.Day) + " " + dt.TimeOfDay; insertSql += add + addToField + Struct[i].name + " = '" + dtValue + "'"; if (asFullInsert) { valuesExport += valuesExportAdd + "'" + dtValue + "'"; } break; case "Boolean": bool bval = Reader.GetBoolean(i); if (bval) { insertSql += add + addToField + Struct[i].name + " = 1"; } else { insertSql += add + addToField + Struct[i].name + " = 0"; } if (asFullInsert) { if (bval) { valuesExport += valuesExportAdd + "1"; } else { valuesExport += valuesExportAdd + "0"; } } break; default: string stringVal = Reader.GetString(i); /* * if (stringVal.Contains(@"\")) * { * stringVal.Replace(@"\", @"\\"); * } */ string sval = stringVal.Replace("'", @"\'"); sval = sval.Replace("‘", "?"); insertSql += add + addToField + Struct[i].name + " = '" + sval + "'"; if (asFullInsert) { valuesExport += valuesExportAdd + "'" + sval + "'"; } break; } add = ","; valuesExportAdd = ","; } else { valuesExport += valuesExportAdd + "NULL"; } } else { valuesExport += valuesExportAdd + "NULL"; } valuesSeperator = ","; } valuesExport += ")"; startRow = false; if (!asFullInsert) { queryList.Add(insertSql); } } if (asFullInsert && count > 0) { string exportSql = fullInsert + fields + ") VALUES " + valuesExport + ";"; queryList.Add(exportSql); } Reader.Close(); } } return(count); }