protected SqlDataReader drSelectAllWhereID(string strtSp,int iID,string strID)
{
conn = new SqlConnection(con);
cmd = new SqlCommand(strSp,conn);
cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.Add(strID,SqlDbType.Int).Value = iID; conn.Open();
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);//这句出错。前面已定义dr作用域,protected SqlDataReader dr;
return dr;
}

解决方案 »

  1.   

    cmd = new SqlCommand(strSp,conn);
    cmd.CommandText = "你的储存过程名"; //增加这句
    cmd.CommandType = CommandType.StoredProcedure;
      

  2.   

    我用的是vb
    dim conn  as new SqlConnection(con)
    dim cmd as new sqlcommand
    dim strID as new Parameters("@strID",SqlDbType.Int)  '@strID 为你的存储过程中的参数名
    strID.value=iID
    with cmd
      .CommandType = CommandType.StoredProcedure 
      .connection=conn
      .CommandText = strSp '储存过程名
      .Parameters.Add(strID)
    end with
    Try
       If conn.State = ConnectionState.Closed Then
              conn.Open()
       End If
      dr = cmd.ExecuteReader
       '你有做的事情
    Catch ex As Exception
         MessageBox.Show(ex.Message)
    Finally
      conn.Close()End Try