C#-如何一个接一个地调用数据库记录并将其显示在ListView中? [英] C# - How to call database records one-by-one and display it in ListView?

查看:72
本文介绍了C#-如何一个接一个地调用数据库记录并将其显示在ListView中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的人们

我面临一个大问题...

我创建了一个C#项目.在这个项目中,我有一个Form1(在此表单中,我有一个Button1,Panel1和ListView1),一个可重用的UserControl1(在此用户控件中,我有10个按钮,例如:cmdOlives,cmdSoup等),并且我还有一个Access 2007数据库(在我的数据库中,我创建了一个包含10条记录的表).哦,我差点忘了,我还创建了一个名为MenuItems.cs的类(请参见该类下面的代码)...

到目前为止,我所做的是:
-从Form1中,Button1_Click事件调用UserControl1并将其显示(在Panel1内)到我的Form1中.
我要实现的目标是:
-UserControl1中要动态链接到数据库的10个按钮,我的意思是动态地表示我不想为每个button_click事件创建一个打开的db连接,exec查询等,但是只创建一个SQL查询(就像数组查询一样说),所以当我单击cmdOlives来从数据库表中获取rocord1并将其显示在我的ListView1中,与cmdSoup相同,等等...

从下面的代码中可以看到,我已经创建了sql查询,但是由于某种原因,它没有在listview中显示任何数据.我遗漏了一些东西,而且我也卡住了.

如果有人可以帮我解决这个问题,那将非常好(我的意思是提供完整的代码示例)...

在这里,我将根据我的代码进行解释.

-这是MenuItems.cs类的代码

Dear People

Im facing a big problem...

I have created a C# Project. In this project I have a Form1 (inside this form I have a Button1 , Panel1 and ListView1 ), a reusable UserControl1 (inside this usercontrol I have 10 buttons such as: cmdOlives , cmdSoup , etc, etc ..) and also I have a Access 2007 Database (In my database I have a table created with 10 records). Oh I almost forgot, I have created a class aswell called MenuItems.cs (see code below of this class)...

What I have done so far is:
- From Form1 the Button1_Click event calls UserControl1 and displays it (inside Panel1 ) into my Form1 .

What Im trying to achieve is this:
- The 10 buttons in UserControl1 to be linked dynamically with my Database, what I mean by dynamically is that I dont want to create for every button_click event an opening db connection, exec query, and so on , but to create one and only one SQL query (lest say like an array query) so when I click cmdOlives to get the rocord1 from database table and display it into my ListView1 , same for cmdSoup and so on...

As you can see from the code below I have created the sql query but for some reason its not displaying any data into my listview Im missing something and Im also stuck.

If someone could help me with this it would be very greatfull (I mean with a full sample of code)...

Here I will try to explain based on my code.

- Here is the code for MenuItems.cs class

public class MenuItems
    {
        private int itemNum = 0;
        public int ItemNum
        {
            get{return itemNum;}
            set{itemNum = value;}
        }
        private string itemName;
        public string ItemName
        {
            get{return itemName;}
            set{itemName = value;}
        }
        private double itemPrice = 0.0;
        public double ItemPrice
        {
            get{return itemPrice;}
            set{itemPrice = value;}
        }
        public void menuitemParam(int ItNo, string nam, double cost)
        {
            itemNum = ItNo; itemName = nam; itemPrice = cost;
        }
        public void meItem(ref MenuItems mi)
        {
            itemNum = mi.itemNum; itemName = mi.itemName; itemPrice = mi.itemPrice;
        }
    }


-这是UserControl1.cs类
-从这个类中,我想将数据库记录显示到我的列表视图中(在Form1中)...

公共局部类UserControl1:UserControl
{
公共UserControl1()
{
InitializeComponent();
}
私有void UserControl1_Load(对象发送者,EventArgs e)
{
}
//这是我单击此按钮以将数据显示到我的Listview中的一部分...


- Here is the UserControl1.cs class
- From this class I want to display database records to my listview (in Form1)...

public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
//This a part that when I click this button to display the data in to my Listview...

private void cmdOlives_Click(object sender, EventArgs e)
        {
            if (menuItems.Count > 0)
            {
                System.Windows.Forms.ListViewItem newItem = new System.Windows.Forms.ListViewItem(menuItems[1].ToString());
                for (int i = 1; i < menuItems[1].ItemName.Length; i++)
                {
                    newItem.SubItems.Add(menuItems[i].ToString());
                }
                TableOrderListView.Items.Add(newItem);
            }
        }
        private void cmdSoup_Click(object sender, EventArgs e)
        {
        }
        .......
        .......
    }



