在消息框中显示多个数据库记录 [英] Displaying multiple database records in a messagebox

查看:102
本文介绍了在消息框中显示多个数据库记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过循环在消息框中显示数据库记录,但每次执行程序时我只显示一条记录。我现在被困在这里...



我尝试了什么:



private void checkProductParLevelBreach()

{

List< string> dataInTableInventory = new List< string>();

int parLevel = 50;

使用(连接)

using(var messageQuery = new SqlCommand (select * from Inventory,连接))

{

connection.Open();

SqlDataReader datareader = messageQuery.ExecuteReader() ;

datareader.Read();

for(int i = 0; i< = datareader.FieldCount; i ++)

{

if(parLevel> Int32.Parse(datareader [4] .ToString()))

{

string selectID = datareader [0] .ToString();

dataInTableInventory.Add(selectID);

}

}



foreach(dataInTableInventory中的字符串产品)

{

string ModelName = datareader [2] .ToString();

MessageBox.Show(ModelName +已达到产品等级!,MessageBoxIcon.Warning.ToString());

}

}

}

解决方案

首先,了解答案OriginalGriff发布在这里,并确保您的代码做正确的事情,以显示您希望用户看到的数据。



然后:



我强烈建议您使用单独的表格来显示您的数据。您可以使用'ShowDialog以模态方式显示表单,并传回'DialogResult和/或其他信息。



看看它有多简单:假设你有



a。创建了一个名为'DataDisplayDialogForm的表单,



b。你已经通过设置'ControlBox = false



c隐藏了Form TitleBar控件。你在它上面放了一个名为'btnExit



d的按钮。你已经把一个CheckBox放在它上面,名为'cbxApproved



e。你已经将'表单的AcceptButton属性设置为'btnExit



f。你已经放置了任何控件来显示表单上的数据,并且你已经实现了显示数据的方法。



用作对话框的表单示例:< pre lang =C#> 使用系统;
使用 System.Windows.Forms;

命名空间 YourNameSpace
{
public partial class DataDisplayDialogForm:表单
{
public DataDisplayDialogForm()
{
InitializeComponent();
}

private void DataDisplayDialogForm_Load( object sender,EventArgs e)
{
this .DialogResult = DialogResult.No;
}

private void btnExit_Click( object sender,EventArgs e)
{
this .DialogResult =(cbxApproved.Checked)
? DialogResult.Yes
:DialogResult.No;

this .Close();
}
}
}

然后,您可以从主窗体显示此窗体的实例作为对话框,并显示实例作为单击按钮时的对话框:

  private  DataDisplayDialogForm dataForm; 

private void MainForm_Load(对象 sender,EventArgs e)
{
dataForm = new DataDisplayDialogForm();
}

private void ShowDataButton_Click( object sender,EventArgs e)
{
DialogResult viewResult = dataForm.ShowDialog();

if (viewResult == DialogResult.Yes)
{
// 数据已批准
}
其他
{
// 数据未获批准
}
}

正如您所看到的,当自定义对话框Form的实例关闭时,DialogResult返回到'MainForm将取决于是否选中自定义对话框上的CheckBox。



当您需要传回自定义数据时,不仅仅是'DialogResult枚举值',可以通过多种方式完成:通过创建自定义类来发回;通过使用可能使用自定义EventArgs类等来引发事件。或者,您可以通过设置包含对它们的引用的公共属性来公开您用于显示数据的控件。而且,还有其他技术。


嗯,是的......你会的。

看看你的代码:你只需要在你的DataReader上发一个Read,然后你循环FieldCount - 这是行的列数 - 并且只在该循环内重复访问第一列。

如果你想访问多个列,你需要使用它们你的 for 循环变量 i ;如果你想访问多行,那么你需要一个循环在DataReader上做多个Read操作来依次获取每一行:

< pre lang =c#> 使用(SqlDataReader datareader = messageQuery.ExecuteReader())
{
while (datareader.Read())
{
...
dataInTableInventory.Add(selectID);
}
}





