在c#中执行Oracle查询
本文关键字:Oracle 查询 执行 | 更新日期: 2023-09-27 18:12:03
我理解线程的概念,但一般来说我是新手,所以请原谅我。对不起,如果这是一个愚蠢的问题。
我需要在我的Windows窗体程序运行对Oracle数据库的一些密集查询。虽然这段代码确实可以工作,但程序冻结了,因为查询可能需要一分钟才能运行,而我正在按顺序运行几个查询。我认为创建一个单独的线程,如下所示,可以防止这种情况。我是否需要在线程的创建中指定一些东西,表明只有一定数量的资源应该专用于它?我在谷歌上找到了一些答案,但并没有真正理解它们。
提前感谢您的帮助!
private void loginButton_Click(object sender, EventArgs e)
{
loginStatus.Text = "STATUS: Running...";
Thread thread1 = new Thread(new ThreadStart(GetLoginData));
thread1.Start();
loginStatus.Text = "STATUS: Ready";
}
private void GetLoginData()
{
try
{
Invoke(new Action(() => loginButton.Enabled = false));
string LDS01_start = "select count(*) from BBLEARN.AUTH_PROVIDER_LOG where AUTH_PROVIDER_PK1 = 103 ";
string LDAPS_start = "select count(*) from BBLEARN.AUTH_PROVIDER_LOG where AUTH_PROVIDER_PK1 = 106 ";
string middle = "and log_date >= '" + GetDate(loginStartDate) + @"'
and log_date < '" + GetDate(loginEndDate) + @"' ";
string LDS01_0 = LDS01_start + middle + "and event_type = 0";
string LDS01_1 = LDS01_start + middle + "and event_type = 1";
string LDS01_2 = LDS01_start + middle + "and event_type = 2";
string LDS01_5 = LDS01_start + middle + "and event_type = 5";
string LDS01_6 = LDS01_start + middle + "and event_type = 6";
string LDAPS_0 = LDAPS_start + middle + "and event_type = 0";
string LDAPS_1 = LDAPS_start + middle + "and event_type = 1";
string LDAPS_2 = LDAPS_start + middle + "and event_type = 2";
string LDAPS_5 = LDAPS_start + middle + "and event_type = 5";
string LDAPS_6 = LDAPS_start + middle + "and event_type = 6";
Invoke(new Action(() => GetData(LDS01_0, LDS01_LB0)));
Invoke(new Action(() => GetData(LDS01_1, LDS01_LB1)));
Invoke(new Action(() => GetData(LDS01_2, LDS01_LB2)));
Invoke(new Action(() => GetData(LDS01_5, LDS01_LB5)));
Invoke(new Action(() => GetData(LDS01_6, LDS01_LB6)));
Invoke(new Action(() => GetData(LDAPS_0, LDAPS_LB0)));
Invoke(new Action(() => GetData(LDAPS_1, LDAPS_LB1)));
Invoke(new Action(() => GetData(LDAPS_2, LDAPS_LB2)));
Invoke(new Action(() => GetData(LDAPS_5, LDAPS_LB5)));
Invoke(new Action(() => GetData(LDAPS_6, LDAPS_LB6)));
Invoke(new Action(() => loginButton.Enabled = true));
loginStatus.Text = "STATUS: Ready";
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
private void GetData(string selectCommand, Label label)
{
//open the connection
OracleConnection conn = new OracleConnection(connectString);
conn.Open();
//define the command
selectCommand = selectCommand.Replace(Environment.NewLine, " ");
OracleDataAdapter dataAdapter = new OracleDataAdapter(selectCommand, conn);
OracleCommandBuilder commandBuilder = new OracleCommandBuilder(dataAdapter);
//run the command
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
//pull the result
label.Text = table.Rows[0][0].ToString();
//close the connection
conn.Close();
}
你是invoking
的GetData()
方法,调用使用UI线程,GetData()方法是反对DB,它需要时间,所以你的屏幕锁定。你只需要调用UI更改,然后Thread off DB调用,在完成后调用UI线程来启用按钮等…
更改GetLoginData()
中的呼叫:
Invoke(new Action(() => GetData(LDS01_0, LDS01_LB0)));
:
GetData(LDS01_0, LDS01_LB0);
将GetData()改为:
private void GetData(string selectCommand, Label label)
{
//open the connection
OracleConnection conn = new OracleConnection(connectString);
conn.Open();
//define the command
selectCommand = selectCommand.Replace(Environment.NewLine, " ");
OracleDataAdapter dataAdapter = new OracleDataAdapter(selectCommand, conn);
OracleCommandBuilder commandBuilder = new OracleCommandBuilder(dataAdapter);
//run the command
DataTable table = new DataTable();
table.Locale = System.Globalization.CultureInfo.InvariantCulture;
dataAdapter.Fill(table);
//close the connection
conn.Close();
Invoke(new Action(() => RenderData(label, table.Rows[0][0].ToString())));
}
创建新的RenderData()方法:
private void RenderData(Label label, string text)
{
label.Text = text;
}