Browse Source

完成MS-SQL匯出功能

master
nelson.kai 2 years ago
parent
commit
e97d4fda6f
  1. BIN
      .vs/ExportDataToFile/v16/.suo
  2. 256
      Form1.Designer.cs
  3. 212
      Form1.cs
  4. 6
      Form1.resx

BIN
.vs/ExportDataToFile/v16/.suo

256
Form1.Designer.cs

@ -55,27 +55,27 @@ 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.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.txtWhere = new System.Windows.Forms.TextBox();
this.label13 = new System.Windows.Forms.Label(); this.label13 = new System.Windows.Forms.Label();
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.sfPath = new System.Windows.Forms.SaveFileDialog(); this.sfPath = new System.Windows.Forms.SaveFileDialog();
this.clExpColumn = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.clClean = new System.Windows.Forms.DataGridViewButtonColumn();
this.clSourceColumn = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.clType = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.clTargetColumn = new System.Windows.Forms.DataGridViewComboBoxColumn();
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.clMaxRecord = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.clWhere = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.clMappingData = new System.Windows.Forms.DataGridViewTextBoxColumn();
((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();
@ -247,6 +247,7 @@ namespace ExportDataToFile
// btnExport // btnExport
// //
this.btnExport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); this.btnExport.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.btnExport.Enabled = false;
this.btnExport.Location = new System.Drawing.Point(1649, 85); this.btnExport.Location = new System.Drawing.Point(1649, 85);
this.btnExport.Name = "btnExport"; this.btnExport.Name = "btnExport";
this.btnExport.Size = new System.Drawing.Size(135, 30); this.btnExport.Size = new System.Drawing.Size(135, 30);
@ -259,7 +260,7 @@ namespace ExportDataToFile
// //
this.label12.AutoSize = true; this.label12.AutoSize = true;
this.label12.ForeColor = System.Drawing.SystemColors.ControlLight; this.label12.ForeColor = System.Drawing.SystemColors.ControlLight;
this.label12.Location = new System.Drawing.Point(1293, 157);
this.label12.Location = new System.Drawing.Point(1224, 157);
this.label12.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label12.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label12.Name = "label12"; this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(73, 20); this.label12.Size = new System.Drawing.Size(73, 20);
@ -343,87 +344,18 @@ namespace ExportDataToFile
this.clSourceTable, this.clSourceTable,
this.clTargetTable, this.clTargetTable,
this.clTableDel, this.clTableDel,
this.clDataTrim,
this.clMaxRecord, this.clMaxRecord,
this.clWhere, this.clWhere,
this.clMappingData}); this.clMappingData});
this.dgvExportList.Location = new System.Drawing.Point(18, 125); this.dgvExportList.Location = new System.Drawing.Point(18, 125);
this.dgvExportList.Name = "dgvExportList"; this.dgvExportList.Name = "dgvExportList";
this.dgvExportList.RowTemplate.Height = 25; this.dgvExportList.RowTemplate.Height = 25;
this.dgvExportList.Size = new System.Drawing.Size(1257, 924);
this.dgvExportList.Size = new System.Drawing.Size(1204, 924);
this.dgvExportList.TabIndex = 44; this.dgvExportList.TabIndex = 44;
this.dgvExportList.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvExportList_CellClick); this.dgvExportList.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvExportList_CellClick);
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;
@ -447,62 +379,32 @@ namespace ExportDataToFile
this.clExpColumn, this.clExpColumn,
this.clClean, this.clClean,
this.clSourceColumn, this.clSourceColumn,
this.clType,
this.clTargetColumn}); this.clTargetColumn});
this.dgvColumnMapping.Location = new System.Drawing.Point(1297, 180);
this.dgvColumnMapping.Location = new System.Drawing.Point(1228, 180);
this.dgvColumnMapping.Name = "dgvColumnMapping"; this.dgvColumnMapping.Name = "dgvColumnMapping";
this.dgvColumnMapping.RowTemplate.Height = 25; this.dgvColumnMapping.RowTemplate.Height = 25;
this.dgvColumnMapping.Size = new System.Drawing.Size(487, 342);
this.dgvColumnMapping.Size = new System.Drawing.Size(556, 342);
this.dgvColumnMapping.TabIndex = 45; this.dgvColumnMapping.TabIndex = 45;
this.dgvColumnMapping.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvColumnMapping_CellContentClick); this.dgvColumnMapping.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvColumnMapping_CellContentClick);
this.dgvColumnMapping.CellEndEdit += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgvColumnMapping_CellEndEdit); 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 // txtWhere
// //
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, 556);
this.txtWhere.Location = new System.Drawing.Point(1228, 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(556, 457);
this.txtWhere.TabIndex = 47; this.txtWhere.TabIndex = 47;
// //
// 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, 533);
this.label13.Location = new System.Drawing.Point(1228, 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);
@ -513,7 +415,7 @@ namespace ExportDataToFile
// //
this.label14.AutoSize = true; this.label14.AutoSize = true;
this.label14.ForeColor = System.Drawing.SystemColors.ControlLight; this.label14.ForeColor = System.Drawing.SystemColors.ControlLight;
this.label14.Location = new System.Drawing.Point(1293, 125);
this.label14.Location = new System.Drawing.Point(1224, 125);
this.label14.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0); this.label14.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label14.Name = "label14"; this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(89, 20); this.label14.Size = new System.Drawing.Size(89, 20);
@ -527,9 +429,9 @@ namespace ExportDataToFile
this.cbTargetTable.DisplayMember = "TableName"; this.cbTargetTable.DisplayMember = "TableName";
this.cbTargetTable.Enabled = false; this.cbTargetTable.Enabled = false;
this.cbTargetTable.FormattingEnabled = true; this.cbTargetTable.FormattingEnabled = true;
this.cbTargetTable.Location = new System.Drawing.Point(1389, 122);
this.cbTargetTable.Location = new System.Drawing.Point(1320, 122);
this.cbTargetTable.Name = "cbTargetTable"; this.cbTargetTable.Name = "cbTargetTable";
this.cbTargetTable.Size = new System.Drawing.Size(395, 28);
this.cbTargetTable.Size = new System.Drawing.Size(464, 28);
this.cbTargetTable.TabIndex = 49; this.cbTargetTable.TabIndex = 49;
this.cbTargetTable.ValueMember = "TableName"; this.cbTargetTable.ValueMember = "TableName";
this.cbTargetTable.SelectedIndexChanged += new System.EventHandler(this.cbTargetTable_SelectedIndexChanged); this.cbTargetTable.SelectedIndexChanged += new System.EventHandler(this.cbTargetTable_SelectedIndexChanged);
@ -545,6 +447,108 @@ namespace ExportDataToFile
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);
// //
// 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;
//
// clType
//
this.clType.DataPropertyName = "DATA_TYPE";
this.clType.HeaderText = "型態";
this.clType.Name = "clType";
this.clType.Width = 80;
//
// clTargetColumn
//
this.clTargetColumn.HeaderText = "目標欄位";
this.clTargetColumn.Name = "clTargetColumn";
this.clTargetColumn.Resizable = System.Windows.Forms.DataGridViewTriState.True;
this.clTargetColumn.Width = 200;
//
// 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 = 300;
//
// 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 = 300;
//
// clTableDel
//
this.clTableDel.HeaderText = "清除目標資料表內容";
this.clTableDel.Name = "clTableDel";
this.clTableDel.Width = 180;
//
// clMaxRecord
//
this.clMaxRecord.HeaderText = "檔案匯出上限筆數";
this.clMaxRecord.Name = "clMaxRecord";
this.clMaxRecord.Width = 160;
//
// clWhere
//
this.clWhere.HeaderText = "過濾條件";
this.clWhere.Name = "clWhere";
this.clWhere.Visible = false;
this.clWhere.Width = 200;
//
// clMappingData
//
this.clMappingData.HeaderText = "欄位對應";
this.clMappingData.Name = "clMappingData";
this.clMappingData.Visible = false;
//
// Form1 // Form1
// //
this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 20F); this.AutoScaleDimensions = new System.Drawing.SizeF(10F, 20F);
@ -631,21 +635,21 @@ namespace ExportDataToFile
private System.Windows.Forms.Label label13; private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label14; private System.Windows.Forms.Label label14;
private System.Windows.Forms.ComboBox cbTargetTable; private System.Windows.Forms.ComboBox cbTargetTable;
private System.Windows.Forms.Button btnMapping;
private System.Windows.Forms.SaveFileDialog sfPath;
private System.Windows.Forms.DataGridViewCheckBoxColumn clExpColumn; private System.Windows.Forms.DataGridViewCheckBoxColumn clExpColumn;
private System.Windows.Forms.DataGridViewButtonColumn clClean; private System.Windows.Forms.DataGridViewButtonColumn clClean;
private System.Windows.Forms.DataGridViewTextBoxColumn clSourceColumn; private System.Windows.Forms.DataGridViewTextBoxColumn clSourceColumn;
private System.Windows.Forms.DataGridViewTextBoxColumn clType;
private System.Windows.Forms.DataGridViewComboBoxColumn clTargetColumn; private System.Windows.Forms.DataGridViewComboBoxColumn clTargetColumn;
private System.Windows.Forms.Button btnMapping;
private System.Windows.Forms.DataGridViewCheckBoxColumn clExport; private System.Windows.Forms.DataGridViewCheckBoxColumn clExport;
private System.Windows.Forms.DataGridViewButtonColumn clCancel; private System.Windows.Forms.DataGridViewButtonColumn clCancel;
private System.Windows.Forms.DataGridViewTextBoxColumn clSourceTable; private System.Windows.Forms.DataGridViewTextBoxColumn clSourceTable;
private System.Windows.Forms.DataGridViewTextBoxColumn clTargetTable; private System.Windows.Forms.DataGridViewTextBoxColumn clTargetTable;
private System.Windows.Forms.DataGridViewCheckBoxColumn clTableDel; private System.Windows.Forms.DataGridViewCheckBoxColumn clTableDel;
private System.Windows.Forms.DataGridViewCheckBoxColumn clDataTrim;
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;
} }
} }