请自己帮个忙:不要做 SELECT * 请求,改为命名列。当您使用带编号的列访问权限时,这一点尤为重要,因为对您的数据库进行小的更改会对您的代码执行一些可怕的操作!另外,检索不使用的列的效率非常低:因此获取表的每一列只使用第一列和第五列并不是一个好主意。


I am trying to display database records in a messagebox via a loop but everytime the program is executed I only get one record displayed. I am stuck here now...

What I have tried:

private void checkProductParLevelBreach()
{
List<string> dataInTableInventory = new List<string>();
int parLevel = 50;
using (connection)
using (var messageQuery = new SqlCommand("select * from Inventory", connection))
{
connection.Open();
SqlDataReader datareader = messageQuery.ExecuteReader();
datareader.Read();
for (int i = 0; i <= datareader.FieldCount; i++)
{
if (parLevel > Int32.Parse(datareader[4].ToString()))
{
string selectID = datareader[0].ToString();
dataInTableInventory.Add(selectID);
}
}

foreach (String product in dataInTableInventory)
{
string ModelName = datareader[2].ToString();
MessageBox.Show(ModelName + " have reached the product par level!", MessageBoxIcon.Warning.ToString());
}
}
}

解决方案

First, understand the answer OriginalGriff posted here, and make sure your code does the "right thing" to display the data you wish the user to see.

Then:

I strongly suggest you use a separate Form to display your data. You can show the Form modally using 'ShowDialog, and pass back a 'DialogResult and/or other information.

Look how simple this is: assume you have

a. created a Form named 'DataDisplayDialogForm,

b. you have hidden the Form TitleBar controls by setting 'ControlBox = false

c. you have put one Button on it named 'btnExit

d. you have put one CheckBox on it named 'cbxApproved

e. you have set the 'AcceptButton Property of the Form to 'btnExit

f. you have put whatever controls to display the data on the Form, and you have implemented the methods to display the data.

Example of Form to used as a Dialog:

using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class DataDisplayDialogForm : Form
    {
        public DataDisplayDialogForm()
        {
            InitializeComponent();
        }

        private void DataDisplayDialogForm_Load(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No;
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.DialogResult = (cbxApproved.Checked)
                ? DialogResult.Yes
                : DialogResult.No;
            
            this.Close();
        }
    }
}

Then, you can display an instance of this Form as a Dialog from your Main Form, and show that instance as a Dialog when a Button is clicked:

private DataDisplayDialogForm dataForm;

private void MainForm_Load(object sender, EventArgs e)
{
    dataForm = new DataDisplayDialogForm();
}

private void ShowDataButton_Click(object sender, EventArgs e)
{
    DialogResult viewResult = dataForm.ShowDialog();

    if (viewResult == DialogResult.Yes)
    {
        // data approved
    }
    else
    {
        // data not approved
    }
}

As you can see,the DialogResult returned to the 'MainForm when the instance of the custom dialog Form is closed will depend on whether the CheckBox on the custom dialog is checked or not.

When you need to pass back custom data, not just a 'DialogResult Enumeration value, that can be done in several ways: by creating a Custom Class to "send back;" through the use of raising Events possibly using a custom EventArgs class, etc. Or, you can expose the Controls you use to display the Data by setting Public Properties that hold references to them. And, there are other techniques.


Well, yes... you will.
Look at your code: you only ever issue one Read on your DataReader, and then you loop on the FieldCount - which is the columns count for the row - and only ever access the first column repeatedly inside that loop.
If you want to access multiple columns, they you need to use your for loop variable i; if you want to access multiple rows, then you need a while loop doing multiple Read operations on the DataReader to fetch each row in turn:

using (SqlDataReader datareader = messageQuery.ExecuteReader())
   {
   while (datareader.Read())
      {
      ...
      dataInTableInventory.Add(selectID);
      }
   }



And please, do yourself a favour: don't do SELECT * requests, name the columns instead. This is particularly important when you use numbered column access as a small change to your DB can do some horrible things to your code! In addition is't very inefficient to retrieve columns you don't use: so fetching every column of your table to use just the first and fifth as you do is not a good idea.


这篇关于在消息框中显示多个数据库记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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