|
|
@ -142,28 +142,7 @@ namespace ExportDataToFile |
|
|
|
{ |
|
|
|
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) |
|
|
|
{ |
|
|
@ -176,16 +155,14 @@ namespace ExportDataToFile |
|
|
|
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 = ""; |
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
@ -259,20 +236,71 @@ namespace ExportDataToFile |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
dgvColumnMapping.Rows[e.RowIndex].Cells["clExpColumn"].Value = true; |
|
|
|
if (cbTargetTable.Enabled) |
|
|
|
{ |
|
|
|
//dgvColumnMapping.Rows[e.RowIndex].Cells["clExpColumn"].Value = true;
|
|
|
|
//dgvExportList.Rows[intExportIndex].Cells["clExport"].Value = true;
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
ErrorHandler.WriteErrorLog("Form1.cs", ex); |
|
|
|
} |
|
|
|
} |
|
|
|
private void btnExport_Click(object sender, EventArgs e) |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
{ |
|
|
|
ErrorHandler.WriteErrorLog("Form1.cs", ex); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
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 btnExport_Click(object sender, EventArgs e) |
|
|
|
|
|
|
|
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) |
|
|
|
{ |
|
|
@ -280,7 +308,34 @@ namespace ExportDataToFile |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#region 自定義功能
|
|
|
|
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() |
|
|
|
{ |
|
|
@ -436,63 +491,156 @@ namespace ExportDataToFile |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
#region MS-SQL
|
|
|
|
private void ShowMSSQLSourceTables(SqlConnection sqlConn) |
|
|
|
private string GenMappingColumn() |
|
|
|
{ |
|
|
|
try |
|
|
|
string strExportColumns = ""; |
|
|
|
string strSourceColumn = ""; |
|
|
|
string strTargetColumn = ""; |
|
|
|
|
|
|
|
//預設所有欄位均匯出
|
|
|
|
foreach (DataGridViewRow dgRow in dgvColumnMapping.Rows) |
|
|
|
{ |
|
|
|
string strGetMSSQLTableList = "Select [name] as TableName from sys.tables order by name"; |
|
|
|
dgvExportList.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLTableList, sqlConn).Tables["Result"]; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
//處理下拉欄位空白的問題
|
|
|
|
if (dgRow.Cells["clExpColumn"].Value != null) |
|
|
|
{ |
|
|
|
ErrorHandler.WriteErrorLog("Form1.cs", ex); |
|
|
|
if ((bool)dgRow.Cells["clExpColumn"].Value == true) |
|
|
|
{ |
|
|
|
if (dgRow.Cells["clSourceColumn"].Value == null) |
|
|
|
{ |
|
|
|
strSourceColumn = " "; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
strSourceColumn = dgRow.Cells["clSourceColumn"].Value.ToString(); |
|
|
|
} |
|
|
|
|
|
|
|
private void ShowMSSQLTargetTableList(SqlConnection sqlConn) |
|
|
|
if (dgRow.Cells["clTargetColumn"].Value == null) |
|
|
|
{ |
|
|
|
try |
|
|
|
strTargetColumn = " "; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
string strGetMSSQLTableList = "Select [name] as TableName from sys.tables order by name"; |
|
|
|
cbTargetTable.DataSource = MSSQLUtility.GetSQLResult(strGetMSSQLTableList, sqlConn).Tables["Result"]; |
|
|
|
strTargetColumn = dgRow.Cells["clTargetColumn"].Value.ToString(); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
|
|
|
|
if (strExportColumns == "") |
|
|
|
{ |
|
|
|
ErrorHandler.WriteErrorLog("Form1.cs", ex); |
|
|
|
|
|
|
|
strExportColumns += strSourceColumn + "," + strTargetColumn; |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
|
strExportColumns += "|" + strSourceColumn + "," + strTargetColumn; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void ShowMSSQLSourceColumnList(SqlConnection sqlConn, string strTableName) |
|
|
|
return strExportColumns; |
|
|
|
} |
|
|
|
|
|
|
|
private void LocatedTarget(int intRowIndex) |
|
|
|
{ |
|
|
|
try |
|
|
|
if (cbTargetClass.SelectedIndex >= 0 && cbTargetClass.Enabled == false) |
|
|
|
{ |
|
|
|
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"]; |
|
|
|
//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(); |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
else |
|
|
|
{ |
|
|
|
ErrorHandler.WriteErrorLog("Form1.cs", ex); |
|
|
|
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('|'); |
|
|
|
} |
|
|
|
private void ShowMSSQLTargetColumnList(SqlConnection sqlConn, string strTableName) |
|
|
|
|
|
|
|
|
|
|
|
//顯示欄位對應表的內容
|
|
|
|
if (cbTargetTable.SelectedValue != null) |
|
|
|
{ |
|
|
|
try |
|
|
|
switch (cbTargetClass.SelectedItem.ToString().Trim()) |
|
|
|
{ |
|
|
|
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"]; |
|
|
|
case "MS-SQL": |
|
|
|
ShowMSSQLSourceColumnList(sqlSourceConn, strTarget.ToString()); |
|
|
|
Application.DoEvents(); |
|
|
|
break; |
|
|
|
case "MySQL": |
|
|
|
|
|
|
|
break; |
|
|
|
case "Oracle": |
|
|
|
|
|
|
|
break; |
|
|
|
case "PostgreSQL": |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
catch (Exception ex) |
|
|
|
} |
|
|
|
|
|
|
|
if (strColumnMapping != null) |
|
|
|
{ |
|
|
|
ErrorHandler.WriteErrorLog("Form1.cs", ex); |
|
|
|
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 CleanExpRow(DataGridView senderGrid, int intRowIndex) |
|
|
|
{ |
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|