md=myread1(mystring);//請問是不是不能這樣做的呀

解决方案 »

  1.   

    public void SqlDataReader myread1(string mystr1)
    {
    SqlConnection SqlConn1=new SqlConnection(ConnString);
    SqlDataReader thisread=null;
    try
    {
    SqlConn1.Open ();
    SqlCommand mycomand=new SqlCommand (mystr1,SqlConn1);
    thisread=mycomand.ExecuteReader (CommandBehavior.CloseConnection);
    return thisread;
    }
    catch
    {
        throw new ExecutionEngineException ();
    }
    }
    ________________________________________________________________________
    string mystring="select * from xxx";
    SqlDataReader md;
    md=myread1(mystring);
    while(md.Read ())
    {
    Response.Write (md["字段名1"]);
    }
      

  2.   

    基本上找到原因﹐但不明白。請看
    private void Page_Load(object sender, System.EventArgs e)
    {
    SqlConn=new SqlConnection (ConnString);
    //SqlConn變量我在函數外面單獨申明了private SqlCommand mycom;
    try
    {
    SqlConn.Open ();
    SqlCommand Cmd=new SqlCommand ("select * from groups",SqlConn);
    myread=Cmd.ExecuteReader ();
    while(myread.Read ())
      {
        Response.Write (myread["groupname"]);
      }
    }
    catch{throw;}}
    _________________________________________________________________________________
    下面是一樣的呀﹐只是SqlConn變量在函數量申明。請看。。
    private void Page_Load(object sender, System.EventArgs e)
    {
    /*SqlConnection SqlConn1=new SqlConnection(ConnString);
    //唯一就是SqlConnn1變量地方不同呀。
    try
      {
       SqlConn1.Open ();
        SqlCommand mycomand=new SqlCommand ("select * from groups",SqlConn1);
        SqlDataReader thisread;
        thisread=mycomand.ExecuteReader();
        while(thisread.Read ())
    {
    Response.Write (myread["groupname"]);
    }

    }
    catch{throw ;}}*/
      

  3.   

    //SqlConn變量我在函數外面單獨申明了private SqlCommand mycom;
    這句打錯了。
    //SqlConn變量我在函數外面單獨申明了private SqlConnection SqlConn;
      

  4.   

    哪位帮忙看一下是什幺原因﹐看过很多书﹐总觉得撑握的差不多了﹐没想到一动手﹐最基础的东东都过不去。唉。
    看看吧﹐我根据书上看的知认实在不知为什幺﹐第一种情况运行一点问题都没有﹐为什幺第二种写法就行不通。就是一个SqlConnection,及SqlCommand变量申明位置不同呀﹐一个是在Page_Load函数外(这样就行)﹐一个是在Page_Load函数内(这样不行)﹐不知怎幺回事呀。。