当前位置:编程学习 > C#/ASP.NET >>

ASP.NET网站数据库查询

--------------------编程问答-------------------- reader 返回的是一个集合列表,而你只读取了一次
你应该循环读取
while(reader.Read())
{
    //循环读取该用户选择的书ID
    //然后根据ID得到该书的详细信息
}

其实你可以两个表做联合查询,然后读取该列表就可以了,包含订单和书的所有信息

select * from Order a inner join Book b on a.BookID=b.id --------------------编程问答-------------------- if (reader.Read())
改成while --------------------编程问答-------------------- OrderForm order = null;
        if (reader.Read())   //这里要用While读取,reader.Read()每次 读取一条记录
        {
            order = new OrderForm();
            order.BookID = (int)reader["BookID"];
        }
        IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );//order 是null的话,会出错,即使判断了,这里也只是最有一个order的book显示了。如果每个order只有一本书,放到前面的{}里面,否则的话可以用主从表,即先只显示Order,用户选择一个Order是,在显示Books
        this.DataList1.DataSource = book;
        this.DataList1.DataBind(); --------------------编程问答-------------------- 大神们说的是对的,+1 --------------------编程问答-------------------- if (reader.Read())这样是不行的

要用while 并且里面用List<T> .add 存起来 --------------------编程问答--------------------
引用 楼主 ShenQiu____ 的回复:
假如说UserID对应的没有BookID,VS会报错。

假如说UserID对应的没有BookID,VS会报错,是因为 order.BookID = (int)reader["BookID"];你BookID为空的话,转换为整型肯定会报错的! --------------------编程问答-------------------- 错误说明楼上的几位说的都差不多,如果有疑问就接着问,闹明白儿的
上代码:
这是子查询方法,只访问一次数据库就可以了
        Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        换成 string sql = "select BookID from OrderForm where userID = '" + u.UserID + "'";
        //SqlDataReader reader = DBHelper.GetReader(sql);
        //OrderForm order = null;
        //if (reader.Read())
        //{
            //order = new OrderForm();
            //order.BookID = (int)reader["BookID"];
        //}
        IList<Book> book = BookManage.BookManager.GetBookBybookId("-1 or BookID in("+sql+")" );
        this.DataList1.DataSource = book;
        this.DataList1.DataBind(); --------------------编程问答--------------------
引用 7 楼 zhengceHH 的回复:
错误说明楼上的几位说的都差不多,如果有疑问就接着问,闹明白儿的
上代码:
这是子查询方法,只访问一次数据库就可以了
        Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        换成 string sql = "select BookID from OrderForm where userID = '" + u.UserID + "'";
        //SqlDataReader reader = DBHelper.GetReader(sql);
        //OrderForm order = null;
        //if (reader.Read())
        //{
            //order = new OrderForm();
            //order.BookID = (int)reader["BookID"];
        //}
        IList<Book> book = BookManage.BookManager.GetBookBybookId("-1 or BookID in("+sql+")" );
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();



--------------------编程问答--------------------
引用 1 楼 Return_false 的回复:
reader 返回的是一个集合列表,而你只读取了一次
你应该循环读取
while(reader.Read())
{
    //循环读取该用户选择的书ID
    //然后根据ID得到该书的详细信息
}

其实你可以两个表做联合查询,然后读取该列表就可以了,包含订单和书的所有信息

select * from Order a inner join Book b on a.BookID=b.id

+1啊
或者楼主可以用dataset填充table啊 --------------------编程问答--------------------
引用 8 楼 ShenQiu____ 的回复:
Quote: 引用 7 楼 zhengceHH 的回复:

错误说明楼上的几位说的都差不多,如果有疑问就接着问,闹明白儿的
上代码:
这是子查询方法,只访问一次数据库就可以了
        Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        换成 string sql = "select BookID from OrderForm where userID = '" + u.UserID + "'";
        //SqlDataReader reader = DBHelper.GetReader(sql);
        //OrderForm order = null;
        //if (reader.Read())
        //{
            //order = new OrderForm();
            //order.BookID = (int)reader["BookID"];
        //}
        IList<Book> book = BookManage.BookManager.GetBookBybookId("-1 or BookID in("+sql+")" );
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();




错误提示不明显么?GetBookBybookId方法需要一个int型的ID,而你非要传一个字符串过去,要么重载方法,要么按该方法的传递参数来调用 --------------------编程问答--------------------
引用 8 楼 ShenQiu____ 的回复:
Quote: 引用 7 楼 zhengceHH 的回复:

