diff --git a/.vs/ExportDataToFile/v16/.suo b/.vs/ExportDataToFile/v16/.suo index 0467867..f248280 100644 Binary files a/.vs/ExportDataToFile/v16/.suo and b/.vs/ExportDataToFile/v16/.suo differ diff --git a/Form1.Designer.cs b/Form1.Designer.cs index 906fbd0..319b130 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -55,6 +55,15 @@ 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.clSourceTable = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.clTargetTable = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.clTableDel = new System.Windows.Forms.DataGridViewCheckBoxColumn(); + this.clDataTrim = new System.Windows.Forms.DataGridViewCheckBoxColumn(); + this.clMaxRecord = new System.Windows.Forms.DataGridViewTextBoxColumn(); + 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(); @@ -66,15 +75,7 @@ namespace ExportDataToFile this.label14 = new System.Windows.Forms.Label(); this.cbTargetTable = new System.Windows.Forms.ComboBox(); this.btnMapping = new System.Windows.Forms.Button(); - this.clExport = new System.Windows.Forms.DataGridViewCheckBoxColumn(); - this.clCancel = 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.clDataTrim = new System.Windows.Forms.DataGridViewCheckBoxColumn(); - this.clMaxRecord = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.clWhere = new System.Windows.Forms.DataGridViewTextBoxColumn(); - this.clMappingData = new System.Windows.Forms.DataGridViewTextBoxColumn(); + this.sfPath = new System.Windows.Forms.SaveFileDialog(); ((System.ComponentModel.ISupportInitialize)(this.dgvExportList)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dgvColumnMapping)).BeginInit(); this.SuspendLayout(); @@ -355,6 +356,74 @@ namespace ExportDataToFile this.dgvExportList.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvExportList_CellContentClick); this.dgvExportList.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvExportList_CellEndEdit); // + // 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 = "Clean"; + this.clCancel.Width = 55; + // + // 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 = 250; + // + // clTargetTable + // + 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 = 250; + // + // clTableDel + // + this.clTableDel.HeaderText = "清除目標資料表內容"; + this.clTableDel.Name = "clTableDel"; + this.clTableDel.Width = 180; + // + // clDataTrim + // + this.clDataTrim.HeaderText = "清除尾段空白"; + this.clDataTrim.Name = "clDataTrim"; + this.clDataTrim.Width = 120; + // + // clMaxRecord + // + this.clMaxRecord.HeaderText = "檔案匯出上限筆數"; + this.clMaxRecord.Name = "clMaxRecord"; + this.clMaxRecord.Width = 180; + // + // clWhere + // + this.clWhere.HeaderText = "過濾條件"; + this.clWhere.Name = "clWhere"; + this.clWhere.Width = 200; + // + // clMappingData + // + this.clMappingData.HeaderText = "欄位對應"; + this.clMappingData.Name = "clMappingData"; + // // label11 // this.label11.AutoSize = true; @@ -423,18 +492,17 @@ namespace ExportDataToFile this.txtWhere.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); - this.txtWhere.Location = new System.Drawing.Point(1297, 592); + this.txtWhere.Location = new System.Drawing.Point(1297, 556); this.txtWhere.Multiline = true; this.txtWhere.Name = "txtWhere"; this.txtWhere.Size = new System.Drawing.Size(487, 457); this.txtWhere.TabIndex = 47; - this.txtWhere.Leave += new System.EventHandler(this.txtWhere_Leave); // // label13 // this.label13.AutoSize = true; this.label13.ForeColor = System.Drawing.SystemColors.ControlLight; - this.label13.Location = new System.Drawing.Point(1297, 569); + this.label13.Location = new System.Drawing.Point(1297, 533); this.label13.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label13.Name = "label13"; this.label13.Size = new System.Drawing.Size(73, 20); @@ -468,83 +536,15 @@ namespace ExportDataToFile // // btnMapping // - this.btnMapping.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); - this.btnMapping.Location = new System.Drawing.Point(1697, 528); + this.btnMapping.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); + this.btnMapping.Location = new System.Drawing.Point(1697, 1019); this.btnMapping.Name = "btnMapping"; this.btnMapping.Size = new System.Drawing.Size(87, 33); this.btnMapping.TabIndex = 50; - this.btnMapping.Text = "欄位對應"; + this.btnMapping.Text = "設 定"; this.btnMapping.UseVisualStyleBackColor = true; this.btnMapping.Click += new System.EventHandler(this.btnMapping_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 = "Clean"; - this.clCancel.Width = 55; - // - // 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 = 250; - // - // clTargetTable - // - 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 = 250; - // - // clTableDel - // - this.clTableDel.HeaderText = "清除目標資料表內容"; - this.clTableDel.Name = "clTableDel"; - this.clTableDel.Width = 180; - // - // clDataTrim - // - this.clDataTrim.HeaderText = "清除尾段空白"; - this.clDataTrim.Name = "clDataTrim"; - this.clDataTrim.Width = 120; - // - // clMaxRecord - // - this.clMaxRecord.HeaderText = "檔案匯出上限筆數"; - this.clMaxRecord.Name = "clMaxRecord"; - this.clMaxRecord.Width = 180; - // - // clWhere - // - this.clWhere.HeaderText = "過濾條件"; - this.clWhere.Name = "clWhere"; - this.clWhere.Width = 200; - // - // clMappingData - // - this.clMappingData.HeaderText = "欄位對應"; - this.clMappingData.Name = "clMappingData"; - // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 20F); @@ -645,6 +645,7 @@ namespace ExportDataToFile private System.Windows.Forms.DataGridViewTextBoxColumn clMaxRecord; private System.Windows.Forms.DataGridViewTextBoxColumn clWhere; private System.Windows.Forms.DataGridViewTextBoxColumn clMappingData; + private System.Windows.Forms.SaveFileDialog sfPath; } } diff --git a/Form1.cs b/Form1.cs index 16ab941..4743768 100644 --- a/Form1.cs +++ b/Form1.cs @@ -142,7 +142,10 @@ namespace ExportDataToFile { try { - LocatedTarget(e.RowIndex); + if (intExportIndex != e.RowIndex) + { + LocatedTarget(e.RowIndex); + } } catch (Exception ex) { @@ -170,18 +173,7 @@ namespace ExportDataToFile ErrorHandler.WriteErrorLog("Form1.cs", ex); } } - - private void txtWhere_Leave(object sender, EventArgs e) - { - try - { - dgvExportList.Rows[intExportIndex].Cells["clWhere"].Value = txtWhere.Text.Trim(); - } - catch (Exception ex) - { - ErrorHandler.WriteErrorLog("Form1.cs", ex); - } - } + private void cbTargetTable_SelectedIndexChanged(object sender, EventArgs e) { try @@ -252,6 +244,28 @@ namespace ExportDataToFile { try { + if (cbSourceClass.SelectedIndex != -1) + { + string strPath = ""; + sfPath.ShowDialog(); + strPath = sfPath.FileName; + switch (cbSourceClass.SelectedItem.ToString().Trim()) + { + case "MS-SQL": //匯出MS-SQL檔 + ExportMSSQLData(strPath); + break; + case "MySQL": + + break; + case "Oracle": + + break; + case "PostgreSQL": + + break; + } + } + } catch (Exception ex) { @@ -277,11 +291,35 @@ namespace ExportDataToFile { dgvExportList.Rows[intExportIndex].Cells["clTargetTable"].Value = cbTargetTable.SelectedValue.ToString(); } + + dgvExportList.Rows[intExportIndex].Cells["clWhere"].Value = txtWhere.Text.Trim(); } #region 自定義功能 #region MS-SQL + public DataSet GetMSSQLResult(string strSQL) + { + DataSet dsData = new DataSet(); + try + { + using (SqlDataAdapter sqlAdapter = new SqlDataAdapter(strSQL, sqlSourceConn)) + { + if (sqlSourceConn.State == ConnectionState.Closed) //判斷連線狀態 + { + sqlSourceConn.Open(); + } + sqlAdapter.Fill(dsData, "Result"); + } + + return dsData; + } + catch (Exception ex) + { + throw ex; + } + } + private void ShowMSSQLSourceTables(SqlConnection sqlConn) { try @@ -320,6 +358,7 @@ namespace ExportDataToFile ErrorHandler.WriteErrorLog("Form1.cs", ex); } } + private void ShowMSSQLTargetColumnList(SqlConnection sqlConn, string strTableName) { try @@ -335,6 +374,110 @@ namespace ExportDataToFile } } + private void ExportMSSQLData(string strPath) + { + string strSourceColumns = ""; + string strSourceCol = ""; + string strSourceTable = ""; + string strTargetColumns = ""; + string strTargetCol = ""; + string strTargetTable = ""; + string strValues = ""; + string[] strColumns = null; + StringBuilder sbSelectCommand = new StringBuilder(); + StringBuilder sbInsertCommand = new StringBuilder(); + + //命令字串處理 + strSourceColumns = ""; + strTargetColumns = ""; + + //取得欄位對應字串陣列 + foreach (DataGridViewRow dgvRow in dgvExportList.Rows) + { + if (dgvRow.Cells["clExport"].Value != null) + { + strColumns = null; + sbSelectCommand.Clear(); + sbInsertCommand.Clear(); + strSourceTable = ""; + strSourceColumns = ""; + strTargetTable = ""; + strTargetColumns = ""; + strValues = ""; + + DataTable dtResult = null; + + if (dgvRow.Cells["clMappingData"].Value != null) + { + strColumns = dgvRow.Cells["clMappingData"].Value.ToString().Split('|'); + } + + foreach (string RowData in strColumns) + { + strSourceCol = RowData.Substring(0, RowData.IndexOf(',')).Trim(); + strTargetCol = RowData.Substring(RowData.IndexOf(',') + 1).Trim(); + + if (strTargetCol == "") + { + strTargetCol = strSourceCol; + } + + if (strSourceColumns == "") + { + strSourceColumns += "[" + strSourceCol + "] AS '" + strTargetCol + "'"; + strTargetColumns += strTargetCol; + } + else + { + strSourceColumns += ", [" + strSourceCol + "] AS '" + strTargetCol + "'"; + strTargetColumns += "," + strTargetCol; + } + } + + if (dgvRow.Cells["clSourceTable"].Value != null) + { + strSourceTable = dgvRow.Cells["clSourceTable"].Value.ToString(); + sbSelectCommand.AppendLine(string.Format("Select {0} From {1} ", strSourceColumns, strSourceTable)); + } + + + if (dgvRow.Cells["clWhere"].Value != null) + { + if (dgvRow.Cells["clWhere"].Value.ToString().Trim() != "") + { + sbSelectCommand.AppendLine("Where 1=1 And " + dgvRow.Cells["clWhere"].Value.ToString()); + } + } + + //取得完整SQL查詢Command + //txtWhere.Text = sbResult.ToString(); + //txtWhere.Text = strTargetColumns; 目標欄位對應 + dtResult = GetMSSQLResult(sbSelectCommand.ToString()).Tables["Result"]; + foreach (DataRow dr in dtResult.Rows) + { + foreach (DataColumn dc in dtResult.Columns) + { + if (strValues == "") + { + strValues = "'" + dr[dc].ToString() + "'"; + } + else + { + strValues += ",'" + dr[dc].ToString() + "'"; + } + } + + if (dgvRow.Cells["clTargetTable"].Value != null) + { + strTargetTable = dgvRow.Cells["clTargetTable"].Value.ToString(); + sbInsertCommand.AppendLine(string.Format("Insert Into {0} ({1}) ",strSourceTable, strValues)); + } + } + } + Utility.WriteFile(strPath, sbInsertCommand.ToString()); + } + } + #endregion //End of MS-SQL private string CheckSource() @@ -617,6 +760,16 @@ namespace ExportDataToFile ((DataGridViewCheckBoxCell)dgRow.Cells["clExpColumn"]).Value = true; } } + + //設定Where條件 + if (dgvExportList.Rows[intRowIndex].Cells["clWhere"].Value != null) + { + txtWhere.Text = dgvExportList.Rows[intRowIndex].Cells["clWhere"].Value.ToString(); + } + else + { + txtWhere.Text = ""; + } } } } diff --git a/Form1.resx b/Form1.resx index d360572..1727aa0 100644 --- a/Form1.resx +++ b/Form1.resx @@ -156,6 +156,9 @@ True + + 17, 17 + diff --git a/bin/Debug/ExportDataToFile.exe b/bin/Debug/ExportDataToFile.exe index 8d0e46e..93e4879 100644 Binary files a/bin/Debug/ExportDataToFile.exe and b/bin/Debug/ExportDataToFile.exe differ diff --git a/bin/Debug/ExportDataToFile.pdb b/bin/Debug/ExportDataToFile.pdb index f94663b..b88035c 100644 Binary files a/bin/Debug/ExportDataToFile.pdb and b/bin/Debug/ExportDataToFile.pdb differ diff --git a/obj/Debug/ExportDataToFile.csproj.GenerateResource.cache b/obj/Debug/ExportDataToFile.csproj.GenerateResource.cache index b4ebde7..b81329c 100644 Binary files a/obj/Debug/ExportDataToFile.csproj.GenerateResource.cache and b/obj/Debug/ExportDataToFile.csproj.GenerateResource.cache differ diff --git a/obj/Debug/ExportDataToFile.exe b/obj/Debug/ExportDataToFile.exe index 8d0e46e..93e4879 100644 Binary files a/obj/Debug/ExportDataToFile.exe and b/obj/Debug/ExportDataToFile.exe differ diff --git a/obj/Debug/ExportDataToFile.pdb b/obj/Debug/ExportDataToFile.pdb index f94663b..b88035c 100644 Binary files a/obj/Debug/ExportDataToFile.pdb and b/obj/Debug/ExportDataToFile.pdb differ