>我使用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条件执行命令后?你能在我的示例代码上给我看这个例子吗?谢谢。
您可以一次向数据库发送多个命令(用分号分隔)。您可以使用 OleDb.NextResult() 获取每个结果集。这将导致一个来回到数据库,而不是你现在的三个。