奇怪!同一条sql语句在数据库和代码中执行结果不同?!当然是人错了 ……

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012407484/article/details/50433581

    sql语句就是它:


    

select count(1) from syscolumns where [id]=object_id( 'tablecommon' )

 


    判断数据库中是否存在表tablecommon,本想让在数据库中查到的值返回到程序里,值却总不是想要的:


    程序里返回 -1

    sql里返回大于0的值(意为存在该表)

    如果代码正确的话,程序里应该返回和sql里一样的值


    程序里是这么写的:


    D层类:





    SqlHelper类:


        执行不带参数的查询存储过程或sql语句:


        public DataTable ExecuteQuery(string cmdText, CommandType cmdType)
        {
            DataTable dt = new DataTable();
            cmd = new SqlCommand(cmdText, GetConn());
            cmd.CommandType = cmdType;
            using (sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                dt.Load(sdr);
            }
            return dt;
        }



        执行不带参数的增删改存储过程或sql语句:



        public int ExecuteNonQuery(string cmdText, CommandType cmdType)
        {
            int res;
            try
            {
                cmd = new SqlCommand(cmdText, GetConn());
                cmd.CommandType = cmdType;
                res = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (conn.State == ConnectionState.Open)
                {
                    conn.Close();
                }
            }
            return res;
        }



    看到这里有没有发现我犯的错误?



    没错,我调用SqlHelper方法调错了,咱们开始缕:


    我写的是一个select语句,是用来查询的,虽然返回结果可以用来直观判断表的存在与否,但不能直接调用返回值是int类型的增删改方法!

    仍需调用返回值是datatable类型的查询方法,之后将datatable中的值强制转换为int类型



    所以,接下来,将D层类修改下,就搞定了!

    

    

        public bool IsExitsTable()
        {
            Boolean flag = false;
            string sql = "IfExistsTable";
            DataTable dt=sqlhelper.ExecuteQuery(sql,CommandType.StoredProcedure);

            int res = Convert.ToInt32(dt.Rows[0][0]);
            if (res > 0)
            {
                flag = true;
                return flag;
            }
            else
            {
                return flag;
            }            
        }



展开阅读全文

一条SQL执行结果不同,求助!

05-11

用Mybatis框架执行的结果和自己执行的结果不一致。rnrn下面是Mybatis的配置rnrnrn (select *rn from (select * from bbs.bbsnote order by responsedate desc)rn where rownum <= #maxnum)rn minusrn (select *rn from (select * from bbs.bbsnote order by responsedate desc)rn where rownum <= #minnum)rnrnrn执行结果:rnDEBUG [http-bio-8888-exec-1] - Registering transaction synchronization for SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@4e69f6b0]rnDEBUG [http-bio-8888-exec-1] - JDBC Connection [oracle.jdbc.driver.T4CConnection@19de40b4] will be managed by SpringrnDEBUG [http-bio-8888-exec-1] - ==> Preparing: (select * from (select * from bbs.bbsnote order by responsedate desc) where rownum <= ?) minus (select * from (select * from bbs.bbsnote order by responsedate desc) where rownum <= ?) rnDEBUG [http-bio-8888-exec-1] - ==> Parameters: 15(Integer), 10(Integer)rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 51(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 22rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 71(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 1rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 91(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 2rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 223(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 25rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 303(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 1rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 338(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 5rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 351(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 12rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 353(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 0rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 354(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 1rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 355(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 2rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 484(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 0rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 522(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 0rnDEBUG [http-bio-8888-exec-1] - ====> Preparing: select * from BBS.BBSNOTESOFA where NOTEID=? rnDEBUG [http-bio-8888-exec-1] - ====> Parameters: 540(Long)rnDEBUG [http-bio-8888-exec-1] - <==== Total: 0rn[color=#FF6600]DEBUG [http-bio-8888-exec-1] - <== Total: 13[/color]rnrn上面得到的结果不是我想要的,居然有13条数据,搞不懂这时为什么,但是我自己来执行sql,结果却是正确的,我真的找不出问题点了:rnrnselect count(*) from ((select *rnfrom (select * from bbs.bbsnote order by responsedate desc)rnwhere rownum <=15)rnminusrn(select *rnfrom (select * from bbs.bbsnote order by responsedate desc)rnwhere rownum <=10));rnrn结果:5rnrnrnrnrn 论坛

没有更多推荐了,返回首页