各位前輩好,小弟寫了Oracle資料轉到SQL Server的轉檔程式,目前已經算是完成,但是想加入在讀取資料時,顯示"讀取資料中","已處理第幾筆/共幾筆資料","處理完畢"等字樣,請問該怎麼寫呢?能否給點方向呢?謝謝!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.OracleClient;
using System.IO;
namespace DV_VEHICLERBOLINE
{
public partial class Form1 : Form
{
#region 資料庫連線宣告
private static string xxxx = System.Configuration.ConfigurationManager.ConnectionStrings["xxxx"].ToString();
private static string xxxx = System.Configuration.ConfigurationManager.ConnectionStrings["xxxx"].ToString();
#endregion
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt1 = Get_SQL_DATA();
InsertSQL(dt1);
UpdateSQL(dt1);
}
#region SELECT ORACLE DV_VEHICLERBOLINE
private DataTable Get_SQL_DATA()
{
DataTable dt = new DataTable("dt");
using (OracleConnection cn = new OracleConnection())
{
string today = DateTime.Now.ToString("yyyy-MM-dd");
string lasttwoday = DateTime.Now.AddDays(-3).ToString("yyyy-MM-dd");
cn.ConnectionString = CS_YN;
cn.Open();
String strOLE = " ";
OracleCommand cmd = new OracleCommand(strOLE, cn);
OracleDataReader dr = cmd.ExecuteReader();
dt.Load(dr);
cn.Close();
}
return dt;
}
#endregion
#region INSERT INTO SQL
private void InsertSQL(DataTable dt)
{
using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = xxxx;
cn.Open();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
int cnt = i + 1 ;
label1.Text = "已處理第" + cnt + "筆/共" + dt.Rows.Count + "筆資料";
System.Windows.Forms.Application.DoEvents();
try
{
string strSQL = "";
}
catch(Exception e)
{
//今天日期
DateTime Date_log = DateTime.Now;
string TodayMillisecond = Date_log.ToString("yyyy-MM-dd HH:mm:ss");
string Today_log = Date_log.ToString("yyyy-MM-dd");
//把例外狀況寫到目的檔案,若檔案存在則附加在原本內容之後(換行)
File.AppendAllText("D:\\tran_log\\" + Today_log + ".txt", "\r\n" + TodayMillisecond + " : " + e);
this.Close();
}
}
cn.Close();
}
}
#endregion
#region UPDATE SQL
private void UpdateSQL(DataTable dt)
{
using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = xxxx;
cn.Open();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
try
{
string strSQL = "";
}
catch(Exception e)
{
//今日日期
DateTime Date_log = DateTime.Now;
string TodyMillisecond = Date_log.ToString("yyyy-MM-dd HH:mm:ss");
string Today_log = Date_log.ToString("yyyy-MM-dd");
//把例外狀況寫到目的檔案,若檔案存在則附加在原本內容之後(換行)
File.AppendAllText("D:\\tran_log\\" + Today_log + ".txt", "\r\n" + TodyMillisecond + ":" + e);
this.Close();
}
}
cn.Close();
}
}
#endregion
}
}
小弟在其他論壇找到解決方法,如下:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Thread t = new Thread(Dowork);
t.Start();
}
private void DoWork()
{
DataTable dt = new DataTable();
using (OracleConnection oracn = new OracleConnection())
{
//部分語法省略
Thread.Sleep(100);
UupdateLabel("資料讀取中...");
}
using (SqlConnection sqlcn = new SqlConnection())
{
//部分語法省略
int cnt = 1;
for (int i = 0; i <= dt.Rows.Count -1; i++)
{
Thread.Sleep(100);
UupdateLabel("已處理 " + cnt.ToString() + " / " + dt.Rows.Count + " 筆資料");
cnt++;
}
UupdateLabel("處理完畢");
}
Thread.Sleep(500);
Application.Exit();
}
private void UupdateLabel(string text)
{
if (this.InvokeRequired)
{
Action action = () => { labe2.Text = text; };
this.Invoke(action);
}
else
{
labe2.Text = text;
}
}
}
感謝各位前輩的協助喔!! 感激不盡~
用個Label吧,
就可以顯示訊息了,
也可以考慮進度條,
不過在程式運行中通常不會馬上更新,
可以加個 System.Windows.Forms.Application.DoEvents();
就會先更新畫面再繼續往下跑了。
private void InsertSQL(DataTable dt)
{
int cnt = 0;
foreach(DataRow row in dt.Rows)
{
//原來轉檔的程式碼
cnt++;
label1.Text = "已處理第" + cnt.ToString() + "筆/共" + dt.Rows.Count().ToString() + "筆資料"
}
}
回海綿寶寶大
我照您的方法做,可以成功顯示字串,但是卻不會動態顯示,只會顯示最後的結果,而且我發現整個資料處理動作都跑完了,查看資料也新增完畢,form才會顯示,請問接下要怎麼改呢?謝謝!
小魚大不是有教了....
在程式裡加一行System.Windows.Forms.Application.DoEvents();
建議就放在label1.Text的下一行
我照您說的加在label1.Text的下一行,結果還是一樣呢!
#region INSERT INTO SQL
private void InsertSQL(DataTable dt)
{
using (SqlConnection cn = new SqlConnection())
{
cn.ConnectionString = xxxx;
cn.Open();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
int cnt = i + 1 ;
label1.Text = "已處理第" + cnt + "筆/共" + dt.Rows.Count + "筆資料";
System.Windows.Forms.Application.DoEvents();
try
{
string strSQL = "";
}
catch(Exception e)
{
//今天日期
DateTime Date_log = DateTime.Now;
string TodayMillisecond = Date_log.ToString("yyyy-MM-dd HH:mm:ss");
string Today_log = Date_log.ToString("yyyy-MM-dd");
//把例外狀況寫到目的檔案,若檔案存在則附加在原本內容之後(換行)
File.AppendAllText("D:\\tran_log\\" + Today_log + ".txt", "\r\n" + TodayMillisecond + " : " + e);
this.Close();
}
}
cn.Close();
}
}
#endregion
網路上找到兩派作法
一是DoEvents
二是BackgroundWorker
不妨兩者都試試看
label1.Refresh()
試過這個嗎?
[MSDN: System.Windows.Forms.Control.Refresh](https://msdn.microsoft.com/zh-tw/library/system.windows.forms.control.refresh(v=vs.110).aspx "")