我们可以编写多个select语句来从不同的表中提取数据 [英] Can we write multiple select statements to pull data from different tables
问题描述
我有多个表,没有一个是彼此相关的。
我想从数据库中选择数据并显示在表单加载第一个
时的表单。使用ms访问作为后端
有不同的参数,文本,列表框,工具提示,语言选择,在表单加载时从数据库中填充。
我正在使用后台线程,因此没有GUI问题。
但是将select语句写入一个函数是否正确下面。
I have multiple tables none of them is related to each other.
I want to select data from database and display it on the form when form loads for the first
time. using ms access as backend
There are different parameters, text , listboxes ,tooltips, Language selection, which are filled from database when form loads.
I am using background thread for this so no GUI issues.
But Is it right to write the select statement in one function as below.
void LoadDatafromDatabase()
{
try{
OleDbConnection AccessConnection = new OleDbConnection(@"Provider = Microsoft.JET.OLEDB.4.0;data source =" + Application.StartupPath + "\\" + "FileDatabase.mdb;Jet OLEDB:Database password =;);
AccessConnection.Open();
//select query 1
OledbCommand AccessCommand = new OleDbCommand("select Value from Parameters ",AccessConnection);
DataReader = AccessCommand.ExecuteReader();
int j=0;
while (DataReader.Read())
{
FormValue = DataReader.GetValue(1).ToString();
LoadFormValues[j] = FormValue;
j++;
}
//select query 2
OledbCommand AccessCommand = new OleDbCommand("select Value from Tooltip",AccessConnection);
DataReader = AccessCommand.ExecuteReader();
int j=0;
while (DataReader.Read())
{
FormValue = DataReader.GetValue(1).ToString();
FormTooltip[j] = FormValue;
j++;
}
////select query 3...n
......... like this I have used different select queries for different
tables.
}
catch (Exception ex)
{
//catch exp
}
finally
{
//dispose open connenction ,datareader
}
}
是吗一个好的编码实践
Is it a good coding practice
推荐答案
虽然可以从1个查询中获取多个数据表到数据集中,但是我测试它的唯一一次是非常慢,它是几年前我从未重复过这个实验。
我试图引入一些非常小的表来支持应用程序中的查找功能。
While it is possible to get back multiple datatables from 1 query into a dataset, the only time I tested this it was dismally slow, it was some years ago and I have never repeated the experiment.
I was attempting to pull in a number (6) of very small tables to support the lookup function in an application.
你的方法应如下所示
步骤1:将所有sql查询合并为一个。例如
Your approach should look like following
Step 1: merge all sql queries as one. For example
sql query = "select Value from Parameters;select Value from Tooltip;";
步骤2:执行查询并读取多个结果集
Step 2: Execute the query and read multiple resultset
using (OledbCommand command = new OledbCommand
("select Value from Parameters;select Value from Tooltip;", AccessConnection ))
{
using (DataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
MessageBox.Show(reader.GetString(0), "Table1.Column1");
}
if(reader.NextResult())
{
while (reader.Read())
{
MessageBox.Show(reader.GetString(0), "Table2.Column2");
}
}
}
}
这篇关于我们可以编写多个select语句来从不同的表中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!