如何使用sql查询在listView控件中显示来自ms访问的数据? [英] How do I display data from ms access in a listView control using sql queries?

查看:51
本文介绍了如何使用sql查询在listView控件中显示来自ms访问的数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用C#和ms访问数据库在microsoft visual web developer 2010中的列表视图控件中显示数据库中的数据。我的用户需要能够选择一个选项(预订房间)。



用户从两个文本框中选择到达日期和出发日期然后按搜索按钮。到目前为止,我有以下代码:

I would like to display the data in my database in a list view control in microsoft visual web developer 2010, using C# and an ms access database. My users need to be able to select an option (to reserve a room).

The user selects an arrival date and a departure date from two text boxes and then presses the search button. I have the following code so far:

protected void searchBtn_Click(object sender, EventArgs e)
        {
            //when the search button is selected these controls become visible
            selectRoomLbl.Visible = true; 
            reserveBtn.Visible = true;
            ListView1.Visible = true;

            try
            {
                //creates a connection to the database
                dbConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" +
                    Request.PhysicalApplicationPath + "App_Data\\Mercure.accdb");

                dbConnection.Open();

                //query retrieves all available rooms for reservation
                string query = "SELECT * FROM RoomsDetails WHERE roomID NOT IN(";
                query += "SELECT roomID FROM ReservationDetails WHERE NOT(";
                query += "dateArrival < " + arrivalTxt.Text + "OR dateDeparture > " + departureTxt.Text + "))";
                query += "ORDER BY RoomsDetails.roomID";

                OleDbCommand cmd = dbConnection.CreateCommand();
                cmd.CommandText = query;
                OleDbDataReader dbReader = cmd.ExecuteReader();

                if (dbReader != null && dbReader.HasRows)
                {
                    while (dbReader.Read())
                    {
                        //I am not too sure how to display in the listview control                  
                    }
                }                 
            }
            finally
            {
                dbConnection.Close();
            }
        }

推荐答案

有几种方法可以添加到ListView控件。重要的是要添加到Items集合。



参见 http://stackoverflow.com/questions/9951704/add-item-to-listview-control [ ^ ]用于几种不同的方法。
There are several ways to add to a ListView control. The important thing to know is to add to the Items collection.

See http://stackoverflow.com/questions/9951704/add-item-to-listview-control[^] for a few different methods.


这篇关于如何使用sql查询在listView控件中显示来自ms访问的数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