diff --git a/.vs/ExportDataToFile/v16/.suo b/.vs/ExportDataToFile/v16/.suo index 64360e5..fba774e 100644 Binary files a/.vs/ExportDataToFile/v16/.suo and b/.vs/ExportDataToFile/v16/.suo differ diff --git a/App.config b/App.config index 4362a87..4cb788c 100644 --- a/App.config +++ b/App.config @@ -1,13 +1,14 @@  - - - - + +
+ + + + - @@ -27,6 +28,16 @@ + + + + + + + + + + \ No newline at end of file diff --git a/ExportDataToFile.csproj b/ExportDataToFile.csproj index b79d09e..e0192e8 100644 --- a/ExportDataToFile.csproj +++ b/ExportDataToFile.csproj @@ -56,6 +56,9 @@ packages\Npgsql.7.0.0\lib\netstandard2.0\Npgsql.dll + + packages\Oracle.ManagedDataAccess.21.8.0\lib\net462\Oracle.ManagedDataAccess.dll + packages\System.Buffers.4.5.1\lib\net461\System.Buffers.dll @@ -126,6 +129,7 @@ ShowForm.cs + @@ -148,6 +152,12 @@ ShowForm.cs + + Designer + + + Designer + SettingsSingleFileGenerator diff --git a/Form1.Designer.cs b/Form1.Designer.cs index b8d32ac..fa9571e 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -55,6 +55,14 @@ namespace ExportDataToFile this.txtSourcePWD = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.dgvExportList = new System.Windows.Forms.DataGridView(); + this.clExport = new System.Windows.Forms.DataGridViewCheckBoxColumn(); + this.clCancel = new System.Windows.Forms.DataGridViewButtonColumn(); + this.clShow = new System.Windows.Forms.DataGridViewButtonColumn(); + this.clSourceTable = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.clTargetTable = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.clTableDel = new System.Windows.Forms.DataGridViewCheckBoxColumn(); + this.clWhere = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.clMappingData = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.label11 = new System.Windows.Forms.Label(); this.dgvColumnMapping = new System.Windows.Forms.DataGridView(); this.clExpColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn(); @@ -78,14 +86,6 @@ namespace ExportDataToFile this.btnImportXML = new System.Windows.Forms.Button(); this.ofPath = new System.Windows.Forms.OpenFileDialog(); this.btnClean = new System.Windows.Forms.Button(); - this.clExport = new System.Windows.Forms.DataGridViewCheckBoxColumn(); - this.clCancel = new System.Windows.Forms.DataGridViewButtonColumn(); - this.clShow = new System.Windows.Forms.DataGridViewButtonColumn(); - this.clSourceTable = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.clTargetTable = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.clTableDel = new System.Windows.Forms.DataGridViewCheckBoxColumn(); - this.clWhere = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.clMappingData = new System.Windows.Forms.DataGridViewTextBoxColumn(); ((System.ComponentModel.ISupportInitialize)(this.dgvExportList)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dgvColumnMapping)).BeginInit(); this.SuspendLayout(); @@ -97,6 +97,7 @@ namespace ExportDataToFile this.txtTargetID.Name = "txtTargetID"; this.txtTargetID.Size = new System.Drawing.Size(135, 29); this.txtTargetID.TabIndex = 11; + this.txtTargetID.Text = "origtek"; // // label7 // @@ -115,6 +116,7 @@ namespace ExportDataToFile this.txtTargetDBName.Name = "txtTargetDBName"; this.txtTargetDBName.Size = new System.Drawing.Size(135, 29); this.txtTargetDBName.TabIndex = 10; + this.txtTargetDBName.Text = "origtekpdb"; // // label8 // @@ -133,6 +135,7 @@ namespace ExportDataToFile this.txtTargetIP.Name = "txtTargetIP"; this.txtTargetIP.Size = new System.Drawing.Size(135, 29); this.txtTargetIP.TabIndex = 8; + this.txtTargetIP.Text = "192.168.1.76"; // // label9 // @@ -177,6 +180,7 @@ namespace ExportDataToFile this.txtSourceID.Name = "txtSourceID"; this.txtSourceID.Size = new System.Drawing.Size(135, 29); this.txtSourceID.TabIndex = 5; + this.txtSourceID.Text = "origtek"; // // label4 // @@ -195,6 +199,7 @@ namespace ExportDataToFile this.txtSourceDBName.Name = "txtSourceDBName"; this.txtSourceDBName.Size = new System.Drawing.Size(135, 29); this.txtSourceDBName.TabIndex = 4; + this.txtSourceDBName.Text = "origtekpdb"; // // label3 // @@ -213,6 +218,7 @@ namespace ExportDataToFile this.txtSourceIP.Name = "txtSourceIP"; this.txtSourceIP.Size = new System.Drawing.Size(135, 29); this.txtSourceIP.TabIndex = 2; + this.txtSourceIP.Text = "192.168.1.76"; // // label2 // @@ -292,6 +298,7 @@ namespace ExportDataToFile this.txtTargetPWD.PasswordChar = '*'; this.txtTargetPWD.Size = new System.Drawing.Size(135, 29); this.txtTargetPWD.TabIndex = 12; + this.txtTargetPWD.Text = "!QAZ2wsx"; // // label6 // @@ -322,6 +329,7 @@ namespace ExportDataToFile this.txtSourcePWD.PasswordChar = '*'; this.txtSourcePWD.Size = new System.Drawing.Size(135, 29); this.txtSourcePWD.TabIndex = 6; + this.txtSourcePWD.Text = "!QAZ2wsx"; // // label5 // @@ -359,6 +367,76 @@ namespace ExportDataToFile this.dgvExportList.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvExportList_CellClick); this.dgvExportList.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvExportList_CellContentClick); // + // clExport + // + this.clExport.FalseValue = "false"; + this.clExport.Frozen = true; + this.clExport.HeaderText = "匯出"; + this.clExport.IndeterminateValue = "false"; + this.clExport.Name = "clExport"; + this.clExport.ReadOnly = true; + this.clExport.TrueValue = "true"; + this.clExport.Width = 55; + // + // clCancel + // + this.clCancel.Frozen = true; + this.clCancel.HeaderText = "清除"; + this.clCancel.Name = "clCancel"; + this.clCancel.Text = "..."; + this.clCancel.Width = 55; + // + // clShow + // + this.clShow.Frozen = true; + this.clShow.HeaderText = "顯示來源"; + this.clShow.Name = "clShow"; + this.clShow.Text = "..."; + this.clShow.Width = 80; + // + // clSourceTable + // + this.clSourceTable.DataPropertyName = "TableName"; + this.clSourceTable.Frozen = true; + this.clSourceTable.HeaderText = "來源資料表名稱"; + this.clSourceTable.Name = "clSourceTable"; + this.clSourceTable.ReadOnly = true; + this.clSourceTable.Resizable = System.Windows.Forms.DataGridViewTriState.True; + this.clSourceTable.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + this.clSourceTable.Width = 300; + // + // clTargetTable + // + this.clTargetTable.Frozen = true; + this.clTargetTable.HeaderText = "目標資料表名稱"; + this.clTargetTable.Name = "clTargetTable"; + this.clTargetTable.ReadOnly = true; + this.clTargetTable.Resizable = System.Windows.Forms.DataGridViewTriState.True; + this.clTargetTable.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + this.clTargetTable.Width = 300; + // + // clTableDel + // + this.clTableDel.Frozen = true; + this.clTableDel.HeaderText = "清除目標資料表內容"; + this.clTableDel.Name = "clTableDel"; + this.clTableDel.Width = 180; + // + // clWhere + // + this.clWhere.Frozen = true; + this.clWhere.HeaderText = "過濾條件"; + this.clWhere.Name = "clWhere"; + this.clWhere.Visible = false; + this.clWhere.Width = 200; + // + // clMappingData + // + this.clMappingData.Frozen = true; + this.clMappingData.HeaderText = "欄位對應"; + this.clMappingData.Name = "clMappingData"; + this.clMappingData.Visible = false; + // // label11 // this.label11.AutoSize = true; @@ -581,76 +659,6 @@ namespace ExportDataToFile this.btnClean.UseVisualStyleBackColor = true; this.btnClean.Click += new System.EventHandler(this.btnClean_Click); // - // clExport - // - this.clExport.FalseValue = "false"; - this.clExport.Frozen = true; - this.clExport.HeaderText = "匯出"; - this.clExport.IndeterminateValue = "false"; - this.clExport.Name = "clExport"; - this.clExport.ReadOnly = true; - this.clExport.TrueValue = "true"; - this.clExport.Width = 55; - // - // clCancel - // - this.clCancel.Frozen = true; - this.clCancel.HeaderText = "清除"; - this.clCancel.Name = "clCancel"; - this.clCancel.Text = "..."; - this.clCancel.Width = 55; - // - // clShow - // - this.clShow.Frozen = true; - this.clShow.HeaderText = "顯示來源"; - this.clShow.Name = "clShow"; - this.clShow.Text = "..."; - this.clShow.Width = 80; - // - // clSourceTable - // - this.clSourceTable.DataPropertyName = "TableName"; - this.clSourceTable.Frozen = true; - this.clSourceTable.HeaderText = "來源資料表名稱"; - this.clSourceTable.Name = "clSourceTable"; - this.clSourceTable.ReadOnly = true; - this.clSourceTable.Resizable = System.Windows.Forms.DataGridViewTriState.True; - this.clSourceTable.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; - this.clSourceTable.Width = 300; - // - // clTargetTable - // - this.clTargetTable.Frozen = true; - this.clTargetTable.HeaderText = "目標資料表名稱"; - this.clTargetTable.Name = "clTargetTable"; - this.clTargetTable.ReadOnly = true; - this.clTargetTable.Resizable = System.Windows.Forms.DataGridViewTriState.True; - this.clTargetTable.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; - this.clTargetTable.Width = 300; - // - // clTableDel - // - this.clTableDel.Frozen = true; - this.clTableDel.HeaderText = "清除目標資料表內容"; - this.clTableDel.Name = "clTableDel"; - this.clTableDel.Width = 180; - // - // clWhere - // - this.clWhere.Frozen = true; - this.clWhere.HeaderText = "過濾條件"; - this.clWhere.Name = "clWhere"; - this.clWhere.Visible = false; - this.clWhere.Width = 200; - // - // clMappingData - // - this.clMappingData.Frozen = true; - this.clMappingData.HeaderText = "欄位對應"; - this.clMappingData.Name = "clMappingData"; - this.clMappingData.Visible = false; - // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 20F); diff --git a/Form1.cs b/Form1.cs index 67e6df6..d36fa68 100644 --- a/Form1.cs +++ b/Form1.cs @@ -12,6 +12,7 @@ using System.Threading.Tasks; using System.Windows.Forms; using Npgsql; using MySqlConnector; +using Oracle.ManagedDataAccess.Client; using ManagementSystem.Utility; namespace ExportDataToFile @@ -26,14 +27,17 @@ namespace ExportDataToFile string strTargetClass = ""; //MS-SQL - SqlConnection sqlMSSourceConn = new SqlConnection(); - SqlConnection sqlMSTargetConn = new SqlConnection(); + SqlConnection sqlMSSourceConn = null; //new SqlConnection(); + SqlConnection sqlMSTargetConn = null; //new SqlConnection(); //PostgreSQL - NpgsqlConnection sqlPsgSourceConn = new NpgsqlConnection(); - NpgsqlConnection sqlPsgTargetConn = new NpgsqlConnection(); + NpgsqlConnection sqlPsgSourceConn = null; //new NpgsqlConnection(); + NpgsqlConnection sqlPsgTargetConn = null; //new NpgsqlConnection(); //MySQL - MySqlConnection sqlMySourceConn = new MySqlConnection(); - MySqlConnection sqlMyTargetConn = new MySqlConnection(); + MySqlConnection sqlMySourceConn = null; //new MySqlConnection(); + MySqlConnection sqlMyTargetConn = null; //new MySqlConnection(); + //Oracle + OracleConnection sqlOraSourceConn = null; //new OracleConnection(); + OracleConnection sqlOraTargetConn = null; //new OracleConnection(); public Form1() { @@ -67,6 +71,10 @@ namespace ExportDataToFile switch (cbSourceClass.SelectedItem.ToString().Trim()) { case "MS-SQL": + //MS-SQL + sqlMSSourceConn = new SqlConnection(); + sqlMSTargetConn = new SqlConnection(); + sqlMSSourceConn = MSSQLUtility.GetConn(strIP, strDBName, strID, strPWD, strPort); if (sqlMSSourceConn == null) { @@ -76,7 +84,11 @@ namespace ExportDataToFile strSourceClass = "MS-SQL"; Application.DoEvents(); break; - case "MySQL": + case "MySQL": + //MySQL + sqlMySourceConn = new MySqlConnection(); + sqlMyTargetConn = new MySqlConnection(); + sqlMySourceConn = MySQLUtility.GetConn(strIP, strDBName, strID, strPWD, strPort); if (sqlMySourceConn == null) { @@ -88,10 +100,25 @@ namespace ExportDataToFile Application.DoEvents(); break; case "Oracle": + //Oracle + sqlOraSourceConn = new OracleConnection(); + sqlOraTargetConn = new OracleConnection(); + + sqlOraSourceConn = OracleUtility.GetConn(strIP, strDBName, strID, strPWD, strPort); + if (sqlOraSourceConn == null) + { + MessageBox.Show("連線失敗,請查詢連線資訊"); + } + dtResult = ShowOracleTableList(sqlOraSourceConn, strDBName); + strSourceClass = "Oracle"; Application.DoEvents(); break; case "PostgreSQL": + //PostgreSQL + sqlPsgSourceConn = new NpgsqlConnection(); + sqlPsgTargetConn = new NpgsqlConnection(); + sqlPsgSourceConn = PostgreSQLUtility.GetConn(strIP, strDBName, strID, strPWD, strPort); if (sqlPsgSourceConn == null) { @@ -157,7 +184,9 @@ namespace ExportDataToFile Application.DoEvents(); break; case "Oracle": - strTargetClass = "Oracle"; + sqlOraTargetConn = OracleUtility.GetConn(strIP, strDBName, strID, strPWD, strPort); + dtResult = ShowOracleTableList(sqlOraTargetConn, strDBName); + strTargetClass = "Oracle"; Application.DoEvents(); break; case "PostgreSQL": @@ -167,7 +196,6 @@ namespace ExportDataToFile break; } - //cbTargetTable.ValueMember = "name"; cbTargetTable.DataSource = dtResult; cbTargetTable.SelectedIndex = -1; blTargetConnStatus = true; @@ -177,6 +205,7 @@ namespace ExportDataToFile else { UnLockTargetForm(); + Form1_FormClosing(null,null); } } else @@ -253,7 +282,8 @@ namespace ExportDataToFile this.clTargetColumn.DisplayMember = "Field"; break; case "Oracle": - + dtResult = ShowOracleColumnList(sqlOraTargetConn, cbTargetTable.SelectedValue.ToString()); + this.clTargetColumn.DisplayMember = "COLUMN_NAME"; break; case "PostgreSQL": dtResult = ShowPostgreSQLColumnList(sqlPsgTargetConn, cbTargetTable.SelectedValue.ToString()); @@ -314,7 +344,7 @@ namespace ExportDataToFile ExportToMySQLData(sfPath.FileName, sfPath.DefaultExt); break; case "Oracle": - + ExportToOracleData(sfPath.FileName, sfPath.DefaultExt); break; case "PostgreSQL": ExportToPostgreSQLData(sfPath.FileName, sfPath.DefaultExt); @@ -332,20 +362,25 @@ namespace ExportDataToFile private void Form1_FormClosing(object sender, FormClosingEventArgs e) { //MS-SQL - sqlMSSourceConn.Close(); + //sqlMSSourceConn.Close(); sqlMSSourceConn = null; - sqlMSTargetConn.Close(); + //sqlMSTargetConn.Close(); sqlMSTargetConn = null; //PostgreSQL - sqlPsgSourceConn.Close(); + //sqlPsgSourceConn.Close(); sqlPsgSourceConn = null; - sqlPsgTargetConn.Close(); + //sqlPsgTargetConn.Close(); sqlPsgTargetConn = null; //MySQL - sqlMySourceConn.Close(); + //sqlMySourceConn.Close(); sqlMySourceConn = null; - sqlMyTargetConn.Close(); + //sqlMyTargetConn.Close(); sqlMyTargetConn = null; + //Oracle + //sqlOraSourceConn.Close(); + sqlOraSourceConn = null; + //sqlOraTargetConn.Close(); + sqlOraTargetConn = null; } private void btnMapping_Click(object sender, EventArgs e) @@ -566,16 +601,24 @@ namespace ExportDataToFile cbTargetTable.SelectedIndex = -1; cbTargetTable.Enabled = false; - //dgvExportList.DataSource = null; - //dgvExportList.Refresh(); - //dgvColumnMapping.DataSource = null; - //dgvColumnMapping.Refresh(); - sqlMSSourceConn.Close(); - sqlMSTargetConn.Close(); - sqlPsgSourceConn.Close(); - sqlPsgTargetConn.Close(); - sqlMySourceConn.Close(); - sqlMyTargetConn.Close(); + //sqlMSSourceConn.Close(); + //sqlMSTargetConn.Close(); + //sqlPsgSourceConn.Close(); + //sqlPsgTargetConn.Close(); + //sqlMySourceConn.Close(); + //sqlMyTargetConn.Close(); + //sqlOraSourceConn.Close(); + //sqlOraTargetConn.Close(); + + sqlMSSourceConn = null; + sqlMSTargetConn = null; + sqlPsgSourceConn = null; + sqlPsgTargetConn = null; + sqlMySourceConn = null; + sqlMyTargetConn = null; + sqlOraSourceConn = null; + sqlOraTargetConn = null; + btnImportXML.Enabled = true; btnExportXML.Enabled = false; btnExport.Enabled = false; @@ -745,6 +788,7 @@ namespace ExportDataToFile dtResult = GetMySQLResult(strSelectCommand, sqlMySourceConn).Tables["Result"]; break; case "Oracle": + dtResult = GetOracleResult(strSelectCommand, sqlOraSourceConn).Tables["Result"]; break; case "PostgreSQL": dtResult = GetPostgreSQLResult(strSelectCommand.ToString(), sqlPsgSourceConn).Tables["Result"]; @@ -768,7 +812,6 @@ namespace ExportDataToFile { //無設定最大筆數 Utility.WriteFile(strPath + ".sql", string.Format("/*======================================================={0}======================================================*/", strTargetTable)); - Utility.WriteFile(strPath + ".sql", string.Format("raiserror('Now Insert {0} Datas .... ', 1, 1)", strTargetTable)); Utility.WriteFile(strPath + ".sql", "Go"); Utility.WriteFile(strPath + ".sql", " "); } @@ -779,7 +822,6 @@ namespace ExportDataToFile strFileCount = ""; Utility.WriteFile(strPath + strFileCount + ".sql", string.Format("/*======================================================={0}======================================================*/", strTargetTable)); - Utility.WriteFile(strPath + strFileCount + ".sql", string.Format("raiserror('Now Insert {0} Datas .... ', 1, 1)", strTargetTable)); Utility.WriteFile(strPath + strFileCount + ".sql", "Go"); Utility.WriteFile(strPath + strFileCount + ".sql", " "); } @@ -822,11 +864,28 @@ namespace ExportDataToFile { if (strValues == "") { - strValues = ConvertMSSQLDataType(dr[dc].ToString().Trim(), dc.DataType.ToString()); + switch (dc.DataType.ToString().ToUpper()) + { + case "SYSTEM.DATETIME": + strValues = ConvertMSSQLDataType(DateTime.Parse(dr[dc].ToString()), dc.DataType.ToString()); + break; + default: + strValues = ConvertMSSQLDataType(dr[dc].ToString().Trim(), dc.DataType.ToString()); + break; + } } else { - strValues += "," + ConvertMSSQLDataType(dr[dc].ToString().Trim(), dc.DataType.ToString()); + switch (dc.DataType.ToString().ToUpper()) + { + case "SYSTEM.DATETIME": + strValues += "," + ConvertMSSQLDataType(DateTime.Parse(dr[dc].ToString()), dc.DataType.ToString()); + break; + default: + strValues += "," + ConvertMSSQLDataType(dr[dc].ToString().Trim(), dc.DataType.ToString()); + break; + } + } } @@ -895,6 +954,7 @@ namespace ExportDataToFile return "[" + strSourceCol + "] AS " + strTargetCol; case "DATETIME": return "Convert(varchar,[" + strSourceCol + "],21) AS " + strTargetCol; + //return "[" + strSourceCol + "] AS " + strTargetCol; default: return "[" + strSourceCol + "] AS " + strTargetCol; } @@ -918,6 +978,11 @@ namespace ExportDataToFile return "'" + strData.ToString().Trim() + "'"; } } + + private string ConvertMSSQLDataType(DateTime dtData, string strType) + { + return "'" + dtData.ToString("yyyy-MM-dd HH:mm:ss") + "'"; + } #endregion //End of MS-SQL #region PostgreSQL @@ -1057,6 +1122,7 @@ namespace ExportDataToFile dtResult = GetMySQLResult(strSelectCommand, sqlMySourceConn).Tables["Result"]; break; case "Oracle": + dtResult = GetOracleResult(strSelectCommand, sqlOraSourceConn).Tables["Result"]; break; case "PostgreSQL": dtResult = GetPostgreSQLResult(strSelectCommand.ToString(), sqlPsgSourceConn).Tables["Result"]; @@ -1081,7 +1147,6 @@ namespace ExportDataToFile { //無設定最大筆數 Utility.WriteFile(strPath + ".sql", string.Format("/*======================================================={0}======================================================*/", strTargetTable)); - Utility.WriteFile(strPath + ".sql", string.Format("raiserror('Now Insert {0} Datas .... ', 1, 1)", strTargetTable)); Utility.WriteFile(strPath + ".sql", "Go"); Utility.WriteFile(strPath + ".sql", " "); } @@ -1092,7 +1157,6 @@ namespace ExportDataToFile strFileCount = ""; Utility.WriteFile(strPath + strFileCount + ".sql", string.Format("/*======================================================={0}======================================================*/", strTargetTable)); - Utility.WriteFile(strPath + strFileCount + ".sql", string.Format("raiserror('Now Insert {0} Datas .... ', 1, 1)", strTargetTable)); Utility.WriteFile(strPath + strFileCount + ".sql", " "); } } @@ -1132,11 +1196,34 @@ namespace ExportDataToFile { if (strValues == "") { - strValues = ConvertMSSQLDataType(dr[dc].ToString().Trim(), dc.DataType.ToString()); + switch (dc.DataType.ToString().ToUpper()) + { + case "TIMESTAMP WITH TIME ZONE": + strValues = ConvertMSSQLDataType(DateTime.Parse(dr[dc].ToString()), dc.DataType.ToString()); + break; + case "TIMESTAMP WITHOUT TIME ZONE": + strValues = ConvertMSSQLDataType(DateTime.Parse(dr[dc].ToString()), dc.DataType.ToString()); + break; + default: + strValues = ConvertMSSQLDataType(dr[dc].ToString().Trim(), dc.DataType.ToString()); + break; + } } else { - strValues += "," + ConvertMSSQLDataType(dr[dc].ToString().Trim(), dc.DataType.ToString()); + switch (dc.DataType.ToString().ToUpper()) + { + case "TIMESTAMP WITH TIME ZONE": + strValues += "," + ConvertMSSQLDataType(DateTime.Parse(dr[dc].ToString()), dc.DataType.ToString()); + break; + case "TIMESTAMP WITHOUT TIME ZONE": + strValues += "," + ConvertMSSQLDataType(DateTime.Parse(dr[dc].ToString()), dc.DataType.ToString()); + break; + default: + strValues += "," + ConvertMSSQLDataType(dr[dc].ToString().Trim(), dc.DataType.ToString()); + break; + } + } } @@ -1193,10 +1280,10 @@ namespace ExportDataToFile return strSourceCol + " AS " + strTargetCol; case "INTEGER": return strSourceCol + " AS " + strTargetCol; - case "TIMESTAMP WITH TIME ZONE": - return "to_char(" + strSourceCol + ", 'yyyy-MM-dd hh24:mm:ss')"; - case "TIMESTAMP WITHOUT TIME ZONE": - return "to_char(" + strSourceCol + ", 'yyyy-MM-dd hh24:mm:ss')"; + //case "TIMESTAMP WITH TIME ZONE": + // return "to_char(" + strSourceCol + ", 'yyyy-MM-dd hh24:mm:ss')"; + //case "TIMESTAMP WITHOUT TIME ZONE": + // return "to_char(" + strSourceCol + ", 'yyyy-MM-dd hh24:mm:ss')"; default: return strSourceCol + " AS " + strTargetCol; @@ -1215,8 +1302,8 @@ namespace ExportDataToFile return "'" + strData.ToString().Replace("'","''").Trim() + "'"; case "SYSTEM.INT32": return strData.ToString().Trim(); - case "SYSTEM.DATETIME": - return "'" + strData.ToString().Trim() + "'"; + //case "SYSTEM.DATETIME": + //return "'" + strData.ToString().Trim() + "'"; case "SYSTEM.BOOLEAN": return strData.ToString().Trim(); default: @@ -1224,6 +1311,11 @@ namespace ExportDataToFile } } + private string ConvertPostgreSQLDataType(DateTime dtData, string strType) + { + return "'" + dtData.ToString("yyyy-MM-dd HH:mm:ss") + "'"; + } + #endregion //End of PostgreSQL #region MySQL @@ -1390,6 +1482,7 @@ namespace ExportDataToFile dtResult = GetMySQLResult(strSelectCommand, sqlMySourceConn).Tables["Result"]; break; case "Oracle": + dtResult = GetOracleResult(strSelectCommand, sqlOraSourceConn).Tables["Result"]; break; case "PostgreSQL": dtResult = GetPostgreSQLResult(strSelectCommand.ToString(), sqlPsgSourceConn).Tables["Result"]; @@ -1575,6 +1668,356 @@ namespace ExportDataToFile } #endregion //End of MySQL + #region Oracle + public DataSet GetOracleResult(string strSQL, OracleConnection conn) + { + DataSet dsData = new DataSet(); + try + { + using (OracleDataAdapter sqlAdapter = new OracleDataAdapter(strSQL, conn)) + { + if (conn.State == ConnectionState.Closed) //判斷連線狀態 + { + conn.Open(); + blSourceConnStatus = true; + } + sqlAdapter.Fill(dsData, "Result"); + } + + return dsData; + } + catch (Exception ex) + { + throw ex; + } + } + + private DataTable ShowOracleTableList(OracleConnection sqlConn , string strDBName) + { + try + { + string strGetOracleTableList = "SELECT TABLE_NAME AS TableName FROM ALL_TABLES Where OWNER Not IN ('SYS', 'XDB', 'SYSTEM', 'CTXSYS', 'MDSYS') "; + return OracleUtility.GetSQLResult(strGetOracleTableList, sqlConn); + } + catch (Exception ex) + { + ErrorHandler.WriteErrorLog("Form1.ShowOracleTableList", ex); + return null; + } + } + + private DataTable ShowOracleColumnList(OracleConnection sqlConn, string strTableName) + { + try + { + string strGetOracleColumnList = "Select COLUMN_NAME,DATA_TYPE From ALL_TAB_COLUMNS Where TABLE_NAME ='" + strTableName + "' Order by COLUMN_NAME"; + return OracleUtility.GetSQLResult(strGetOracleColumnList, sqlConn); + } + catch (Exception ex) + { + ErrorHandler.WriteErrorLog("Form1.ShowOracleColumnList", ex); + return null; + } + } + + private void ShowOracleTargetColumnList(OracleConnection sqlConn, string strTableName) + { + try + { + string strGetOracleColumnList = "Select COLUMN_NAME From ALL_TAB_COLUMNS Where TABLE_NAME ='" + strTableName + "' Order by COLUMN_NAME"; + this.clTargetColumn.DisplayMember = "COLUMN_NAME"; + this.clTargetColumn.DataSource = OracleUtility.GetSQLResult(strGetOracleColumnList, sqlConn); + Application.DoEvents(); + } + catch (Exception ex) + { + ErrorHandler.WriteErrorLog("Form1.ShowOracleTargetColumnList", ex); + } + } + + private void ExportToOracleData(string strPath, string subFileName) + { + ProgressForm pgsForm = new ProgressForm(); + int intDataCount = 0; + int intMaxData = 0; + int intProgress = 0; + string strGenResult = ""; + string strSourceTable = ""; + string strTargetColumns = ""; + string strWhere = ""; + string strTargetTable = ""; + string strDeleteCommand = ""; + string strValues = ""; + string strFileCount = ""; + string[] strColumns = null; + string[,] strColumnResults = new string[dgvExportList.Rows.Count, 2]; + string strSelectCommand = ""; + + try + { + //命令字串處理 + strTargetColumns = ""; + + //取得每個檔案最大筆數 + if (Int32.Parse(txtMaxCount.Text.Trim()) > 0) + { + intMaxData = Int32.Parse(txtMaxCount.Text.Trim()); + } + else + { + intMaxData = 1; + } + + //取得欄位對應字串陣列 + pgsForm.WindowState = FormWindowState.Normal; + pgsForm.pbExport.Minimum = 0; + pgsForm.Show(); + foreach (DataGridViewRow dgvRow in dgvExportList.Rows) + { + if (dgvRow.Cells["clExport"].Value != null && (bool)dgvRow.Cells["clExport"].Value == true) + { + strSelectCommand = ""; + strSourceTable = ""; + strTargetColumns = ""; + strDeleteCommand = ""; + strValues = ""; + + DataTable dtResult = null; + + //取得欄位對應 + if (dgvRow.Cells["clMappingData"].Value != null) + { + strColumns = dgvRow.Cells["clMappingData"].Value.ToString().Split('|'); + } + //取得來源Table + if (dgvRow.Cells["clSourceTable"].Value != null) + { + if (dgvRow.Cells["clSourceTable"].Value.ToString().Trim() != "") + { + strSourceTable = dgvRow.Cells["clSourceTable"].Value.ToString(); + } + } + //取得目標Table + if (dgvRow.Cells["clTargetTable"].Value != null) + { + if (dgvRow.Cells["clTargetTable"].Value.ToString().Trim() != "") + { + strTargetTable = dgvRow.Cells["clTargetTable"].Value.ToString(); + } + } + + //取得Where條件 + if (dgvRow.Cells["clWhere"].Value != null) + { + if (dgvRow.Cells["clWhere"].Value.ToString().Trim() != "") + { + strWhere = " " + dgvRow.Cells["clWhere"].Value.ToString(); + } + } + #region 產生來源資料查詢命令 + strGenResult = GenExpSourceCommand(strColumns, strSourceClass, strSourceTable, strWhere); + strSelectCommand = strGenResult.Substring(0, strGenResult.IndexOf('|')).Trim(); + strTargetColumns = strGenResult.Substring(strGenResult.IndexOf('|') + 1).Trim(); + + switch (strSourceClass) + { + case "MS-SQL": + dtResult = GetMSSQLResult(strSelectCommand, sqlMSSourceConn).Tables["Result"]; + break; + case "MySQL": + dtResult = GetMySQLResult(strSelectCommand, sqlMySourceConn).Tables["Result"]; + break; + case "Oracle": + dtResult = GetOracleResult(strSelectCommand, sqlOraSourceConn).Tables["Result"]; + break; + case "PostgreSQL": + dtResult = GetPostgreSQLResult(strSelectCommand.ToString(), sqlPsgSourceConn).Tables["Result"]; + break; + } + #endregion + //產生匯出資料語法 + if (dtResult.Rows.Count > 0) + { + //Progess bar + intProgress = 0; + pgsForm.pbExport.Value = 0; + pgsForm.pbExport.Maximum = dtResult.Rows.Count; + + if (dgvRow.Cells["clTargetTable"].Value != null) + { + strTargetTable = dgvRow.Cells["clTargetTable"].Value.ToString(); + + //撰寫實體檔案 + if (intMaxData == 1) + { + //無設定最大筆數 + Utility.WriteFile(strPath + ".sql", string.Format("/*======================================================={0}======================================================*/", strTargetTable)); + Utility.WriteFile(strPath + ".sql", " "); + } + else + { + //有設定最大筆數 + if (strFileCount == "0") + strFileCount = ""; + + Utility.WriteFile(strPath + strFileCount + ".sql", string.Format("/*======================================================={0}======================================================*/", strTargetTable)); + Utility.WriteFile(strPath + strFileCount + ".sql", " "); + } + } + + //勾選清除目標資料表 + if (dgvRow.Cells["clTableDel"].Value != null && Convert.ToBoolean(dgvRow.Cells["clTableDel"].Value) == false) + { + strDeleteCommand = string.Format("Delete From {0} ", strTargetTable); + + if (dgvRow.Cells["clWhere"].Value != null) + { + if (dgvRow.Cells["clWhere"].Value.ToString().Trim() != "") + { + strDeleteCommand += "Where 1=1 And " + dgvRow.Cells["clWhere"].Value.ToString(); + } + } + //撰寫實體檔案 + if (intMaxData == 1) + { + //無設定最大筆數 + Utility.WriteFile(strPath + ".sql", strDeleteCommand + ";"); + } + else + { + //有設定最大筆數 + if (strFileCount == "0") + strFileCount = ""; + + Utility.WriteFile(strPath + strFileCount + ".sql", strDeleteCommand + ";"); + } + } + + //處理筆數上限似資料匯出 + foreach (DataRow dr in dtResult.Rows) + { + foreach (DataColumn dc in dtResult.Columns) + { + if (strValues == "") + { + switch (dc.DataType.ToString().ToUpper()) + { + case "SYSTEM.DATETIME": + strValues = ConvertOracleDataType(DateTime.Parse(dr[dc].ToString()), dc.DataType.ToString()); + break; + default: + strValues = ConvertOracleDataType(dr[dc].ToString().Trim(), dc.DataType.ToString()); + break; + } + + } + else + { + switch (dc.DataType.ToString().ToUpper()) + { + case "SYSTEM.DATETIME": + strValues += "," + ConvertOracleDataType(DateTime.Parse(dr[dc].ToString()), dc.DataType.ToString()); + break; + default: + strValues += "," + ConvertOracleDataType(dr[dc].ToString().Trim(), dc.DataType.ToString()); + break; + } + + } + } + + //撰寫實體檔案 + if (intMaxData == 1) + { + //無設定最大筆數 + Utility.WriteFile(strPath + ".sql", string.Format("Insert Into {0} ({1}) Values ({2}) ;", strTargetTable, strTargetColumns, strValues)); + } + else + { + //有設定最大筆數 + if (strFileCount == "0") + strFileCount = ""; + Utility.WriteFile(strPath + strFileCount + ".sql", string.Format("Insert Into {0} ({1}) Values ({2}) ;", strTargetTable, strTargetColumns, strValues)); + } + intDataCount += 1; + intProgress += 1; + pgsForm.pbExport.Value = intProgress; //變化Progess的狀態 + Application.DoEvents(); + strFileCount = (intDataCount / intMaxData).ToString(); //切分檔案 + strValues = ""; //清除已經存在的資料 + } + + //撰寫實體檔案 + if (intMaxData != 1) + { + //有設定最大筆數 + if (strFileCount == "0") + strFileCount = ""; + } + } + } + } + + MessageBox.Show("匯出完成"); + } + catch (Exception ex) + { + + MessageBox.Show("匯出失敗"); + ErrorHandler.WriteErrorLog("Form1.ExportToMSSQLData", ex); + } + finally + { + pgsForm.Close(); + } + } + + private string ConvertOracleColumnType(string strSourceCol, string strTargetCol, string strType) + { + return strSourceCol + " AS " + strTargetCol; + + //switch (strType.ToUpper()) + //{ + // case "VARCHAR2": + // return strSourceCol + " AS " + strTargetCol; + // case "NUMBER": + // return strSourceCol + " AS " + strTargetCol; + // case "FLOAT": + // return strSourceCol + " AS " + strTargetCol; + // case "INT": + // return strSourceCol + " AS " + strTargetCol; + // case "TIMESTAMP(0)": + // return strSourceCol + " AS " + strTargetCol; + // case "TIMESTAMP": + // return strSourceCol + " AS " + strTargetCol; + // default: + // return strSourceCol + " AS " + strTargetCol; + //} + } + + private string ConvertOracleDataType(string strData, string strType) + { + if (strData == "") + { + return "Null"; + } + switch (strType.ToUpper()) + { + case "SYSTEM.STRING": + return "'" + strData.ToString().Trim() + "'"; + case "SYSTEM.DECIMAL": + return strData.ToString().Trim(); + default: + return "'" + strData.ToString().Trim() + "'"; + } + } + + private string ConvertOracleDataType(DateTime dtData, string strType) + { + return "TO_TIMESTAMP('" + dtData.ToString("yyyy-MM-dd HH:mm:ss") + "', 'YYYY-MM-DD HH24:MI:SS')"; + } + #endregion //End of Oracle + private string CheckSource() { try @@ -1710,6 +2153,8 @@ namespace ExportDataToFile { cbTargetClass.Enabled = false; cbTargetTable.Enabled = true; + cbTargetTable.ValueMember = "TableName"; + cbTargetTable.DisplayMember = "TableName"; cbTargetTable.SelectedIndex = -1; txtTargetIP.Enabled = false; txtTargetPort.Enabled = false; @@ -1730,7 +2175,9 @@ namespace ExportDataToFile { cbTargetClass.Enabled = true; cbTargetTable.DataSource = null; - //cbTargetTable.Enabled = false; + cbTargetTable.ValueMember = "TableName"; + cbTargetTable.DisplayMember = "TableName"; + cbTargetTable.Enabled = false; txtTargetIP.Enabled = true; txtTargetPort.Enabled = true; txtTargetID.Enabled = true; @@ -1862,7 +2309,8 @@ namespace ExportDataToFile Application.DoEvents(); break; case "Oracle": - + dtResult = ShowOracleColumnList(sqlOraSourceConn, strTarget.ToString()); + Application.DoEvents(); break; case "PostgreSQL": dtResult = ShowPostgreSQLColumnList(sqlPsgSourceConn, strTarget.ToString()); @@ -1950,7 +2398,7 @@ namespace ExportDataToFile dtResult = MySQLUtility.GetTable(strTableName, 200, sqlMySourceConn); break; case "Oracle": - + dtResult = OracleUtility.GetTable(strTableName, 200, sqlOraSourceConn); break; case "PostgreSQL": dtResult = PostgreSQLUtility.GetTable(strTableName, 200, sqlPsgSourceConn); @@ -2043,7 +2491,21 @@ namespace ExportDataToFile } break; case "Oracle": + if (strTargetCol == "") + { + strTargetCol = strSourceCol; + } + if (strSourceColumns == "") + { + strSourceColumns += ConvertOracleColumnType(strSourceCol, strTargetCol, strSourceType); + strTargetColumns += strTargetCol; + } + else + { + strSourceColumns += "," + ConvertOracleColumnType(strSourceCol, strTargetCol, strSourceType); + strTargetColumns += "," + strTargetCol; + } break; case "PostgreSQL": if (strTargetCol == "") @@ -2092,7 +2554,15 @@ namespace ExportDataToFile } break; case "Oracle": + if (strSourceTable != "") + { + strSelectCommand += string.Format("Select {0} From {1} ", strSourceColumns, strSourceTable); + } + if (strWhere.Trim() != "") + { + strSelectCommand += "Where 1=1 And " + strWhere.Trim(); + } break; case "PostgreSQL": if (strSourceTable != "") @@ -2117,6 +2587,6 @@ namespace ExportDataToFile } - #endregion + #endregion } } diff --git a/packages.config b/packages.config index ab11b3c..79155bb 100644 --- a/packages.config +++ b/packages.config @@ -7,6 +7,7 @@ +