asp.net访问数据库时,如果sql查询语句中的记录不存在,ExecuteReader()就会报错"select附近有错误",该怎么判断要查询的记录是否不存在?

解决方案 »

  1.   


     try
                {
                    int count = 0;
                    String cnnStr = ConfigurationManager.ConnectionStrings["SERVER"].ConnectionString;
                    SqlConnection cnn = new SqlConnection(cnnStr);
                    String cmdStr = @"declare @s varchar(50) 
                                      select @s=DataBaseName from Metadata_DB where ID=(select DB from Metadata_Terrain where Level='" + level + "' and Row='" + row + "' and Col='" + col + "') exec('use '+@s+' select Data from Data where Level=''" + level + "'' and Row=''" + row + "'' and Col=''" + col + "''')";
                    using (SqlCommand cmd = new SqlCommand(cmdStr, cnn))
                    {  
                        cnn.Open();
                        SqlDataReader dataReader = cmd.ExecuteReader();
                        while (dataReader.Read())
                        {
                            Response.BinaryWrite((byte[])dataReader["Data"]);
                count=((byte[])dataReader["Data"]).Length;
                        }
                dataReader.Close();
                        Response.ContentType = "application/zip";
                    }
                    cnn.Close();
                }
                catch (SqlException e){}
                Response.End();
                Response.Close();
      

  2.   

    where ID===>
    where ID IN 
      

  3.   

    select count(1) from db where 
      

  4.   

    select count(*) from db
      

  5.   

    应该是的
    在exec('use '+@s+'前 加上if(@s is not null)
    因为当@s为空时,你没有办法改变数据库的。
      

  6.   

    还是通过参数SqlParameter查询,获取返回的参数值
      

  7.   

    where ID isnull((select DB from ),0)
    对你查询的值进行判断一下,如果不存在的话,SQL会返回NULL,判断一下,如果是NULL的话,给它一个默认值。这样就不会出来这种情况了
      

  8.   

    select top 1  DB from Metadata_Terrain 
      

  9.   

    主要你的where条件有两条以上的时候就会报错哦 也可以加个 TOP 1
      

  10.   

    select * from t5 where name in(  )如果括号内只有一个数,那么in与=是一样的
      

  11.   

    select附近有错误 很明显 SQL语句错误 
      

  12.   

    "select附近有错误", 意思是 sql语句有错误,和数据没关系的啊
      

  13.   

    要取的记录没有存在,才出现"select附近有错误"的,并不是我写的sql字符串有错误
      

  14.   

    首先 纠正 你两个错误
    1.
    in 和= 是不一样的  in 返回多条结果  =只返回一条
    2.
    sql 附近有语法错误,是sql语句写的有问题,和查没查到数据没关系
    把 SQL 语句放到 查询分析器里 执行以下  就知道有没有问题了