string strSql,strConn,num;
        int  maxvalue ;
        maxvalue = 0;
        strConn = WebConfigurationManager.ConnectionStrings[1].ConnectionString;
        strSql = "select max(substring(gcbm,16,2)) as maxvalue  from sggcjbqkb where substring(gcbm,7,8)='"+dd +"'";
        SqlConnection sc = new SqlConnection();
        sc.ConnectionString = strConn;
        sc.Open();        Response.Write(maxvalue);
        num = Convert.ToString(maxvalue + 1);-----------sql语句
strSql = "select max(substring(gcbm,16,2)) as maxvalue  from sggcjbqkb where substring(gcbm,7,8)='"+dd +"'";
中的maxvalue的值在程序运运

解决方案 »

  1.   

    string strSql,strConn,num;
            int  maxvalue ;
            maxvalue = 0;
            strConn = WebConfigurationManager.ConnectionStrings[1].ConnectionString;
            strSql = "select max(substring(gcbm,16,2)) as maxvalue  from sggcjbqkb where substring(gcbm,7,8)='"+dd +"'";
            SqlConnection sc = new SqlConnection();
            sc.ConnectionString = strConn;
            sc.Open();        Response.Write(maxvalue);
            num = Convert.ToString(maxvalue + 1);========================
     strSql = "select max(substring(gcbm,16,2)) as maxvalue  from sggcjbqkb where substring(gcbm,7,8)='"+dd +"'";
    写这个语句是为了得到最大值,我把这个语句在数据库里执xing的时候,可以得到最大值,但是在程序运行的时候,我设断点查看到的值为0,为什么不能正确的返回值?
      

  2.   

    老兄:你并没有把执行结果的值赋给maxvalue,当然结果是0了。
    你需要用SqlDataReader 或者DataSet对象,然后将取得的值赋给maxvalue。如:
    SqlCommand myCmd=new SqlCommand(strSql,strConn);
    SqlDataReader myReader=myCmd.ExecuteReader();
    if(myReader.Read())
    {
      maxvalue=int.Parse(myReader["maxvalue"].ToString());
    }
      

  3.   

    拜托老大你的strSql = "select max(substring(gcbm,16,2)) as maxvalue  from sggcjbqkb where substring(gcbm,7,8)='"+dd +"'";
    根本就没有被类似与sqldataadapter或者sqlcommand来执行!!!
      

  4.   

    楼上的方法就可以执行sql语句得到结果,而你的代码就没有执行sql语句
      

  5.   

    语句没执行,当然取不到值啦
    cmd.ExecuteScalar();返回首行首列