212
Form1.cs

@ -57,6 +57,7 @@ namespace ExportDataToFile
break; break;
} }
EnableExport(); //啟動匯出鈕
} }
else else
{ {
@ -89,6 +90,8 @@ namespace ExportDataToFile
{ {
if (cbTargetClass.Enabled) if (cbTargetClass.Enabled)
{ {
LockTargetForm();
switch (cbTargetClass.SelectedItem.ToString().Trim()) switch (cbTargetClass.SelectedItem.ToString().Trim())
{ {
case "MS-SQL": case "MS-SQL":
@ -108,7 +111,7 @@ namespace ExportDataToFile
break; break;
} }
LockTargetForm();
EnableExport(); //啟動匯出鈕
} }
else else
{ {
@ -244,15 +247,23 @@ namespace ExportDataToFile
{ {
try try
{ {
if (cbSourceClass.SelectedIndex != -1)
if (cbSourceClass.SelectedIndex > 0)
{ {
string strPath = ""; string strPath = "";
sfPath.ShowDialog();
sfPath.AddExtension = false;
sfPath.Filter = "All files (*.*)|*.*|SQL Files(*.sql)|*.sql|txt files(*.txt)|*.txt";
sfPath.FileName = "Output" + DateTime.Now.ToString("_yyyyMMddHHmmss");
if (sfPath.ShowDialog() == DialogResult.Cancel) //如果按下取消,放棄後面動作
return;
strPath = sfPath.FileName; strPath = sfPath.FileName;
switch (cbSourceClass.SelectedItem.ToString().Trim()) switch (cbSourceClass.SelectedItem.ToString().Trim())
{ {
case "MS-SQL": //匯出MS-SQL檔 case "MS-SQL": //匯出MS-SQL檔
ExportMSSQLData(strPath);
ExportMSSQLData(sfPath.FileName,sfPath.DefaultExt);
break; break;
case "MySQL": case "MySQL":
@ -283,6 +294,9 @@ namespace ExportDataToFile
private void btnMapping_Click(object sender, EventArgs e) private void btnMapping_Click(object sender, EventArgs e)
{ {
if (dgvColumnMapping.DataSource == null)
return;
//自動預設產生所有的Mapping檔案 //自動預設產生所有的Mapping檔案
dgvExportList.Rows[intExportIndex].Cells["clMappingData"].Value = GenMappingColumn(); dgvExportList.Rows[intExportIndex].Cells["clMappingData"].Value = GenMappingColumn();
dgvExportList.Rows[intExportIndex].Cells["clExport"].Value = true; dgvExportList.Rows[intExportIndex].Cells["clExport"].Value = true;
@ -350,7 +364,7 @@ namespace ExportDataToFile
{ {
try try
{ {
string strGetMSSQLColumnList = "Select COLUMN_NAME From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME ='" + strTableName + "' Order by ORDINAL_POSITION";
string strGetMSSQLColumnList = "Select COLUMN_NAME,DATA_TYPE From INFORMATION_SCHEMA.COLUMNS Where TABLE_NAME ='" + strTableName + "' Order by ORDINAL_POSITION";
dgvColumnMapping.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLColumnList, sqlConn).Tables["Result"]; dgvColumnMapping.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLColumnList, sqlConn).Tables["Result"];
} }
catch (Exception ex) catch (Exception ex)
@ -374,18 +388,22 @@ namespace ExportDataToFile
} }
} }
private void ExportMSSQLData(string strPath)
private void ExportMSSQLData(string strPath,string subFileName)
{ {
int intDataCount = 0;
int intMaxData = 0;
string strSourceColumns = ""; string strSourceColumns = "";
string strSourceCol = ""; string strSourceCol = "";
string strSourceTable = ""; string strSourceTable = "";
string strSourceType = "";
string strTargetColumns = ""; string strTargetColumns = "";
string strTargetCol = ""; string strTargetCol = "";
string strTargetTable = ""; string strTargetTable = "";
string strDeleteCommand = "";
string strValues = ""; string strValues = "";
string[] strColumns = null; string[] strColumns = null;
string[,] strColumnResults = new string[dgvExportList.Rows.Count, 2];
StringBuilder sbSelectCommand = new StringBuilder(); StringBuilder sbSelectCommand = new StringBuilder();
StringBuilder sbInsertCommand = new StringBuilder();
//命令字串處理 //命令字串處理
strSourceColumns = ""; strSourceColumns = "";
@ -396,26 +414,38 @@ namespace ExportDataToFile
{ {
if (dgvRow.Cells["clExport"].Value != null) if (dgvRow.Cells["clExport"].Value != null)
{ {
intDataCount = 0;
intMaxData = 0;
strColumns = null; strColumns = null;
sbSelectCommand.Clear(); sbSelectCommand.Clear();
sbInsertCommand.Clear();
strSourceTable = ""; strSourceTable = "";
strSourceColumns = ""; strSourceColumns = "";
strSourceType = "";
strTargetTable = ""; strTargetTable = "";
strTargetColumns = ""; strTargetColumns = "";
strDeleteCommand = "";
strValues = ""; strValues = "";
DataTable dtResult = null; DataTable dtResult = null;
//取得欄位對應
if (dgvRow.Cells["clMappingData"].Value != null) if (dgvRow.Cells["clMappingData"].Value != null)
{ {
strColumns = dgvRow.Cells["clMappingData"].Value.ToString().Split('|'); strColumns = dgvRow.Cells["clMappingData"].Value.ToString().Split('|');
} }
//取得欄位上限
if (dgvRow.Cells["clMaxRecord"].Value != null)
{
intMaxData = Int32.Parse(dgvRow.Cells["clMaxRecord"].Value.ToString());
}
#region 產生來源資料查詢命令
foreach (string RowData in strColumns) foreach (string RowData in strColumns)
{ {
strSourceCol = RowData.Substring(0, RowData.IndexOf(',')).Trim(); strSourceCol = RowData.Substring(0, RowData.IndexOf(',')).Trim();
strTargetCol = RowData.Substring(RowData.IndexOf(',') + 1).Trim();
strSourceType = RowData.Substring(RowData.IndexOf(',') + 1, RowData.IndexOf(';') - 1 - RowData.IndexOf(',')).Trim();
strTargetCol = RowData.Substring(RowData.IndexOf(';') + 1).Trim();
if (strTargetCol == "") if (strTargetCol == "")
{ {
@ -423,15 +453,15 @@ namespace ExportDataToFile
} }
if (strSourceColumns == "") if (strSourceColumns == "")
{
strSourceColumns += "[" + strSourceCol + "] AS '" + strTargetCol + "'";
strTargetColumns += strTargetCol;
{
strSourceColumns += ConvertMSSQLColumnType(strSourceCol, strTargetCol, strSourceType);
strTargetColumns += "[" + strTargetCol + "]";
} }
else else
{ {
strSourceColumns += ", [" + strSourceCol + "] AS '" + strTargetCol + "'";
strTargetColumns += "," + strTargetCol;
}
strSourceColumns += "," + ConvertMSSQLColumnType(strSourceCol, strTargetCol, strSourceType);
strTargetColumns += ",[" + strTargetCol + "]";
}
} }
if (dgvRow.Cells["clSourceTable"].Value != null) if (dgvRow.Cells["clSourceTable"].Value != null)
@ -440,7 +470,6 @@ namespace ExportDataToFile
sbSelectCommand.AppendLine(string.Format("Select {0} From {1} ", strSourceColumns, strSourceTable)); sbSelectCommand.AppendLine(string.Format("Select {0} From {1} ", strSourceColumns, strSourceTable));
} }
if (dgvRow.Cells["clWhere"].Value != null) if (dgvRow.Cells["clWhere"].Value != null)
{ {
if (dgvRow.Cells["clWhere"].Value.ToString().Trim() != "") if (dgvRow.Cells["clWhere"].Value.ToString().Trim() != "")
@ -448,34 +477,77 @@ namespace ExportDataToFile
sbSelectCommand.AppendLine("Where 1=1 And " + dgvRow.Cells["clWhere"].Value.ToString()); sbSelectCommand.AppendLine("Where 1=1 And " + dgvRow.Cells["clWhere"].Value.ToString());
} }
} }
#endregion
//取得完整SQL查詢Command
//txtWhere.Text = sbResult.ToString();
//txtWhere.Text = strTargetColumns; 目標欄位對應
#region 產生匯出資料語法
dtResult = GetMSSQLResult(sbSelectCommand.ToString()).Tables["Result"]; dtResult = GetMSSQLResult(sbSelectCommand.ToString()).Tables["Result"];
foreach (DataRow dr in dtResult.Rows)
{
foreach (DataColumn dc in dtResult.Columns)
if (dtResult.Rows.Count > 0)
{
if (dgvRow.Cells["clTargetTable"].Value != null)
{ {
if (strValues == "")
strTargetTable = dgvRow.Cells["clTargetTable"].Value.ToString();
Utility.WriteFile(strPath + ".sql", " ");
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");
}
//勾選清除目標資料表
if (dgvRow.Cells["clTableDel"].Value != null)
{
strDeleteCommand = string.Format("Delete From {0} ", strTargetTable);
if (dgvRow.Cells["clWhere"].Value != null)
{ {
strValues = "'" + dr[dc].ToString() + "'";
if (dgvRow.Cells["clWhere"].Value.ToString().Trim() != "")
{
strDeleteCommand += "Where 1=1 And " + dgvRow.Cells["clWhere"].Value.ToString();
}
} }
else
Utility.WriteFile(strPath + ".sql", strDeleteCommand + ";");
Utility.WriteFile(strPath + ".sql", "Go");
}
//處理筆數上限似資料匯出
string strFileCount = "";
foreach (DataRow dr in dtResult.Rows)
{
strFileCount = (intDataCount / intMaxData).ToString(); //切分檔案
foreach (DataColumn dc in dtResult.Columns)
{ {
strValues += ",'" + dr[dc].ToString() + "'";
if (strValues == "")
{
strValues = ConvertMSSQLDataType(dr[dc].ToString().Trim(), dc.DataType.ToString());
}
else
{
strValues += "," + ConvertMSSQLDataType(dr[dc].ToString().Trim(), dc.DataType.ToString());
}
} }
}
if (dgvRow.Cells["clTargetTable"].Value != null)
{
strTargetTable = dgvRow.Cells["clTargetTable"].Value.ToString();
sbInsertCommand.AppendLine(string.Format("Insert Into {0} ({1}) ",strSourceTable, strValues));
if (intMaxData == 0)
{
//無設定最大筆數
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;
strValues = ""; //清除已經存在的資料
} }
}
}
Utility.WriteFile(strPath, sbInsertCommand.ToString());
Utility.WriteFile(strPath + strFileCount + ".sql", "Go");
}
}
#endregion
} }
MessageBox.Show("匯出完成");
} }
#endregion //End of MS-SQL #endregion //End of MS-SQL
@ -486,7 +558,7 @@ namespace ExportDataToFile
{ {
string strResult = ""; string strResult = "";
if (cbSourceClass.SelectedItem == null)
if (cbSourceClass.SelectedIndex <= 0)
{ {
strResult = "請選擇來源資料庫類別"; strResult = "請選擇來源資料庫類別";
cbSourceClass.Focus(); cbSourceClass.Focus();
@ -529,8 +601,13 @@ namespace ExportDataToFile
try try
{ {
string strResult = ""; string strResult = "";
if (cbTargetClass.SelectedItem != null)
if (cbTargetClass.SelectedIndex <= 0)
{
strResult = "請選擇目標資料庫類別";
cbTargetClass.Focus();
return strResult;
}
else
{ {
if (txtTargetIP.Text.Trim() == "") if (txtTargetIP.Text.Trim() == "")
{ {
@ -639,6 +716,7 @@ namespace ExportDataToFile
string strExportColumns = ""; string strExportColumns = "";
string strSourceColumn = ""; string strSourceColumn = "";
string strTargetColumn = ""; string strTargetColumn = "";
string strSourceColType = "";
//預設所有欄位均匯出 //預設所有欄位均匯出
foreach (DataGridViewRow dgRow in dgvColumnMapping.Rows) foreach (DataGridViewRow dgRow in dgvColumnMapping.Rows)
@ -657,6 +735,15 @@ namespace ExportDataToFile
strSourceColumn = dgRow.Cells["clSourceColumn"].Value.ToString(); strSourceColumn = dgRow.Cells["clSourceColumn"].Value.ToString();
} }
if (dgRow.Cells["clType"].Value == null)
{
strSourceColType = " ";
}
else
{
strSourceColType = dgRow.Cells["clType"].Value.ToString();
}
if (dgRow.Cells["clTargetColumn"].Value == null) if (dgRow.Cells["clTargetColumn"].Value == null)
{ {
strTargetColumn = " "; strTargetColumn = " ";
@ -669,11 +756,11 @@ namespace ExportDataToFile
if (strExportColumns == "") if (strExportColumns == "")
{ {
strExportColumns += strSourceColumn + "," + strTargetColumn;
strExportColumns += strSourceColumn + "," + strSourceColType + ";" + strTargetColumn;
} }
else else
{ {
strExportColumns += "|" + strSourceColumn + "," + strTargetColumn;
strExportColumns += "|" + strSourceColumn + "," + strSourceColType + ";" + strTargetColumn;
} }
} }
} }
@ -779,7 +866,6 @@ namespace ExportDataToFile
senderGrid.Rows[intRowIndex].Cells["clExport"].Value = false; senderGrid.Rows[intRowIndex].Cells["clExport"].Value = false;
senderGrid.Rows[intRowIndex].Cells["clTargetTable"].Value = ""; senderGrid.Rows[intRowIndex].Cells["clTargetTable"].Value = "";
senderGrid.Rows[intRowIndex].Cells["clTableDel"].Value = false; senderGrid.Rows[intRowIndex].Cells["clTableDel"].Value = false;
senderGrid.Rows[intRowIndex].Cells["clDataTrim"].Value = false;
senderGrid.Rows[intRowIndex].Cells["clMaxRecord"].Value = ""; senderGrid.Rows[intRowIndex].Cells["clMaxRecord"].Value = "";
senderGrid.Rows[intRowIndex].Cells["clWhere"].Value = ""; senderGrid.Rows[intRowIndex].Cells["clWhere"].Value = "";
senderGrid.Rows[intRowIndex].Cells["clMappingData"].Value = ""; senderGrid.Rows[intRowIndex].Cells["clMappingData"].Value = "";
@ -794,8 +880,52 @@ namespace ExportDataToFile
CleanTable.Rows.Clear(); CleanTable.Rows.Clear();
dgvColumnMapping.DataSource = CleanTable; dgvColumnMapping.DataSource = CleanTable;
} }
private void EnableExport()
{
if (sqlSourceConn.State.ToString().ToUpper() == "OPEN" && sqlTargetConn.State.ToString().ToUpper() == "OPEN")
{
btnExport.Enabled = true;
}
}
private string ConvertMSSQLColumnType(string strSourceCol, string strTargetCol, string strType)
{
switch (strType.ToUpper())
{
case "VARCHAR":
return "[" + strSourceCol + "] AS " + strTargetCol ;
case "VAR":
return "[" + strSourceCol + "] AS " + strTargetCol ;
case "INT":
return "[" + strSourceCol + "] AS " + strTargetCol;
case "DATETIME":
return "Convert(varchar,[" + strSourceCol + "],21) AS " + strTargetCol;
default:
return "[" + strSourceCol + "] AS " + strTargetCol;
}
}
private string ConvertMSSQLDataType(string strData, string strType)
{
if(strData == "")
{
return "Null";
}
switch (strType.ToUpper())
{
case "STRING":
return "'" + strData.ToString().Trim() + "'";
case "INT32":
return strData.ToString().Trim();
case "DECIMAL":
return strData.ToString().Trim();
default:
return "'" + strData.ToString().Trim() + "'";
}
}
#endregion #endregion
} }
} }

6
Form1.resx

@ -132,9 +132,6 @@
<metadata name="clTableDel.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="clTableDel.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
<metadata name="clDataTrim.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<metadata name="clMaxRecord.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="clMaxRecord.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
@ -153,6 +150,9 @@
<metadata name="clSourceColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"> <metadata name="clSourceColumn.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value> <value>True</value>
</metadata> </metadata>
<metadata name="clType.UserAddedColumn" type="System.Boolean, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<value>True</value>
</metadata>
<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>

Loading…
Cancel
Save