.Close()这个关闭链接的方法 应该写在return前面还是后面 这样写总是提示有错误说fildcount什么的 应该怎么写?
mycon = cf.conn();
        if (mycon.State == ConnectionState.Closed)
            mycon.Open();
        myds = new DataSet();
        myda = new SqlDataAdapter();
        mycmd = new SqlCommand(sel_letak, mycon);
        mycmd.CommandType = CommandType.StoredProcedure;
        myda.SelectCommand = mycmd;
        myda.Fill(myds);
        mycon.Close();
mycmd.Dispose();
        return myds;

解决方案 »

  1.   

    我觉得你用using把这SQL操作放在using中。这样一般就不会出现这种关闭的问题
      

  2.   

    使用SqlDataAdapter不需要显示打开连接和关闭,它会自动打开和关闭mycon = cf.conn();
    if(mycon!=null)
    {
            myds = new DataSet();
            myda = new SqlDataAdapter(); 
            mycmd = new SqlCommand(sel_letak, mycon); 
            mycmd.CommandType = CommandType.StoredProcedure; 
            myda.SelectCommand = mycmd; 
            myda.Fill(myds); 
            return myds;
    }//这样试试.close(),关闭连接将连接扔进连接池,等待下次被调用,
    .Dispose(),销毁连接
      

  3.   

    mycon = cf.conn(); 
            if (mycon.State == ConnectionState.Closed) 
                mycon.Open(); 
            myds = new DataSet(); 
            myda = new SqlDataAdapter(); 
            mycmd = new SqlCommand(sel_letak, mycon); 
            mycmd.CommandType = CommandType.StoredProcedure; 
            myda.SelectCommand = mycmd; 
            myda.Fill(myds); mycmd.Dispose();
            mycon.Close(); 
     
            return myds;
      

  4.   

    写在 return 后的语句不会有错误,但会有警告信息:有无法访问的代码,也就是说,tetrun 语句后的语句永远不会执行