错误说明楼上的几位说的都差不多,如果有疑问就接着问,闹明白儿的
上代码:
这是子查询方法,只访问一次数据库就可以了
        Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        换成 string sql = "select BookID from OrderForm where userID = '" + u.UserID + "'";
        //SqlDataReader reader = DBHelper.GetReader(sql);
        //OrderForm order = null;
        //if (reader.Read())
        //{
            //order = new OrderForm();
            //order.BookID = (int)reader["BookID"];
        //}
        IList<Book> book = BookManage.BookManager.GetBookBybookId("-1 or BookID in("+sql+")" );
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();






Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        SqlDataReader reader = DBHelper.GetReader(sql);
        OrderForm order = null;
        List<Book> booklist=new List<Book>();
        while (reader.Read())
        {
            order = new OrderForm();
            if(order.BookID!=null&&order.BookID.ToString()!="")
            {
             Book book=BookManage.BookManager.GetBookBybookId((int)reader["BookID"]; );
              bookList.Add(book);
             }
            
        }
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();


代码这么写  然后你在BookManage.BookManager.GetBookBybookId自己去写一个返回Book 的方法 --------------------编程问答--------------------
引用 10 楼 Return_false 的回复:
Quote: 引用 8 楼 ShenQiu____ 的回复:

Quote: 引用 7 楼 zhengceHH 的回复:

错误说明楼上的几位说的都差不多,如果有疑问就接着问,闹明白儿的
上代码:
这是子查询方法,只访问一次数据库就可以了
        Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        换成 string sql = "select BookID from OrderForm where userID = '" + u.UserID + "'";
        //SqlDataReader reader = DBHelper.GetReader(sql);
        //OrderForm order = null;
        //if (reader.Read())
        //{
            //order = new OrderForm();
            //order.BookID = (int)reader["BookID"];
        //}
        IList<Book> book = BookManage.BookManager.GetBookBybookId("-1 or BookID in("+sql+")" );
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();




错误提示不明显么?GetBookBybookId方法需要一个int型的ID,而你非要传一个字符串过去,要么重载方法,要么按该方法的传递参数来调用

不好意思上面代码写错了:
这样子的:

Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        SqlDataReader reader = DBHelper.GetReader(sql);
        List<Book> booklist=new List<Book>();
        while (reader.Read())
        {
            if(reader["BookID"]!=null&&reader["BookID"].ToString()!="")
            {
             Book book=BookManage.BookManager.GetBookBybookId((int)reader["BookID"]; );
              bookList.Add(book);
             }
             
        }
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();
--------------------编程问答-------------------- 得到一笔记录,是因为使用了等号:
--------------------编程问答-------------------- 调试运行后发现:我的DataList1中最多只能显示一条数据,想问下为什么呀?
if 改成while

而且这个代码貌似还有一个问题就是,假如说UserID对应的没有BookID,VS会报错。
判断reader["BookID"]是否为空
--------------------编程问答--------------------

..
IList<Book> book = new List<Book>(); //这里创建一个集合
if (reader.Read())
{
   order = new OrderForm();
   order.BookID = (int)reader["BookID"];
   ...
   //这里必须往集合里加数据,而不是在外面加。否则你加的永远是最后一条记录
}
   
--------------------编程问答-------------------- 哦没看仔细,确实需要循环,这里只是读了一次而已 --------------------编程问答-------------------- Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        SqlDataReader reader = DBHelper.GetReader(sql);
        OrderForm order = null;
        if (reader.Read())
        {
            order = new OrderForm();
            order.BookID = (int)reader["BookID"];
        }
        IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();

你这样写根本不对,你想获得Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        SqlDataReader reader = DBHelper.GetReader(sql);
        OrderForm order = null;
        if (reader.Read())
        {
            order = new OrderForm();
            order.BookID = (int)reader["BookID"];
        }
        IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();

你这样写更本就不对,你想获得UserID下的BookID的信息。你可以这样修改:
Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        SqlDataReader reader = DBHelper.GetReader(sql);
        OrderForm order = null;
        if (reader.Read())
        {
            order = new OrderForm();
            order.BookID = (int)reader["BookID"];
        }
        IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();

你这样写根本不对,你想获得Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        SqlDataReader reader = DBHelper.GetReader(sql);
        OrderForm order = null;
IList<Book> book = new List<Book>();
        if (reader.Read())
        {
            order = new OrderForm();
            order.BookID = (int)reader["BookID"];
            book.Add(order);
        }
        //IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();

这样应该就可以了。


--------------------编程问答--------------------    
   IList<Book> booklist=new  new List<Book>();
   while(reader.Read())
        {
            Book book=new Book();
            order = new OrderForm();
            order.BookID = (int)reader["BookID"];
            book = BookManage.BookManager.GetBookBybookId(order .BookID );
            booklist.add(book);
        }
        this.DataList1.DataSource = booklist;
        this.DataList1.DataBind();
--------------------编程问答--------------------

Users user = (Users)Session["user"];
        Users u = new Users();
        u.UserID = user.UserID;
        string sql = "select * from OrderForm where userID = '" + u.UserID + "'";
        SqlDataReader reader = DBHelper.GetReader(sql);
        OrderForm order = null;
        while (reader.Read())
        {
            order = new OrderForm();
            order.BookID = (int)reader["BookID"];
        }
        IList<Book> book = BookManage.BookManager.GetBookBybookId(order .BookID );
        if(book!=null)
        {
        this.DataList1.DataSource = book;
        this.DataList1.DataBind();
         }
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,