在ODP.NET中选择最近使用ALTER TABLE插入的列时出错

时间:2012-04-17 20:15:09

标签: c# .net oracle odp.net

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

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

3-执行命令以选择此列

4-使用DataReader读取。 Application引发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();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

尝试使用OracleConnection.BeginTransaction创建一个事务,然后在选择之前Commit,就像文档中的example所示。