Oracle开发中,关于查询的一个问题

今天运行单元测试的时候,有一段突然出现问题,测试代码大致如下:

 1    [Test]
 2        public void TestSelectSql()
 3        {
 4            try
 5            {
 6                //创建临时表
 7                DBFactorySingleton.GetInstance().ExecuteSql("create table testSql (id int,name varchar(20)) ");
 8            }

 9            catch(Exception)
10            {
11                //删除临时数据
12                DBFactorySingleton.GetInstance().ExecuteSql("drop table testSql ");
13                //创建临时表
14                DBFactorySingleton.GetInstance().ExecuteSql("create table testSql (id int,name varchar(20)) ");
15
16            }

17            
18            //插入数据
19            DBFactorySingleton.GetInstance().ExecuteSql("insert into testSql values(1,'10')");
20            DBFactorySingleton.GetInstance().ExecuteSql("insert into testSql values(2,'20')");
21
22            string selectSql="select * from testSql";
23            //查询数据
24            IDataReader read= DBFactorySingleton.GetInstance().SelectSql(selectSql);
25            Assert.IsNotNull(read);
26
27            read.Read();
28            Assert.AreEqual( read[0].ToString(),"1");
29            Assert.AreEqual( read[1].ToString(),"10");
30
31            read.Read();
32            Assert.AreEqual( read[0].ToString(),"2");
33            Assert.AreEqual( read[1].ToString(),"20");
34
35            Assert.IsFalse( read.Read());   //只有两条记录
36            read.Close();
37    //添加一列 
38            DBFactorySingleton.GetInstance().ExecuteSql("alter table testSql add testcolumn int");
39
40            
41            //查询数据
42            read= DBFactorySingleton.GetInstance().SelectSql(selectSql);
43            Assert.IsNotNull(read);
44
45            
46            read.Read();
47            Assert.AreEqual(3,read.FieldCount) ;
48            
49            read.Close();
50//删除临时数据
51            DBFactorySingleton.GetInstance().ExecuteSql("drop table testSql ");
52        }

问题出现在第47行,即先执行某查询,然后添加字段,再次查询出来的结果中 字段数量不变!

这个unit在Sqlserver中测试是没有问题的,为什么在Oracle中就不行了呢?

开始的时候,以为Oracel的Connection缓存的原因,于是在ODP.net的连接字符串中加入Pooling=false,不使用连接池,但测试还是不通过。
即然与连接池无关,那就可能是查询本身的问题,突然想到原来在做Oracle优化时候的一个原则:尽量执行同样的SQL语句。 是否与这有关呢?
在第40行加入:
selectSql="select * from testSql ";  //后面加一空格
再次运行,测试通过!

总结:在Oracle中,同样的Sql语句在查询的时候性能会更高,但如果修改了表结构,查询结果中的表格保持不变。

posted on 2005-05-18 00:35  wljcan  阅读(2354)  评论(3编辑  收藏  举报

导航