Browse Source

2022/11/16 更新

master
nelson.kai 2 years ago
parent
commit
876358bd50
  1. BIN
      .vs/ExportDataToFile/v16/.suo
  2. 167
      Form1.Designer.cs
  3. 179
      Form1.cs
  4. 3
      Form1.resx
  5. BIN
      bin/Debug/ExportDataToFile.exe
  6. BIN
      bin/Debug/ExportDataToFile.pdb
  7. BIN
      obj/Debug/ExportDataToFile.csproj.GenerateResource.cache
  8. BIN
      obj/Debug/ExportDataToFile.exe
  9. BIN
      obj/Debug/ExportDataToFile.pdb

BIN
.vs/ExportDataToFile/v16/.suo

167
Form1.Designer.cs

@ -55,6 +55,15 @@ namespace ExportDataToFile
this.txtSourcePWD = new System.Windows.Forms.TextBox(); this.txtSourcePWD = new System.Windows.Forms.TextBox();
this.label5 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label();
this.dgvExportList = new System.Windows.Forms.DataGridView(); 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.label11 = new System.Windows.Forms.Label();
this.dgvColumnMapping = new System.Windows.Forms.DataGridView(); this.dgvColumnMapping = new System.Windows.Forms.DataGridView();
this.clExpColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn(); this.clExpColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
@ -66,15 +75,7 @@ namespace ExportDataToFile
this.label14 = new System.Windows.Forms.Label(); this.label14 = new System.Windows.Forms.Label();
this.cbTargetTable = new System.Windows.Forms.ComboBox(); this.cbTargetTable = new System.Windows.Forms.ComboBox();
this.btnMapping = new System.Windows.Forms.Button(); 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.dgvExportList)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.dgvColumnMapping)).BeginInit(); ((System.ComponentModel.ISupportInitialize)(this.dgvColumnMapping)).BeginInit();
this.SuspendLayout(); this.SuspendLayout();
@ -355,6 +356,74 @@ namespace ExportDataToFile
this.dgvExportList.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvExportList_CellContentClick); this.dgvExportList.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvExportList_CellContentClick);
this.dgvExportList.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvExportList_CellEndEdit); 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 // label11
// //
this.label11.AutoSize = true; 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) 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.Left)
| System.Windows.Forms.AnchorStyles.Right))); | 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.Multiline = true;
this.txtWhere.Name = "txtWhere"; this.txtWhere.Name = "txtWhere";
this.txtWhere.Size = new System.Drawing.Size(487, 457); this.txtWhere.Size = new System.Drawing.Size(487, 457);
this.txtWhere.TabIndex = 47; this.txtWhere.TabIndex = 47;
this.txtWhere.Leave += new System.EventHandler(this.txtWhere_Leave);
// //
// label13 // label13
// //
this.label13.AutoSize = true; this.label13.AutoSize = true;
this.label13.ForeColor = System.Drawing.SystemColors.ControlLight; 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.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label13.Name = "label13"; this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(73, 20); this.label13.Size = new System.Drawing.Size(73, 20);
@ -468,83 +536,15 @@ namespace ExportDataToFile
// //
// btnMapping // 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.Name = "btnMapping";
this.btnMapping.Size = new System.Drawing.Size(87, 33); this.btnMapping.Size = new System.Drawing.Size(87, 33);
this.btnMapping.TabIndex = 50; this.btnMapping.TabIndex = 50;
this.btnMapping.Text = "欄位對應";
this.btnMapping.Text = "設 定";
this.btnMapping.UseVisualStyleBackColor = true; this.btnMapping.UseVisualStyleBackColor = true;
this.btnMapping.Click += new System.EventHandler(this.btnMapping_Click); 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 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 20F); 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 clMaxRecord;
private System.Windows.Forms.DataGridViewTextBoxColumn clWhere; private System.Windows.Forms.DataGridViewTextBoxColumn clWhere;
private System.Windows.Forms.DataGridViewTextBoxColumn clMappingData; private System.Windows.Forms.DataGridViewTextBoxColumn clMappingData;
private System.Windows.Forms.SaveFileDialog sfPath;
} }
} }

179
Form1.cs

@ -142,7 +142,10 @@ namespace ExportDataToFile
{ {
try try
{ {
LocatedTarget(e.RowIndex);
if (intExportIndex != e.RowIndex)
{
LocatedTarget(e.RowIndex);
}
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -170,18 +173,7 @@ namespace ExportDataToFile
ErrorHandler.WriteErrorLog("Form1.cs", ex); 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) private void cbTargetTable_SelectedIndexChanged(object sender, EventArgs e)
{ {
try try
@ -252,6 +244,28 @@ namespace ExportDataToFile
{ {
try 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) catch (Exception ex)
{ {
@ -277,11 +291,35 @@ namespace ExportDataToFile
{ {
dgvExportList.Rows[intExportIndex].Cells["clTargetTable"].Value = cbTargetTable.SelectedValue.ToString(); dgvExportList.Rows[intExportIndex].Cells["clTargetTable"].Value = cbTargetTable.SelectedValue.ToString();
} }
dgvExportList.Rows[intExportIndex].Cells["clWhere"].Value = txtWhere.Text.Trim();
} }
#region 自定義功能 #region 自定義功能
#region MS-SQL #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) private void ShowMSSQLSourceTables(SqlConnection sqlConn)
{ {
try try
@ -320,6 +358,7 @@ namespace ExportDataToFile
ErrorHandler.WriteErrorLog("Form1.cs", ex); ErrorHandler.WriteErrorLog("Form1.cs", ex);
} }
} }
private void ShowMSSQLTargetColumnList(SqlConnection sqlConn, string strTableName) private void ShowMSSQLTargetColumnList(SqlConnection sqlConn, string strTableName)
{ {
try 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 #endregion //End of MS-SQL
private string CheckSource() private string CheckSource()
@ -617,6 +760,16 @@ namespace ExportDataToFile
((DataGridViewCheckBoxCell)dgRow.Cells["clExpColumn"]).Value = true; ((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 = "";
}
} }
} }
} }

3
Form1.resx

@ -156,6 +156,9 @@
<metadata name="clTargetColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="clTargetColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
<metadata name="sfPath.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
<value>17, 17</value>
</metadata>
<assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" /> <assembly alias="System.Drawing" name="System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
<data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <data name="$this.Icon" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value> <value>

BIN
bin/Debug/ExportDataToFile.exe

BIN
bin/Debug/ExportDataToFile.pdb

BIN
obj/Debug/ExportDataToFile.csproj.GenerateResource.cache

BIN
obj/Debug/ExportDataToFile.exe

BIN
obj/Debug/ExportDataToFile.pdb

Loading…
Cancel
Save