-加载Form1时(请参见下面的代码,我已创建数据库连接)



- When Form1 Loads (see the code below I have created database connection)

public partial class DtposMDIParentSystem : Form
{
     List<menuitems> menuItems = new List<menuitems>();
     public DtposMDIParentSystem()
     {
          InitializeComponent();
          //create the database connection<
          OleDbConnection aConnection = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;
                   Data Source=C:\Users\AP_AE\Desktop\DTPOS_APP\DataBase\DtposDatabase.accdb;");
          //create the command object and store the sql query
          OleDbCommand aCommand = new OleDbCommand("SELECT * FROM Food", aConnection);
          try
          {
               aConnection.Open();
               //create the datareader object to connect to table
               OleDbDataReader reader = aCommand.ExecuteReader();
               while (reader.Read())
               {
                   MenuItems mi = new MenuItems();
                   mi.ItemName = reader["Name"].ToString();
                   mi.ItemPrice = Double.Parse(reader["Price"].ToString());
                   menuItems.Add(mi);
               }
               reader.Close();
               aConnection.Close();
          }
          catch (InvalidOperationException ex)
          {
               MessageBox.Show("Invalid Masseage = " + ex.Message);
          }
     }
}</menuitems></menuitems>


-单击按钮1(在Form1内部)调用UserControl1并将其显示到Form1中(请参见下面的代码)


- Click the button1 (inside Form1) calls the UserControl1 and displays it into Form1 (see the code below)

UserControl1 userC = new UserControl();
        private void cmdStarters_Click(object sender, EventArgs e)
        {
            this.StartersPanel.Visible = true;
            this.userC.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.userC.Location = new System.Drawing.Point(0, 0);
            this.userC.Size = new System.Drawing.Size(696, 556);
            this.userC.Enabled = true;
            this.userC.Visible = true;
            this.StartersPanel.Controls.Add(userC);
        }


-UserControl1有10个按钮(例如cmdOlives,cmdSoup等...)
-现在,当我单击时:
-cmdOlives_Click事件按钮我想从数据库表中显示rocord1(例如Olives)并将其显示到我的TableOrderListView ...
-另外,当我点击以下内容时:
-cmdSoup_Click事件按钮我想从数据库表中显示rocord2(例如Soup)并将其显示到我的TableOrderListView ...中,依此类推
这就是我希望数据显示在ListView中的方式....
ListView
_________________________________________
编号名称价格
_________________________________________
1橄榄£2.95
1汤£4.95
. . .
_________________________________________
我希望我的解释清楚,以便谁读过我的问题可以理解我正在尝试做什么...
亲切的问候
Roni


- The UserControl1 has 10 Buttons (such as cmdOlives, cmdSoup etc. etc...)
- Now when I click on:
- cmdOlives_Click event button I want to display rocord1 (such as Olives) from database table and display it into my TableOrderListView...
- Also when I click on:
- cmdSoup_Click event button I want to display rocord2 (such as Soup) from database table and display it into my TableOrderListView... and so on
Here is how I want the data to be shown in my ListView....
ListView
_________________________________________
Num Name Price
_________________________________________
1 Olives £2.95
1 Soup £4.95
. . .
_________________________________________
I hope my explonation is clear so who ever reads my question can understand what im trying to do...
kind regards
Roni

推荐答案

您正在使用datareader对象而不是从数据集中读取记录从数据库中逐条读取记录,并管理自己的指向数据集每一行的光标指针. U可以根据此指针从数据集中一一调用记录,任何需要的代码都可以随意询问
有关在garadshree25@gmail.com上发邮件给我的任何查询
You are reading one by one record from database using datareader object instead of that fetch record in dataset and manage u r own cursor pointer to each row of dataset. U can call then one by one record from dataset on the basis of this pointer any code required feel free to ask
any query regarding mail me on garadshree25@gmail.com


这篇关于C#-如何一个接一个地调用数据库记录并将其显示在ListView中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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