diff --git a/.vs/ExportDataToFile/v16/.suo b/.vs/ExportDataToFile/v16/.suo index 6df010c..0467867 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 8ff41d1..906fbd0 100644 --- a/Form1.Designer.cs +++ b/Form1.Designer.cs @@ -57,14 +57,15 @@ namespace ExportDataToFile this.dgvExportList = new System.Windows.Forms.DataGridView(); this.label11 = new System.Windows.Forms.Label(); this.dgvColumnMapping = new System.Windows.Forms.DataGridView(); - this.txtWhere = new System.Windows.Forms.TextBox(); - this.label13 = new System.Windows.Forms.Label(); - this.label14 = new System.Windows.Forms.Label(); - this.cbTargetTable = new System.Windows.Forms.ComboBox(); this.clExpColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn(); this.clClean = new System.Windows.Forms.DataGridViewButtonColumn(); this.clSourceColumn = new System.Windows.Forms.DataGridViewTextBoxColumn(); this.clTargetColumn = new System.Windows.Forms.DataGridViewComboBoxColumn(); + this.txtWhere = new System.Windows.Forms.TextBox(); + this.label13 = new System.Windows.Forms.Label(); + 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(); @@ -381,11 +382,42 @@ namespace ExportDataToFile this.dgvColumnMapping.Location = new System.Drawing.Point(1297, 180); this.dgvColumnMapping.Name = "dgvColumnMapping"; this.dgvColumnMapping.RowTemplate.Height = 25; - this.dgvColumnMapping.Size = new System.Drawing.Size(487, 373); + this.dgvColumnMapping.Size = new System.Drawing.Size(487, 342); this.dgvColumnMapping.TabIndex = 45; this.dgvColumnMapping.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvColumnMapping_CellContentClick); this.dgvColumnMapping.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvColumnMapping_CellEndEdit); // + // clExpColumn + // + this.clExpColumn.HeaderText = "匯出"; + this.clExpColumn.Name = "clExpColumn"; + this.clExpColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True; + this.clExpColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic; + this.clExpColumn.Width = 65; + // + // clClean + // + this.clClean.HeaderText = "清除"; + this.clClean.Name = "clClean"; + this.clClean.Width = 50; + // + // clSourceColumn + // + this.clSourceColumn.DataPropertyName = "COLUMN_NAME"; + this.clSourceColumn.HeaderText = "來源欄位"; + this.clSourceColumn.Name = "clSourceColumn"; + this.clSourceColumn.ReadOnly = true; + this.clSourceColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True; + this.clSourceColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; + this.clSourceColumn.Width = 200; + // + // clTargetColumn + // + this.clTargetColumn.HeaderText = "目標欄位"; + this.clTargetColumn.Name = "clTargetColumn"; + this.clTargetColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True; + this.clTargetColumn.Width = 200; + // // txtWhere // this.txtWhere.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) @@ -434,45 +466,31 @@ namespace ExportDataToFile this.cbTargetTable.ValueMember = "TableName"; this.cbTargetTable.SelectedIndexChanged += new System.EventHandler(this.cbTargetTable_SelectedIndexChanged); // - // clExpColumn + // btnMapping // - this.clExpColumn.HeaderText = "匯出"; - this.clExpColumn.Name = "clExpColumn"; - this.clExpColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True; - this.clExpColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic; - this.clExpColumn.Width = 65; - // - // clClean - // - this.clClean.HeaderText = "清除"; - this.clClean.Name = "clClean"; - this.clClean.Width = 50; - // - // clSourceColumn - // - this.clSourceColumn.DataPropertyName = "COLUMN_NAME"; - this.clSourceColumn.HeaderText = "來源欄位"; - this.clSourceColumn.Name = "clSourceColumn"; - this.clSourceColumn.ReadOnly = true; - this.clSourceColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True; - this.clSourceColumn.Width = 200; - // - // clTargetColumn - // - this.clTargetColumn.HeaderText = "目標欄位"; - this.clTargetColumn.Name = "clTargetColumn"; - this.clTargetColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True; - this.clTargetColumn.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.Automatic; - this.clTargetColumn.Width = 200; + 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.Name = "btnMapping"; + this.btnMapping.Size = new System.Drawing.Size(87, 33); + this.btnMapping.TabIndex = 50; + 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"; @@ -481,12 +499,13 @@ namespace ExportDataToFile // 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 = 200; + this.clSourceTable.Width = 250; // // clTargetTable // @@ -495,7 +514,7 @@ namespace ExportDataToFile this.clTargetTable.ReadOnly = true; this.clTargetTable.Resizable = System.Windows.Forms.DataGridViewTriState.True; this.clTargetTable.SortMode = System.Windows.Forms.DataGridViewColumnSortMode.NotSortable; - this.clTargetTable.Width = 130; + this.clTargetTable.Width = 250; // // clTableDel // @@ -532,6 +551,7 @@ namespace ExportDataToFile this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.ControlDarkDark; this.ClientSize = new System.Drawing.Size(1796, 1061); + this.Controls.Add(this.btnMapping); this.Controls.Add(this.cbTargetTable); this.Controls.Add(this.label14); this.Controls.Add(this.txtWhere); @@ -570,6 +590,7 @@ namespace ExportDataToFile this.Name = "Form1"; this.Text = "資料匯出程式"; this.WindowState = System.Windows.Forms.FormWindowState.Maximized; + this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing); ((System.ComponentModel.ISupportInitialize)(this.dgvExportList)).EndInit(); ((System.ComponentModel.ISupportInitialize)(this.dgvColumnMapping)).EndInit(); this.ResumeLayout(false); @@ -614,6 +635,7 @@ namespace ExportDataToFile private System.Windows.Forms.DataGridViewButtonColumn clClean; private System.Windows.Forms.DataGridViewTextBoxColumn clSourceColumn; private System.Windows.Forms.DataGridViewComboBoxColumn clTargetColumn; + private System.Windows.Forms.Button btnMapping; private System.Windows.Forms.DataGridViewCheckBoxColumn clExport; private System.Windows.Forms.DataGridViewButtonColumn clCancel; private System.Windows.Forms.DataGridViewTextBoxColumn clSourceTable; diff --git a/Form1.cs b/Form1.cs index 722cecc..16ab941 100644 --- a/Form1.cs +++ b/Form1.cs @@ -141,29 +141,8 @@ namespace ExportDataToFile private void dgvExportList_CellClick(object sender, DataGridViewCellEventArgs e) { try - { - if (e.RowIndex != -1) - { - intExportIndex = e.RowIndex; - if (dgvExportList.Rows[e.RowIndex].Cells["clTargetTable"].Value != null) - { - cbTargetTable.SelectedValue = dgvExportList.Rows[e.RowIndex].Cells["clTargetTable"].Value; - } - else - { - cbTargetTable.SelectedValue = dgvExportList.Rows[e.RowIndex].Cells["clSourceTable"].Value; - } - if (cbTargetTable.SelectedValue != null) - { - ShowMSSQLSourceColumnList(sqlSourceConn, cbTargetTable.SelectedValue.ToString()); - } - - //預設所有欄位均匯出 - foreach (DataGridViewRow dgRow in dgvColumnMapping.Rows) - { - dgRow.Cells["clExpColumn"].Value = true; - } - } + { + LocatedTarget(e.RowIndex); } catch (Exception ex) { @@ -174,18 +153,16 @@ namespace ExportDataToFile private void dgvExportList_CellContentClick(object sender, DataGridViewCellEventArgs e) { try - { + { var senderGrid = (DataGridView)sender; - if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0) + + if (e.RowIndex >= 0) { - senderGrid.Rows[e.RowIndex].Cells["clExport"].Value = false; - senderGrid.Rows[e.RowIndex].Cells["clTargetTable"].Value = ""; - senderGrid.Rows[e.RowIndex].Cells["clTableDel"].Value = false; - senderGrid.Rows[e.RowIndex].Cells["clDataTrim"].Value = false; - senderGrid.Rows[e.RowIndex].Cells["clMaxRecord"].Value = ""; - senderGrid.Rows[e.RowIndex].Cells["clWhere"].Value = ""; - senderGrid.Rows[e.RowIndex].Cells["clMappingData"].Value = ""; - txtWhere.Text = ""; + //清除該列資料 + if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn) + { + CleanExpRow(dgvExportList, e.RowIndex); + } } } catch (Exception ex) @@ -242,11 +219,11 @@ namespace ExportDataToFile try { var senderGrid = (DataGridView)sender; + //觸發清除事件 if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0) { senderGrid.Rows[e.RowIndex].Cells["clExpColumn"].Value = false; - ((DataGridViewComboBoxCell)senderGrid.Rows[e.RowIndex].Cells["clTargetColumn"]).Value = null; - txtWhere.Text = ""; + ((DataGridViewComboBoxCell)senderGrid.Rows[e.RowIndex].Cells["clTargetColumn"]).Value = null; } } catch (Exception ex) @@ -259,10 +236,12 @@ namespace ExportDataToFile { try { - dgvColumnMapping.Rows[e.RowIndex].Cells["clExpColumn"].Value = true; - dgvExportList.Rows[intExportIndex].Cells["clExport"].Value = true; - //設定來源與目標的資料表對應 - dgvExportList.Rows[intExportIndex].Cells["clTargetTable"].Value = cbTargetTable.SelectedValue.ToString(); + if (cbTargetTable.Enabled) + { + //dgvColumnMapping.Rows[e.RowIndex].Cells["clExpColumn"].Value = true; + //dgvExportList.Rows[intExportIndex].Cells["clExport"].Value = true; + + } } catch (Exception ex) { @@ -280,8 +259,84 @@ namespace ExportDataToFile } } + private void Form1_FormClosing(object sender, FormClosingEventArgs e) + { + sqlSourceConn.Close(); + sqlSourceConn = null; + sqlTargetConn.Close(); + sqlTargetConn = null; + } + + private void btnMapping_Click(object sender, EventArgs e) + { + //自動預設產生所有的Mapping檔案 + dgvExportList.Rows[intExportIndex].Cells["clMappingData"].Value = GenMappingColumn(); + dgvExportList.Rows[intExportIndex].Cells["clExport"].Value = true; + //設定來源與目標的資料表對應 + if (cbTargetTable.SelectedValue != null) + { + dgvExportList.Rows[intExportIndex].Cells["clTargetTable"].Value = cbTargetTable.SelectedValue.ToString(); + } + } + #region 自定義功能 + #region MS-SQL + private void ShowMSSQLSourceTables(SqlConnection sqlConn) + { + try + { + string strGetMSSQLTableList = "Select [name] as TableName from sys.tables order by name"; + dgvExportList.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLTableList, sqlConn).Tables["Result"]; + } + catch (Exception ex) + { + ErrorHandler.WriteErrorLog("Form1.cs", ex); + } + } + + private void ShowMSSQLTargetTableList(SqlConnection sqlConn) + { + try + { + string strGetMSSQLTableList = "Select [name] as TableName from sys.tables order by name"; + cbTargetTable.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLTableList, sqlConn).Tables["Result"]; + } + catch (Exception ex) + { + ErrorHandler.WriteErrorLog("Form1.cs", ex); + } + } + + private void ShowMSSQLSourceColumnList(SqlConnection sqlConn, string strTableName) + { + try + { + string strGetMSSQLColumnList = "Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME ='" + strTableName + "' Order by ORDINAL_POSITION"; + dgvColumnMapping.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLColumnList, sqlConn).Tables["Result"]; + } + catch (Exception ex) + { + ErrorHandler.WriteErrorLog("Form1.cs", ex); + } + } + private void ShowMSSQLTargetColumnList(SqlConnection sqlConn, string strTableName) + { + try + { + string strGetMSSQLColumnList = "Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME ='" + strTableName + "' Order by ORDINAL_POSITION"; + this.clTargetColumn.DisplayMember = "COLUMN_NAME"; + this.clTargetColumn.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLColumnList, sqlConn).Tables["Result"]; + Application.DoEvents(); + } + catch (Exception ex) + { + ErrorHandler.WriteErrorLog("Form1.cs", ex); + } + } + + #endregion //End of MS-SQL + private string CheckSource() { try @@ -435,64 +490,157 @@ namespace ExportDataToFile ErrorHandler.WriteErrorLog("Form1.cs", ex); } } - - #region MS-SQL - private void ShowMSSQLSourceTables(SqlConnection sqlConn) + + private string GenMappingColumn() { - try - { - string strGetMSSQLTableList = "Select [name] as TableName from sys.tables order by name"; - dgvExportList.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLTableList, sqlConn).Tables["Result"]; - } - catch (Exception ex) - { - ErrorHandler.WriteErrorLog("Form1.cs", ex); - } - } + string strExportColumns = ""; + string strSourceColumn = ""; + string strTargetColumn = ""; - private void ShowMSSQLTargetTableList(SqlConnection sqlConn) - { - try + //預設所有欄位均匯出 + foreach (DataGridViewRow dgRow in dgvColumnMapping.Rows) { - string strGetMSSQLTableList = "Select [name] as TableName from sys.tables order by name"; - cbTargetTable.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLTableList, sqlConn).Tables["Result"]; - } - catch (Exception ex) - { - ErrorHandler.WriteErrorLog("Form1.cs", ex); + //處理下拉欄位空白的問題 + if (dgRow.Cells["clExpColumn"].Value != null) + { + if ((bool)dgRow.Cells["clExpColumn"].Value == true) + { + if (dgRow.Cells["clSourceColumn"].Value == null) + { + strSourceColumn = " "; + } + else + { + strSourceColumn = dgRow.Cells["clSourceColumn"].Value.ToString(); + } + + if (dgRow.Cells["clTargetColumn"].Value == null) + { + strTargetColumn = " "; + } + else + { + strTargetColumn = dgRow.Cells["clTargetColumn"].Value.ToString(); + } + + if (strExportColumns == "") + { + + strExportColumns += strSourceColumn + "," + strTargetColumn; + } + else + { + strExportColumns += "|" + strSourceColumn + "," + strTargetColumn; + } + } + } } + + return strExportColumns; } - private void ShowMSSQLSourceColumnList(SqlConnection sqlConn, string strTableName) + private void LocatedTarget(int intRowIndex) { - try - { - string strGetMSSQLColumnList = "Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME ='" + strTableName + "' Order by ORDINAL_POSITION"; - dgvColumnMapping.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLColumnList, sqlConn).Tables["Result"]; - } - catch (Exception ex) + if (cbTargetClass.SelectedIndex >= 0 && cbTargetClass.Enabled == false) { - ErrorHandler.WriteErrorLog("Form1.cs", ex); + //cbTargetTable.SelectedValue = dgvExportList.Rows[intRowIndex].Cells["clSourceTable"].Value.ToString(); + + string strTarget = ""; + string[] strColumnMapping = null; + + if (intRowIndex != -1) + { + intExportIndex = intRowIndex; + + //設定目標Table下拉式選單的內容 + if (dgvExportList.Rows[intRowIndex].Cells["clTargetTable"].Value != null && dgvExportList.Rows[intRowIndex].Cells["clTargetTable"].Value.ToString() != "") + { + strTarget = dgvExportList.Rows[intRowIndex].Cells["clTargetTable"].Value.ToString(); + } + else + { + strTarget = dgvExportList.Rows[intRowIndex].Cells["clSourceTable"].Value.ToString(); + } + + cbTargetTable.SelectedValue = strTarget; + + //取得欄位對應字串陣列 + if (dgvExportList.Rows[intRowIndex].Cells["clMappingData"].Value != null) + { + strColumnMapping = dgvExportList.Rows[intRowIndex].Cells["clMappingData"].Value.ToString().Split('|'); + } + + + //顯示欄位對應表的內容 + if (cbTargetTable.SelectedValue != null) + { + switch (cbTargetClass.SelectedItem.ToString().Trim()) + { + case "MS-SQL": + ShowMSSQLSourceColumnList(sqlSourceConn, strTarget.ToString()); + Application.DoEvents(); + break; + case "MySQL": + + break; + case "Oracle": + + break; + case "PostgreSQL": + + break; + } + } + + if (strColumnMapping != null) + { + string strSourceColumn = ""; + string strTargetColumn = ""; + foreach (string strColumn in strColumnMapping) + { + strSourceColumn = strColumn.Substring(0, strColumn.IndexOf(',')).Trim(); + strTargetColumn = strColumn.Substring(strColumn.IndexOf(',') + 1).Trim(); + foreach (DataGridViewRow dgvRow in dgvColumnMapping.Rows) + { + if (dgvRow.Cells["clSourceColumn"].Value.ToString() == strSourceColumn) + { + dgvRow.Cells[0].Value = true; + } + } + } + } + else + { + //預設所有欄位均匯出 + foreach (DataGridViewRow dgRow in dgvColumnMapping.Rows) + { + ((DataGridViewCheckBoxCell)dgRow.Cells["clExpColumn"]).Value = true; + } + } + } } } - private void ShowMSSQLTargetColumnList(SqlConnection sqlConn, string strTableName) + + private void CleanExpRow(DataGridView senderGrid, int intRowIndex) { - try - { - string strGetMSSQLColumnList = "Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME ='" + strTableName + "' Order by ORDINAL_POSITION"; - this.clTargetColumn.DisplayMember = "COLUMN_NAME"; - this.clTargetColumn.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLColumnList, sqlConn).Tables["Result"]; - Application.DoEvents(); - } - catch (Exception ex) - { - ErrorHandler.WriteErrorLog("Form1.cs", ex); - } + senderGrid.Rows[intRowIndex].Cells["clExport"].Value = false; + senderGrid.Rows[intRowIndex].Cells["clTargetTable"].Value = ""; + senderGrid.Rows[intRowIndex].Cells["clTableDel"].Value = false; + senderGrid.Rows[intRowIndex].Cells["clDataTrim"].Value = false; + senderGrid.Rows[intRowIndex].Cells["clMaxRecord"].Value = ""; + senderGrid.Rows[intRowIndex].Cells["clWhere"].Value = ""; + senderGrid.Rows[intRowIndex].Cells["clMappingData"].Value = ""; + CleanMappingRow(senderGrid, intRowIndex); + txtWhere.Text = ""; + Application.DoEvents(); } - - #endregion - + private void CleanMappingRow(DataGridView senderGrid,int intRowIndex) + { + DataTable CleanTable = (DataTable)dgvColumnMapping.DataSource; + CleanTable.Rows.Clear(); + dgvColumnMapping.DataSource = CleanTable; + } #endregion diff --git a/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache b/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache index 8ef9cad..811fdaf 100644 Binary files a/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache and b/obj/Debug/DesignTimeResolveAssemblyReferencesInput.cache differ diff --git a/obj/Debug/ExportDataToFile.csproj.GenerateResource.cache b/obj/Debug/ExportDataToFile.csproj.GenerateResource.cache index 268db79..b4ebde7 100644 Binary files a/obj/Debug/ExportDataToFile.csproj.GenerateResource.cache and b/obj/Debug/ExportDataToFile.csproj.GenerateResource.cache differ