我想把数据库中的参数拿到asp.net 中用
如:我把string strsql定义为如下:
declare @number int 
set @number = (select count(h) from test )
这个SQL语句,,
我想把@number拿到函数中用
应该怎么办,,
是不是根SqlParameter有关的,,
应该怎么用啊,,

解决方案 »

  1.   

    一个  out 类型的参数 
    执行sql 语句结束以后 
    取得该参数的值 。
      

  2.   

    http://community.csdn.net/Expert/topic/3589/3589084.xml?temp=.429043
      

  3.   

    http://www.vchelp.net/vchelp/file_cs/adonet_4com.asp?type_id=96&class_id=2&cata_id=21&article_id=1134&search_term=
      

  4.   

    如果不想用存储过程的话
    string strsql = "select count(h) as count from test ";
    SqlCommand cmd = new SqlCommand(strsql,conn);
    SqlDataReader dr= cmd..ExecuteReader()
     if(dr.Read())
    {
       aa=dr["count"].ToString();
    }
    如果不中的话,给count加单引号看看,没用过
      

  5.   

    重要的忘了, conn.Open();
    最后,
    dr.Dispose();  
    conn.Close();
      

  6.   

    这样写好些:using (conn = new SqlConnection("xxx"))
    {
      ....
      conn.Close();
    }
      

  7.   

    不好意思,好像是错的
    "select 'count'=count(h) from test"
    我也应该好好看看书了,想法就是生成一列,然后将列值多出来
      

  8.   

    SqlConnection Con = new SqlConnection("server=localhost;uid=sa;pwd=sa;database=xx");
    string strSQL="declare @number int \n"
      +"select @number = (select count(h) from test )";
    SqlCommand cmd = new SqlCommand(Con,strSQL);
    Con.Open();
    int Number=(int)cmd.ExecuteScalar()
    Con.Close();
      

  9.   

    Thank You Everyone!!!!!!!!!!!
      

  10.   

    我再区测试了一下,,
    发现我的定义变量是多此一举的,,
    还弄成什么没实例化,,
    现在只需这样就可以的,,当然先要谢谢各位了
    "
    string strSql = "select count(h) from test";
    string strConnection = "server=localhost;database=mydatabase;uid=sa;password=sa";
    SqlConnection objConnection = new SqlConnection(strConnection);
    SqlCommand objCommand = new SqlCommand(strSql,objConnection);
    objConnection.Open();
    int number = (int) objCommand.ExecuteScalar();
    //SqlCommand.ExecuteScalar()返回执行查询结果中的第一行的第一列
    Response.Write("sdfds"+number+"<br>");
    "
    但是我不知道为什么把strSql换成
    “declare @number int select @number=(seleclt count(h) from test)”
    为什么不可以,,
    照例他也应该返回的是
    一行一列的,,
    有什么不同啊