- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
ODP.NET OracleCommand 类有一个 CommandTimeout 属性,可用于强制执行命令超时。此属性似乎适用于 CommandText 是 SQL 语句的情况。示例代码用于说明此属性的实际操作。在代码的初始版本中,CommandTimeout 设置为零 - 告诉 ODP.NET 不要强制超时。
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
using (OracleConnection con = new OracleConnection("User ID=xxxx; Password=xxxx; Data Source=xxxx;"))
using (OracleCommand cmd = new OracleCommand())
{
con.Open();
cmd.Connection = con;
Console.WriteLine("Executing Query...");
try
{
cmd.CommandTimeout = 0;
// Data set SQL:
cmd.CommandText = "<some long running SQL statement>";
cmd.CommandType = System.Data.CommandType.Text;
Stopwatch watch1 = Stopwatch.StartNew();
OracleDataReader reader = cmd.ExecuteReader();
watch1.Stop();
Console.WriteLine("Query complete. Execution time: {0} ms", watch1.ElapsedMilliseconds);
int counter = 0;
Stopwatch watch2 = Stopwatch.StartNew();
if (reader.Read()) counter++;
watch2.Stop();
Console.WriteLine("First record read: {0} ms", watch2.ElapsedMilliseconds);
Stopwatch watch3 = Stopwatch.StartNew();
while (reader.Read())
{
counter++;
}
watch3.Stop();
Console.WriteLine("Records 2..n read: {0} ms", watch3.ElapsedMilliseconds);
Console.WriteLine("Records read: {0}", counter);
}
catch (OracleException ex)
{
Console.WriteLine("Exception was thrown: {0}", ex.Message);
}
Console.WriteLine("Press any key to continue...");
Console.Read();
}
}
}
}
Executing Query...
Query complete. Execution time: 8372 ms
First record read: 3 ms
Records 2..n read: 1222 ms
Records read: 20564
Press any key to continue...
cmd.CommandTimeout = 3;
Executing Query...
Exception was thrown: ORA-01013: user requested cancel of current operation
Press any key to continue...
PROCEDURE PROC_A(i_sql VARCHAR2, o_cur1 OUT SYS_REFCURSOR)
is
begin
open o_cur1
for
i_sql;
END PROC_A;
using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Text;
using Oracle.DataAccess.Client;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
using (OracleConnection con = new OracleConnection("User ID=xxxx; Password=xxxx; Data Source=xxxx;"))
using (OracleCommand cmd = new OracleCommand())
{
con.Open();
cmd.Connection = con;
Console.WriteLine("Executing Query...");
try
{
cmd.CommandTimeout = 3;
string sql = "<some long running sql>";
cmd.CommandText = "PROC_A";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("i_sql", OracleDbType.Varchar2) { Direction = ParameterDirection.Input, Value = sql });
cmd.Parameters.Add(new OracleParameter("o_cur1", OracleDbType.RefCursor) { Direction = ParameterDirection.Output });
Stopwatch watch1 = Stopwatch.StartNew();
OracleDataReader reader = cmd.ExecuteReader();
watch1.Stop();
Console.WriteLine("Query complete. Execution time: {0} ms", watch1.ElapsedMilliseconds);
int counter = 0;
Stopwatch watch2 = Stopwatch.StartNew();
if (reader.Read()) counter++;
watch2.Stop();
Console.WriteLine("First record read: {0} ms", watch2.ElapsedMilliseconds);
Stopwatch watch3 = Stopwatch.StartNew();
while (reader.Read())
{
counter++;
}
watch3.Stop();
Console.WriteLine("Records 2..n read: {0} ms", watch3.ElapsedMilliseconds);
Console.WriteLine("Records read: {0}", counter);
}
catch (OracleException ex)
{
Console.WriteLine("Exception was thrown: {0}", ex.Message);
}
Console.WriteLine("Press any key to continue...");
Console.Read();
}
}
}
}
Executing Query...
Query complete. Execution time: 34 ms
First record read: 8521 ms
Records 2..n read: 1014 ms
Records read: 20564
Press any key to continue...
最佳答案
这是我最终采用的解决方案。它只是 OracleDataReader 类的扩展方法。这个方法有一个超时值和一个回调函数作为参数。回调函数通常(如果不总是)是 OracleCommand.Cancel。
namespace ConsoleApplication1
{
public static class OracleDataReaderExtensions
{
public static bool Read(this OracleDataReader reader, int timeout, Action cancellationAction)
{
Task<bool> task = Task<bool>.Factory.StartNew(() =>
{
try
{
return reader.Read();
}
catch (OracleException ex)
{
// When cancellationAction is called below, it will trigger
// an ORA-01013 error in the Read call that is still executing.
// This exception can be ignored as we're handling the situation
// by throwing a TimeoutException.
if (ex.Number == 1013)
{
return false;
}
else
{
throw;
}
}
});
try
{
if (!task.Wait(timeout))
{
// call the cancellation callback function (i.e. OracleCommand.Cancel())
cancellationAction();
// throw an exception to notify calling code that a timeout has occurred
throw new TimeoutException("The OracleDataReader.Read operation has timed-out.");
}
return task.Result;
}
catch (AggregateException ae)
{
throw ae.Flatten();
}
}
}
}
namespace ConsoleApplication1
{
class Program
{
static string constring = "User ID=xxxx; Password=xxxx; Data Source=xxxx;";
static void Main(string[] args)
{
using (OracleConnection con = new OracleConnection(constring))
using (OracleCommand cmd = new OracleCommand())
{
cmd.Connection = con;
con.Open();
Console.WriteLine("Executing Query...");
string sql = "<some long running sql>";
cmd.CommandText = "PROC_A";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("i_sql", OracleDbType.Varchar2) { Direction = ParameterDirection.Input, Value = sql });
cmd.Parameters.Add(new OracleParameter("o_cur1", OracleDbType.RefCursor) { Direction = ParameterDirection.Output });
try
{
// execute command and get reader for ref cursor
OracleDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
// read first record; this is where the ref cursor SQL gets evaluated
Console.WriteLine("Reading first record...");
if (reader.Read(3000, cmd.Cancel)) { }
// read remaining records
Console.WriteLine("Reading records 2 to N...");
while (reader.Read(3000, cmd.Cancel)) { }
}
catch (TimeoutException ex)
{
Console.WriteLine("Exception: {0}", ex.Message);
}
Console.WriteLine("Press any key to continue...");
Console.Read();
}
}
}
}
Executing Query...
Reading first record...
Exception: The OracleDataReader.Read operation has timed-out.
Press any key to continue...
关于.net - OracleDataReader.Read 方法超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16614035/
与 MS SQL Server 相比,我在从 OracleDataReader 对象读取数据时遇到了一些糟糕的性能。它几乎慢了 10 倍,这是 Not Acceptable 。 下面是两个测试都使用的
代码卡在 OracleDataReader reader = inst.Command.ExecuteReader(); 出现错误 ORA-00911:无效字符(查询中)。我已经分别检查了查询
我正在使用 Oracle.ManagedDataAccess 从我的数据库返回数据,我确实需要对结果进行分页,因为该表中有很多寄存器。 所以我使用了 this 中的第二个答案发布到分页,当我在 Ora
我正在尝试遍历游标。当我直接在 pl/sql 中执行命令(表示为 sql - 见下文)时,我得到了包含更多行的结果集。但是当我运行这段代码时,我只得到一行: using (OracleConnecti
ODP.NET OracleCommand 类有一个 CommandTimeout 属性,可用于强制执行命令超时。此属性似乎适用于 CommandText 是 SQL 语句的情况。示例代码用于说明此属
谁能告诉我如何在 .net 2.0 中从 OracleDataReader 查找行数? 最佳答案 An OracleDataReaderobject represents a forward-only
我试图从 oracal 数据库中获取数据,其中表的列之一是 NUMBER(4,0)。我如何从 C# 中的数据库中获取此数据类型... 这是我试图编译的示例代码 ArrayList ListofSal
我的问题,类似于this one ,如何使用 OracleDataReader 检索给定记录的所有字段?目前,我一直在使用这种方法,它一次只返回一列值: public string Select_Fi
我已经创建了一个软件来从 Oracle DB 读取数据并通过 SMS 发送它,但是当我按下键盘上的 C 键时我想停止 datareader 读取,但是当我这样做时我收到了这个异常消息: Operati
我有以下代码,我想遍历此查询结果中的所有字段并填充名为字段的字典。 给定数据读取器这可能吗? OracleCommand command = connection.Create
当在 c# 程序中使用 OracleDataReader (Oracle.DataAccess.Client) 从记录集中读取数据时,是否存在其中一个字段中的数据类型可以逐行更改(DBNull 除外)
我正在开发我的第一个 ASP.Net 应用程序,似乎遇到了很多障碍(背景主要是 WinForms,最近有一个 MVC5 项目)。 我正在使用 OracleCommand 成功建立我的数据库连接并执行我
我在使用 OracleDataReader(Oracle.DataAccess.Client) 从 Oracle 存储过程输出数据时遇到问题。 程序: procedure LOAD_BL_RE
当我尝试分配读取器时,C# 抛出异常: 无效操作。连接关闭 我尝试从返回单个单元格的查询中获取结果,该单元格中包含平均值。cmd 是一个 oraclecomand,我用它来向表中插入一行,到目前为止一
我有这样的代码, string ConnectionString = ConfigurationManager.ConnectionStrings["ora"].ConnectionString; O
我是一名优秀的程序员,十分优秀!