1

1- 使用 Oracle.DataAccess 连接到 Oracle XE

2-执行命令向表中添加列:alter table TABLE add COLUMN b int;

3-执行命令以选择此列

4- 使用 DataReader 读取。应用程序引发 IndexOutOfRangeException:无法在结果集中找到指定的列

5-重新启动应用程序,查询正常运行

为什么 DataReader 无法访问我刚才创建的列?

这是一个大而简单的测试代码:

private void button1_Click(object sender, EventArgs e)
{
    using (OracleConnection con = new OracleConnection(Settings.Default.CS))
    {
        con.Open();
        try
        {
            using (OracleCommand com = new OracleCommand())
            {
                com.Connection = con;

                // Create a test table
                com.CommandText = "CREATE TABLE Test (a int)";
                com.ExecuteNonQuery();

                // Add one column
                com.CommandText = "ALTER TABLE Test ADD b int";
                com.ExecuteNonQuery();

                com.CommandText = "SELECT * FROM Test";
                using (DbDataReader dr = com.ExecuteReader())
                {
                    MessageBox.Show(dr.FieldCount.ToString());
                    // Here is showing "2", thats ok
                }
            }
        }
        finally
        {
            con.Close();
        }
    }
}

private void button2_Click(object sender, EventArgs e)
{
    using (OracleConnection con = new OracleConnection(Settings.Default.CS))
    {
        con.Open();
        try
        {
            using (OracleCommand com = new OracleCommand())
            {
                OracleTransaction trans = con.BeginTransaction();
                try
                {
                    // Add a column to table already created
                    com.Connection = con;
                    com.CommandText = "ALTER TABLE Test ADD c int";
                    com.ExecuteNonQuery();

                    // Insert a value, ok
                    com.CommandText = "INSERT INTO TEST (a, b, c) VALUES (1, 2, 3)";
                    com.ExecuteNonQuery();

                    trans.Commit();
                }
                catch
                {
                    trans.Rollback();
                    throw;
                }

                // Selecting only "c" column
                com.CommandText = "SELECT c FROM Test";
                using (DbDataReader dr = com.ExecuteReader())
                {
                    if (dr.Read())
                        MessageBox.Show(Convert.ToInt32(dr["c"]).ToString());
                        // Showing correct value, ok
                }

                // Uncomment these lines to solve problem
                //con.Close();
                //OracleConnection.ClearAllPools();
                //con.Open();

                // Selecting all fields * from table
                com.CommandText = "SELECT * FROM Test";
                using (DbDataReader dr = com.ExecuteReader())
                {
                    MessageBox.Show(dr.GetSchemaTable().Rows.Count.ToString() + " / " + dr.FieldCount.ToString());
                    // HERE IS THE PROBLEM: message are showing 2/2, but table haves 3 fields

                    if (dr.Read())
                        MessageBox.Show(Convert.ToInt32(dr["c"]).ToString());
                    // Here throws IndexOutOfRangeException: Unable to find specified column in result set
                }
            }
        }
        finally
        {
            con.Close();
        }
    }
}
4

2 回答 2

1

当您执行阅读器时,听起来插入事务尚未提交。我认为您可以通过在插入后关闭连接并在执行阅读器之前再次打开连接来验证这一点(您可能需要使用 OracleConnection.ClearAllPools)

于 2012-04-17T20:38:15.963 回答
0

尝试使用OracleConnection.BeginTransaction创建交易,然后Commit在选择之前,如文档中的示例所示。

于 2012-04-18T12:54:28.477 回答