并非所有数据都通过Oracle.ManagedDataAccess可见

时间:2015-04-20 10:57:16

标签: .net oracle oracle10g oracle-sqldeveloper

问题摘要

通过Oracle.ManagedDataAccess使用Oracle数据库时遇到了奇怪的行为:应用程序的数据访问组件只看到从此应用程序添加的表数据。

重现的步骤

Oracle表:

CREATE TABLE TEST_TABLE (
    "TIMESTAMP" DATE DEFAULT SYSDATE NOT NULL
)

测试应用:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Before run, count = {0}", GetCount());
        InsertNew();
        Console.WriteLine("After app insert, count = {0}", GetCount());

        Console.WriteLine("Insert row via SQL developer and press enter");
        Console.Read();

        Console.WriteLine("After SQL developer insert, count = {0}", GetCount());

       Console.Read();
    }

    private static void InsertNew()
    {
        using (var connection = OpenConnection())
        {
            var command = new OracleCommand("INSERT INTO TEST_TABLE(TIMESTAMP) VALUES(:Timestamp)", connection);
            command.Parameters.Add(new OracleParameter("Timestamp", OracleDbType.Date)).Value = DateTime.Now;
            command.ExecuteNonQuery();
        }
    }



    private static int GetCount()
    {
        using (var connection = OpenConnection())
        {
            var command = new OracleCommand("SELECT COUNT(1) FROM TEST_TABLE", connection);
            return (int)(decimal)command.ExecuteScalar();
        }
    }

    private static OracleConnection OpenConnection()
    {
        var connectionString = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
        var connection = new OracleConnection(connectionString);
        connection.Open();
        return connection;
    }
}

当应用程序提示通过SQL开发人员插入行时,我运行

INSERT INTO TEST_TABLE(TIMESTAMP) VALUES(SYSDATE);

连接字符串如下所示:

Data Source=DATA_SOURCE_NAME;User Id=USER_ID;Password=PASS;

Oracle版本:10g(10.2.0.5.0) Oracle.ManagedDataAccess版本:4.121.2.0

结果

首次运行时,我得到结果:

Before run, count = 0
After app insert, count = 1
Insert row via SQL developer and press enter

After SQL developer insert, count = 1

第二轮我得到结果:

Before run, count = 1
After app insert, count = 2
Insert row via SQL developer and press enter

After SQL developer insert, count = 2

因此应用程序会看到它添加的数据,包括先前运行时添加的数据,但看不到通过SQL开发人员添加的数据

当通过SQL开发人员运行查询时,将返回所有数据

问题

所以问题是什么可能导致这种行为?以及如何确保应用程序可以获取所有数据?

1 个答案:

答案 0 :(得分:0)

我找到了答案 - 重现的步骤缺少步骤"在SQL中按[Commit]"执行插入后。