frmExport.cs
上传用户:lyg_rssy
上传日期:2022-04-14
资源大小:104k
文件大小:28k
源码类别:

数据库系统

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Data.SqlClient;
  10. using System.IO;
  11. using System.Threading;
  12. namespace HKBU_DataExport
  13. {
  14.     public partial class frmExportData : Form
  15.     {
  16.         public frmExportData()
  17.         {
  18.             InitializeComponent();
  19.         }
  20.         private static List<Thread> backendThread = new List<Thread>();
  21.         private static List<string> backendThreadName = new List<string>();
  22.         private const string TempFilePath = "";
  23.         private static string GoodlistFileName = "\" + Properties.Settings.Default.Table1 + ".ecp";
  24.         private static string VisitorFileName = "\" + Properties.Settings.Default.Table2  + ".ecp";
  25.         private const string TempFileName = "\Temp.ecp";
  26.         private const string LogFileName = "\Log.txt";
  27.         private const string Key = "P@ssw0rd";
  28.         private StreamWriter UpdateLog = null;
  29.         private bool isTable1Updated = false;
  30.         private bool isTable2Updated = false;
  31.         private string NullValue = "NULL";
  32.         private object updateLock = new object();
  33.         private const string FILE_UPDATE_TABLE = "UpdateTable_log";
  34.         private const string LOG_SEPERATOR = "-----------------------------------------------";
  35.         private const char FileSeperator = 't';
  36.         private delegate void InvokeDelegate(string msg);
  37.         private delegate void setupGVDelegate(DataGridView gvData, string file);
  38.         private void frmExportData_Load(object sender, EventArgs e)
  39.         {            
  40.             try
  41.             {
  42.                 if (!File.Exists(Properties.Settings.Default.Output_Path))
  43.                 {
  44.                     Directory.CreateDirectory(Properties.Settings.Default.Output_Path);
  45.                 }
  46.                 UpdateLog = new StreamWriter(Properties.Settings.Default.Output_Path + LogFileName, true);
  47.                 UpdateLog.AutoFlush = true;
  48.                 UpdateLog.WriteLine("------------------------------------------------------------------------------------------------------");
  49.                 UpdateLog.WriteLine("Application Launched At: " + DateTime.Today.ToString("yyyy/MM/dd HH:mm:ss"));
  50.                 UpdateLog.WriteLine ("------------------------------------------------------------------------------------------------------");
  51.                 writeLog("System Loading ...");
  52.                 txtOutputPath.Text = Properties.Settings.Default.Output_Path;
  53.                 txtUpdateHour.Text = Properties.Settings.Default.update_hour.ToString();
  54.                 txtUpdateMinute.Text = Properties.Settings.Default.update_minute.ToString();
  55.                 txtConnectionString.Text = Properties.Settings.Default.ConnectionString;
  56.                 txtTable1.Text = Properties.Settings.Default.Table1;
  57.                 txtTable2.Text = Properties.Settings.Default.Table2;
  58.                 btnTable1.Text = Properties.Settings.Default.Table1;
  59.                 btnTable2.Text = Properties.Settings.Default.Table2;
  60.                 writeLog("Initialize Completed");
  61.             }
  62.             catch (Exception ex)
  63.             {
  64.                 MessageBox.Show("Initialize System Failed, Error Message: " + ex.ToString());
  65.             }
  66.         }
  67.         private void setupGridView(DataGridView gvData,string table)
  68.         {
  69.             if (this.InvokeRequired)
  70.             {
  71.                 this.Invoke(new setupGVDelegate(setupGridView),gvData,table);
  72.             }
  73.             else
  74.             {
  75.                 try
  76.                 {
  77.                     using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.ConnectionString))
  78.                     {
  79.                         string command = "Select * from " + table;
  80.                         using (SqlDataAdapter sda = new SqlDataAdapter(command, conn))
  81.                         {
  82.                             DataSet ds = new DataSet();
  83.                             sda.Fill(ds);
  84.                             gvData.DataSource = ds.Tables[0];
  85.                         }
  86.                     }
  87.                 }
  88.                 catch (Exception ex)
  89.                 {
  90.                     MessageBox.Show("Open Data Table Failed, Error Message: " + ex.ToString(), "Error", MessageBoxButtons.OK);
  91.                     writeLog("Open Data Table Failed, Error Message: " + ex.ToString());
  92.                 }
  93.             }
  94.         }
  95.         private void btnSave_Click(object sender, EventArgs e)
  96.         {
  97.             writeLog("------------------------------------------------");
  98.             try
  99.             {                
  100.                 this.Cursor = Cursors.WaitCursor;
  101.                 SaveFileDialog saveFileDialog = new SaveFileDialog();
  102.                 saveFileDialog.Filter = "Encrypted files (*.ecp)|*.ecp|Excel files (*.xls)|*.xls|Comma Separated Value File(*.csv)|*.csv|Microsoft SQL Server Query File(*.sql)|*.sql";
  103.                 saveFileDialog.FilterIndex = 0;
  104.                 saveFileDialog.RestoreDirectory = true;
  105.                 saveFileDialog.CreatePrompt = true;
  106.                 saveFileDialog.Title = "Export File To";
  107.                 string filePath = "";
  108.                 string msg;
  109.                 if (saveFileDialog.ShowDialog() == DialogResult.OK)
  110.                 {
  111.                     filePath = saveFileDialog.FileName;                    
  112.                     writeLog("Saving file as: " + filePath);
  113.                     string tempPath = Properties.Settings.Default.Output_Path + TempFileName;                        
  114.                     string extension = filePath.Split('.').Last().ToLower();        
  115.                     switch(extension)
  116.                     {
  117.                         case "sql":
  118.                             SaveAs(gvDataView, tempPath, FileSeperator);
  119.                             generateSqlFile(filePath,tempPath,btnTable1.Enabled?btnTable2.Text.Trim():btnTable1.Text.Trim());
  120.                             break;
  121.                         case "ecp":
  122.                             SaveAs(gvDataView, tempPath, FileSeperator);
  123.                             if (EncryptFunction.EncryptFile(tempPath, filePath, Key, out msg))
  124.                             try
  125.                             {
  126.                                 File.Delete(tempPath);
  127.                             }
  128.                             catch { }
  129.                             break;
  130.                         case "csv":
  131.                             SaveAs(gvDataView, filePath, ',');
  132.                             break;
  133.                         case "xls":
  134.                             SaveAs(gvDataView, filePath,FileSeperator);
  135.                             break;
  136.                     }                
  137.                     System.Windows.Forms.MessageBox.Show("File Saved", "Message", MessageBoxButtons.OK);
  138.                     writeLog("File Saved");                    
  139.                 }
  140.                 this.Cursor = Cursors.Default;
  141.             }
  142.             catch (Exception ex)
  143.             {
  144.                 System.Windows.Forms.MessageBox.Show("Error occured, Error Message: n" + ex.ToString(), "Message", MessageBoxButtons.OK);
  145.                 writeLog("Error occured, Error Message: n" + ex.ToString());
  146.                 this.Cursor = Cursors.Default;
  147.             }
  148.         }
  149.         /// <summary>
  150.         /// 另存新档按钮
  151.         /// </summary>
  152.         private void SaveAs(DataGridView gvData,string fileName,char seperator) //另存新档按钮   导出成CSV
  153.         {
  154.             Stream myStream;
  155.             myStream = File.Open(fileName, FileMode.Create);            
  156.             StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.UTF8);
  157.             string str = "";
  158.             try
  159.             {
  160.                 //写标题
  161.                 for (int i = 0; i < gvData.ColumnCount; i++)
  162.                 {
  163.                     if (i > 0)
  164.                     {
  165.                         str += seperator;
  166.                     }
  167.                     str += gvData.Columns[i].HeaderText;
  168.                 }
  169.                 sw.WriteLine(str);
  170.                 for (int j = 0; j < gvData.Rows.Count; j++)
  171.                 {
  172.                     string tempStr = "";
  173.                     for (int k = 0; k < gvData.Columns.Count; k++)
  174.                     {
  175.                         if (k > 0)
  176.                         {
  177.                             tempStr += seperator;
  178.                         }
  179.                         if (gvData.Rows[j].Cells[k].Value == gvData.DefaultCellStyle.NullValue || gvData.Rows[j].Cells[k].Value == DBNull.Value)
  180.                         {
  181.                             tempStr += NullValue;
  182.                         }
  183.                         else if (gvData.Rows[j].Cells[k].ValueType.Name.ToLower().CompareTo("datetime") == 0)
  184.                         {
  185.                             tempStr += Convert.ToDateTime(gvData.Rows[j].Cells[k].Value).ToString("yyyy-MM-dd HH:mm:ss");                            
  186.                         }
  187.                         else
  188.                         {
  189.                             tempStr += gvData.Rows[j].Cells[k].Value.ToString();
  190.                         }
  191.                     }
  192.                     sw.WriteLine(tempStr);   
  193.                 }
  194.                 sw.Close();
  195.                 myStream.Close();
  196.             }
  197.             catch (Exception e)
  198.             {
  199.                 MessageBox.Show(e.ToString());
  200.                 writeLog("Export File Error, Error Message: " + e.ToString());
  201.             }
  202.             finally
  203.             {
  204.                 sw.Close();
  205.                 myStream.Close();
  206.             }   
  207.       }
  208.         private void btnOutputPath_Click(object sender, EventArgs e)
  209.         {
  210.             FolderBrowserDialog fbdOutputPath = new FolderBrowserDialog();
  211.             fbdOutputPath.RootFolder = Environment.SpecialFolder.Desktop;
  212.             if (fbdOutputPath.ShowDialog() == DialogResult.OK)
  213.             {
  214.                 txtOutputPath.Text = fbdOutputPath.SelectedPath;
  215.             }
  216.         }
  217.         private void changeOutputPath(string origin_path, string new_path)
  218.         {
  219.             string msg = "";
  220.             writeLog("Changing OutputPath...");
  221.             writeLog("Moving Log File to New Output Floder...");
  222.             UpdateLog.Close();
  223.             if (EncryptFunction.EncryptFile(origin_path + LogFileName, origin_path + TempFileName, Key, out msg))
  224.             {
  225.                 if (EncryptFunction.DecryptFile(origin_path + TempFileName, new_path + LogFileName, Key, out msg))
  226.                 {
  227.                     Properties.Settings.Default.Output_Path = new_path;
  228.                     UpdateLog = new StreamWriter(Properties.Settings.Default.Output_Path + LogFileName, true);
  229.                     UpdateLog.AutoFlush = true;
  230.                     try
  231.                     {
  232.                         File.Delete(origin_path + LogFileName);
  233.                     }
  234.                     catch { }
  235.                     writeLog("Output Path Changed");
  236.                 }
  237.                 else
  238.                 {
  239.                     Properties.Settings.Default.Output_Path = origin_path;
  240.                     UpdateLog = new StreamWriter(Properties.Settings.Default.Output_Path + LogFileName, true);
  241.                     UpdateLog.AutoFlush = true;
  242.                     writeLog("Change Output Path Failed, Error Message: " + msg);
  243.                     return;
  244.                 }
  245.             }
  246.             else
  247.             {
  248.                 Properties.Settings.Default.Output_Path = origin_path;
  249.                 UpdateLog = new StreamWriter(Properties.Settings.Default.Output_Path + LogFileName, true);
  250.                 UpdateLog.AutoFlush = true;
  251.                 writeLog("Change Output Path Failed, Error Message: " + msg);
  252.             }
  253.             try
  254.             {                
  255.                 File.Delete(origin_path + TempFileName);                
  256.             }
  257.             catch(Exception ex)
  258.             { }
  259.         }
  260.         private void btnSaveSetting_Click(object sender, EventArgs e)
  261.         {
  262.             try
  263.             {
  264.                 writeLog("------------------------------------------------");
  265.                 writeLog("Modifying System Setting");
  266.                 writeLog("Original Output Path: " + Properties.Settings.Default.Output_Path);
  267.                 writeLog("Original Update Time: " + Properties.Settings.Default.update_hour + " : " + Properties.Settings.Default.update_minute);
  268.                 writeLog("Original Connection String: " + Properties.Settings.Default.ConnectionString);
  269.                 writeLog("Original Table 1: " + Properties.Settings.Default.Table1);
  270.                 writeLog("Original Table 2: " + Properties.Settings.Default.Table2);
  271.                 changeOutputPath(Properties.Settings.Default.Output_Path, txtOutputPath.Text);
  272.                 Properties.Settings.Default.update_hour = Int32.Parse(txtUpdateHour.Text);
  273.                 Properties.Settings.Default.update_minute = Int32.Parse(txtUpdateMinute.Text);
  274.                 Properties.Settings.Default.ConnectionString = txtConnectionString.Text;
  275.                 Properties.Settings.Default.Table1 = txtTable1.Text;
  276.                 Properties.Settings.Default.Table2 = txtTable2.Text;
  277.                 btnTable1.Text = txtTable1.Text;
  278.                 btnTable2.Text = txtTable2.Text;
  279.                 System.Windows.Forms.MessageBox.Show("System Setting Saved", "Message", MessageBoxButtons.OK);
  280.                 writeLog("System Setting Modified");
  281.                 writeLog("Current Output Path: " + txtOutputPath.Text);
  282.                 writeLog("Current Update Time: " + txtUpdateHour.Text + " : " + txtUpdateMinute.Text);
  283.                 writeLog("Current Connection String: " + txtConnectionString.Text);
  284.                 writeLog("Current Table 1: " + txtTable1.Text);
  285.                 writeLog("Current Table 2: " + txtTable2.Text);
  286.             }
  287.             catch (Exception ex)
  288.             {
  289.                 System.Windows.Forms.MessageBox.Show("Error occured, Error Message: n" + ex.ToString(), "Message", MessageBoxButtons.OK);
  290.                 writeLog("Error occured, Error Message: n" + ex.ToString());
  291.                 writeLog("Original Output Path: " + Properties.Settings.Default.Output_Path);
  292.                 writeLog("Original Update Time: " + Properties.Settings.Default.update_hour + " : " + Properties.Settings.Default.update_minute);
  293.                 writeLog("Original Connection String: " + Properties.Settings.Default.ConnectionString);
  294.                 writeLog("Original Table 1: " + Properties.Settings.Default.Table1);
  295.                 writeLog("Original Table 2: " + Properties.Settings.Default.Table2);
  296.             }
  297.             finally
  298.             {
  299.                 resetManualPanel();
  300.             }
  301.         }
  302.         private void exportGoodlist()
  303.         {
  304.             try
  305.             {
  306.                 this.Cursor = Cursors.WaitCursor;
  307.                 if (!isTable1Updated)
  308.                 {
  309.                     writeLog("------------------------------------------------");
  310.                     writeLog("Exproting Data Table: [" + Properties.Settings.Default.Table1 + "]");
  311.                     string inputfilePath = Properties.Settings.Default.Output_Path + TempFilePath;
  312.                     if (!File.Exists(inputfilePath)) Directory.CreateDirectory(inputfilePath);
  313.                     string outputfilePath = Properties.Settings.Default.Output_Path;
  314.                     if (!File.Exists(outputfilePath)) Directory.CreateDirectory(outputfilePath);
  315.                     inputfilePath += TempFileName;
  316.                     outputfilePath += GoodlistFileName;
  317.                     string msg = "";
  318.                     setupGridView(gvGoodlist, Properties.Settings.Default.Table1);
  319.                     SaveAs(gvGoodlist, inputfilePath, FileSeperator);
  320.                     this.Cursor = Cursors.WaitCursor;
  321.                     if (EncryptFunction.EncryptFile(inputfilePath, outputfilePath, Key, out msg))
  322.                     {
  323.                         writeLog("Data Table: [" + Properties.Settings.Default.Table1 + "] Exported");
  324.                         isTable1Updated = true;
  325.                     }
  326.                     else
  327.                     {
  328.                         writeLog("Error occured, Error Message: " + msg);
  329.                     }
  330.                     try
  331.                     {
  332.                         File.Delete(inputfilePath);
  333.                     }
  334.                     catch { }
  335.                 }
  336.             }
  337.             catch
  338.             {
  339.                 isTable1Updated = false;
  340.             }
  341.             this.Cursor = Cursors.Default;
  342.         }
  343.         private void exportVisitor()
  344.         {
  345.             try
  346.             {
  347.                 this.Cursor = Cursors.WaitCursor;
  348.                 if (!isTable2Updated)
  349.                 {
  350.                     writeLog("------------------------------------------------");
  351.                     writeLog("Exproting Data Table: [" + Properties.Settings.Default.Table2 + "]");
  352.                     string inputfilePath = Properties.Settings.Default.Output_Path + TempFilePath;
  353.                     if (!File.Exists(inputfilePath)) Directory.CreateDirectory(inputfilePath);
  354.                     string outputfilePath = Properties.Settings.Default.Output_Path;
  355.                     if (!File.Exists(outputfilePath)) Directory.CreateDirectory(outputfilePath);
  356.                     inputfilePath += TempFileName;
  357.                     outputfilePath += VisitorFileName;
  358.                     string msg = "";
  359.                     setupGridView(gvVisitor, Properties.Settings.Default.Table2);
  360.                     SaveAs(gvVisitor, inputfilePath, FileSeperator);
  361.                     this.Cursor = Cursors.WaitCursor;
  362.                     if (EncryptFunction.EncryptFile(inputfilePath, outputfilePath, Key, out msg))
  363.                     {
  364.                         writeLog("Data Table: [" + Properties.Settings.Default.Table2 + "] Exported");
  365.                         isTable2Updated = true;
  366.                     }
  367.                     else
  368.                     {
  369.                         writeLog("Error occured, Error Message: " + msg);
  370.                     }
  371.                     try
  372.                     {
  373.                         File.Delete(inputfilePath);
  374.                     }
  375.                     catch { }
  376.                 }
  377.             }
  378.             catch
  379.             {
  380.                 isTable2Updated = false;
  381.             }
  382.             this.Cursor = Cursors.Default;
  383.         }       
  384.         private void btnStart_Click(object sender, EventArgs e)
  385.         {
  386.             writeLog("------------------------------------------------");
  387.             writeLog("Launching Update Threads...");
  388.             Thread tdExportTable = new Thread(ExportTableInSchedule);
  389.             tdExportTable.Start();
  390.             writeLog("Update Thread Started");
  391.             backendThread.Insert(0,tdExportTable);
  392.             backendThreadName.Insert(0,"ExportTable");
  393.             btnStart.Enabled = false;
  394.             btnStop.Enabled = true;
  395.             writeLog("Update Threads Launched");
  396.         }
  397.         private void btnStop_Click(object sender, EventArgs e)
  398.         {
  399.             writeLog("------------------------------------------------");
  400.             writeLog("Aborting Update Threads...");
  401.             foreach (Thread td in backendThread)
  402.             {
  403.                 td.Abort();             
  404.             }
  405.             backendThread.Clear();
  406.             backendThreadName.Clear();
  407.             btnStart.Enabled = true;
  408.             btnStop.Enabled = false;
  409.             writeLog("All Thread Aborted");
  410.         }
  411.         private void btnTable2_Click(object sender, EventArgs e)
  412.         {
  413.             writeLog("------------------------------------------------");
  414.             writeLog("Opening Data Table [" + Properties.Settings.Default.Table2 + "] ...");
  415.             this.Cursor = Cursors.WaitCursor;
  416.             btnTable1.Enabled = true;
  417.             btnTable2.Enabled = false;
  418.             btnSave.Enabled = true;
  419.             setupGridView(gvDataView, Properties.Settings.Default.Table2);
  420.             writeLog("Data Table [" + Properties.Settings.Default.Table2 + "] Opened");
  421.         }
  422.         private void btnTable1_Click(object sender, EventArgs e)
  423.         {
  424.             writeLog("------------------------------------------------");
  425.             writeLog("Opening Data Table [" + Properties.Settings.Default.Table1 + "] ...");
  426.             this.Cursor = Cursors.WaitCursor;
  427.             btnTable1.Enabled = false;
  428.             btnTable2.Enabled = true;
  429.             btnSave.Enabled = true;
  430.             setupGridView(gvDataView,Properties.Settings.Default.Table1);
  431.             writeLog("Data Table [" + Properties.Settings.Default.Table1 + "] Opened");
  432.         }
  433.         private void writeLog(string msg)
  434.         {            
  435.             if (this.InvokeRequired)
  436.             {
  437.                 this.Invoke(new InvokeDelegate(writeLog), msg);
  438.             }
  439.             else
  440.             {
  441.                 txtMessage.AppendText("[ " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "]:t" + msg + "n");
  442.                 txtMessage.ScrollToCaret();
  443.                 UpdateLog.WriteLine("[ " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "]:t" + msg );
  444.             }
  445.         }
  446.         private void btnKey_Click(object sender, EventArgs e)
  447.         {
  448.             writeLog("------------------------------------------------");
  449.             if (btnKey.Text.Trim().CompareTo("Confirm") == 0)
  450.             {
  451.                 if (txtKey.Text.Trim().CompareTo(Key) == 0)
  452.                 {
  453.                     plManual.Enabled = true;
  454.                     plSetting.Enabled = true;
  455.                     btnKey.Text = "Lock";
  456.                     txtKey.Enabled = false;
  457.                     System.Windows.Forms.MessageBox.Show("Administrator Control Unlocked", "Message", MessageBoxButtons.OK);
  458.                     writeLog("Access Administrator Control Mode");                 
  459.                 }
  460.                 else
  461.                 {
  462.                     System.Windows.Forms.MessageBox.Show("Access Administrator Control Mode Failed, Please Check Key.", "Message", MessageBoxButtons.OK);
  463.                     writeLog("Access Administrator Control Mode Faile");
  464.                 }
  465.             }
  466.             else
  467.             {
  468.                 resetManualPanel();
  469.                 plManual.Enabled = false;
  470.                 plSetting.Enabled = false;
  471.                 txtKey.Enabled = true;
  472.                 txtKey.Text = "";
  473.                 btnKey.Text = "Confirm";
  474.                 System.Windows.Forms.MessageBox.Show("Administrator Control Locked", "Message", MessageBoxButtons.OK);
  475.                 writeLog("Lock Administrator Control Mode");
  476.             }
  477.         }
  478.         private void resetManualPanel()
  479.         {
  480.             gvDataView.DataSource = null;
  481.             btnTable1.Enabled = true;
  482.             btnTable2.Enabled = true;
  483.             btnSave.Enabled = false;
  484.         }
  485.         private void DataView_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
  486.         {            
  487.             this.Cursor = Cursors.Default;
  488.         }
  489.         private void frmExportData_FormClosing(object sender, FormClosingEventArgs e)
  490.         {
  491.             if (MessageBox.Show("Are you sure to Exit?", "Message", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
  492.             {
  493.                 e.Cancel = true;
  494.             }
  495.             else
  496.             {
  497.                 try
  498.                 {
  499.                     File.Delete(Properties.Settings.Default.Output_Path + TempFileName);
  500.                 }
  501.                 catch { }
  502.                 UpdateLog.WriteLine("------------------------------------------------------------------------------------------------------");
  503.                 UpdateLog.WriteLine("Application Aborted At: " + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));
  504.                 UpdateLog.WriteLine("------------------------------------------------------------------------------------------------------");
  505.                 UpdateLog.Close();
  506.             }
  507.         }
  508.         private void ExportTableInSchedule()
  509.         {
  510.             while (true)
  511.             {
  512.                 if (DateTime.Now.Hour == Properties.Settings.Default.update_hour && DateTime.Now.Minute == Properties.Settings.Default.update_minute)
  513.                 {
  514.                     exportGoodlist();
  515.                     exportVisitor();
  516.                 }
  517.                 else
  518.                 {
  519.                     isTable1Updated = false;
  520.                     isTable2Updated = false;
  521.                     Thread.Sleep(Properties.Settings.Default.update_frequency * 1000);
  522.                 }
  523.             }
  524.         }
  525.         private void btnExportAtOnce_Click(object sender, EventArgs e)
  526.         {
  527.             btnExportAtOnce.Enabled = false;
  528.             exportGoodlist();
  529.             exportVisitor();
  530.             isTable1Updated = false;
  531.             isTable2Updated = false;
  532.             MessageBox.Show("Operation Completed", "Message", MessageBoxButtons.OK);
  533.             btnExportAtOnce.Enabled = true;
  534.         }
  535.         public bool generateSqlFile(string outFilePath, string inFilePath, string tableName)
  536.         {
  537.             int successRecord = 0;
  538.             int failRecord = 0;
  539.             string inputPath = inFilePath;
  540.             StreamReader inputFile = new StreamReader(inputPath, System.Text.Encoding.UTF8);
  541.             StreamWriter updatelog = new StreamWriter(outFilePath,false,System.Text.Encoding.Unicode);
  542.             try
  543.             {
  544.                 string tempLine = inputFile.ReadLine(); // skip header(first line)
  545.                 string[] values;
  546.                 string tempCmd = "";
  547.                 int i = 0;
  548.                 values = Properties.Settings.Default.ConnectionString.Split(';');
  549.                 values = values[1].Split('=');
  550.                 tempCmd = "USE [" + values[1].Trim() + "]";
  551.                 WriteLine(updatelog, tempCmd);
  552.                 WriteLine(updatelog, "GO");
  553.                 while (!inputFile.EndOfStream)
  554.                 {
  555.                     tempLine = inputFile.ReadLine();
  556.                     values = tempLine.Split(FileSeperator);
  557.                     tempCmd = "Insert into [" + tableName + "] Values (";
  558.                     for (i = 0; i < values.Count() - 1; i++)
  559.                     {
  560.                         if (values[i].CompareTo(NullValue) == 0)
  561.                         {
  562.                             tempCmd += "NULL,";
  563.                         }
  564.                         else
  565.                         {
  566.                             tempCmd += "'" + values[i].Replace("'", "''") + "', ";
  567.                         }
  568.                     }
  569.                     if (values[i].CompareTo(NullValue) == 0)
  570.                     {
  571.                         tempCmd += "NULL)";
  572.                     }
  573.                     else
  574.                     {
  575.                         tempCmd += "'" + values[i].Replace("'", "''") + "')";
  576.                     }
  577.                     WriteLine(updatelog, tempCmd);
  578.                 }
  579.                 WriteLine(updatelog, "--" + LOG_SEPERATOR);
  580.                 WriteLine(updatelog, "--Finish Updating  At: " + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
  581.                 WriteLine(updatelog, "--Total Updated Records: " + (successRecord + failRecord).ToString());
  582.                 WriteLine(updatelog, "--Successed Records: " + (successRecord).ToString());
  583.                 WriteLine(updatelog, "--Failed Records: " + (failRecord).ToString());
  584.                 inputFile.Close();
  585.                 File.Delete(inputPath);
  586.                 updatelog.Flush();
  587.                 updatelog.Close();
  588.                 return true;
  589.             }
  590.             catch (Exception ex)
  591.             {
  592.                 WriteLine(updatelog, "--" + LOG_SEPERATOR);
  593.                 WriteLine(updatelog, "--Error Occured, Error Message: " + ex.ToString());
  594.                 inputFile.Close();
  595.                 File.Delete(inputPath);
  596.                 return false;
  597.             }
  598.         }
  599.         private void WriteLine(StreamWriter updatelog,string msg)
  600.         {
  601.             updatelog.WriteLine(msg);
  602.         }
  603.     }
  604. }