|
|
@ -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
|
|
|
|
|
|
|
|
|
|
|
|