如何将executereader用于不同的变量?

时间:2016-02-04 07:57:18

标签: c# asp.net executescalar executereader

我使用ExecuteScalar进行单一查询,如下所示:

try
{
    OleDbConnection Connection;
    using (Connection = new OleDbConnection("Provider=MSDAORA.1;Data Source=DATABASE:1521/orcl;Persist Security Info=True;Password=PASSWORD;User ID=USERNAME"))
    {
    OleDbCommand Command1, Command2, Command3;

    Command1 = new OleDbCommand("SELECT HOUR FROM Table1 WHERE type='CAR' and name='FERRARI'",", Connection);
    Command2 = new OleDbCommand("SELECT HOUR FROM Table1 WHERE type='CAR' and name='FORD'", Connection);
    Command3 = new OleDbCommand("SELECT HOUR FROM Table1 WHERE type='CAR' and name='OPEL'", Connection);

    Connection.Open();
    var1 = (int)Command1.ExecuteScalar();
    var2 = (int)Command2.ExecuteScalar();
    var3 = (int)Command3.ExecuteScalar();
    Connection.Close();
} 

它对我来说很好。但是3(以及其他数据库的更多)查询等待因为数据库太多了。如何选择所有表(从表1中选择*)并在每个where条件下执行命令后?你能举例说明我的示例代码吗?谢谢。

1 个答案:

答案 0 :(得分:1)

您可以一次向数据库发送多个命令(以分号分隔)。您可以使用OleDb.NextResult()获取每个结果集。这将导致一个来回数据库,而不是你现在拥有的三个。