简体   繁体   English

从OleDb查询检索数据未显示在MessageBox中

[英]Retrieving data from OleDb query not displaying in MessageBox

I get no exception errors with the below code which could mean there is no problem with my sql statement. 我的以下代码没有异常错误,这可能意味着我的sql语句没有问题。 However upon trying to verify if it returns any value, i pass the returned items to a string value and try to display it in a message box. 但是,在尝试验证它是否返回任何值时,我将返回的项目传递给字符串值,并尝试在消息框中显示它。

the problem am facing is the message box only displays the name of the column and not the data ive requested from the table. 面临的问题是消息框仅显示列的名称,而不显示表中请求的数据。

what could possibly be the problem? 可能是什么问题? and please advice if theres a better way to work around this... 如果有更好的解决方法,请提出建议...

    public void DisplayPurchase(OleDbConnection mDB)
    {
        openDB();
        string sqlQuery;
        OleDbCommand cmd;
        OleDbDataReader rdr;



        sqlQuery = "SELECT CustomerTable.[Youth ID], CustomerTable.Firstname, " +
            "CustomerTable.Lastname, Youth.Purchaseid, Youth.NumbersOfSport, " + 
            "Youth.Price, Youth.TotalCostOfTraining, Youth.PercentageDiscount, " +
            "Youth.AmountDue, Youth.DatePurchased" +
            " FROM CustomerTable, Youth WHERE Youth.YouthID = CustomerTable.[Youth ID]" +
            " AND CustomerTable.[Youth ID] = 7";

        try
        {
            cmd = new OleDbCommand(sqlQuery, mDB);

            rdr = cmd.ExecuteReader();
            if (rdr.HasRows)
            {
                qtyInt1 = (int)rdr["Youth.NumbersOfSport"];
                youthInt1 = (int)rdr["CustomerTable.[Youth ID]"];
                firstStr1 = (string)rdr["CustomerTable.Firstname"];
                purStr1 = (string)rdr["Youth.Purchaseid"];
                lastStr1 = (string)rdr["CustomerTable.Lastname"];
                priceStr1 = (string)rdr["Youth.Price"];
                totalCstStr1 = (string)rdr["Youth.TotalCostOfTraining"];
                discountStr1 = (string)rdr["Youth.PercentageDiscount"];
                amtDueStr1 = (string)rdr["Youth.AmountDue"];
                //purDate1 = (DateTime)rdr["Youth.DatePurchased"];

                MessageBox.Show(firstStr1.ToString());

                closeDB();

            }
            else
            {
                MessageBox.Show("Reader has no rows");
                closeDB();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    }

Thank you 谢谢

I'm pretty sure that you have to call rdr.Read() before you can access any data from it. 我很确定您必须先调用rdr.Read()才能访问其中的任何数据。 So, add that as the first line after if(rdr.HasRows()) 因此,将其添加为if(rdr.HasRows())之后的第一行

You need to call Read() on the reader to read the first row. 您需要在阅读器上调用Read()以阅读第一行。

if(rdr.HasRows) {
    rdr.Read();
    ...

Best wishes, 最好的祝愿,
Fabian 法比安

Have you tried: 你有没有尝试过:

firstStr1 = (string)rdr["Firstname"];

The fieldname in a datareader typically does not include the tablename prefix. 数据读取器中的字段名通常不包含表名前缀。

Remove the table names when you are retrieving the data: 检索数据时,请删除表名称:

qtyInt1 = (int)rdr["NumbersOfSport"];
youthInt1 = (int)rdr["Youth ID"];  // You may need to rename this one in the query
firstStr1 = (string)rdr["Firstname"];
purStr1 = (string)rdr["Purchaseid"];
lastStr1 = (string)rdr["Lastname"];
priceStr1 = (string)rdr["Price"];
totalCstStr1 = (string)rdr["TotalCostOfTraining"];
discountStr1 = (string)rdr["PercentageDiscount"];
amtDueStr1 = (string)rdr["AmountDue"];

Its amazing how this stuff works... Id firstly like to thank everybody that contributed to solving this problem.Am really grateful for every alphabet posted. 首先,我要感谢所有为解决这个问题做出贡献的人。我非常感谢张贴的每个字母。

sqlQuery = "SELECT Youth.YouthID, Firstname, Lastname, NumbersOfSport, Price, TotalCostOFTraining, PercentageDiscount, Purchaseid, AmountDue, DatePurchased FROM CustomerTable, Youth WHERE CustomerTable.YouthID = Youth.YouthID AND Youth.YouthID = "+ toSql(youthInt); sqlQuery =“ SELECT Youth.YouthID,名字,姓氏,NumbersOfSport,价格,TotalCostOFTraining,百分比折扣,Purchaseid,AmountDue,从CustomerTable购买的Date,YouthWHERECustomerTable。YouthID= Youth.YouthID和Youth.YouthID =” + toSql(youthInt);

        try
        {
            cmd = new OleDbCommand(sqlQuery, mDB);

            rdr = cmd.ExecuteReader();
            if (rdr.HasRows)
            {
                rdr.Read();
                qtyInt1 = (int)rdr["NumbersOfSport"];
                youthInt1 = (int)rdr["YouthID"];
                firstStr1 = (string)rdr["Firstname"];
                purInt1 = (int)rdr["Purchaseid"];
                lastStr1 = (string)rdr["Lastname"];
                priceStr1 = (string)rdr["Price"];
                totalCstStr1 = (string)rdr["TotalCostOfTraining"];
                discountStr1 = (string)rdr["PercentageDiscount"];
                amtDueStr1 = (string)rdr["AmountDue"];
                purDate1 = (DateTime)rdr["DatePurchased"];



                closeDB();

            }
            else
            {
                MessageBox.Show("Reader has no rows");
                closeDB();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }


    }

I had to call for the rdr.read(); 我不得不打电话给rdr.read(); functions as well as taking of the table referencing on the database columns and each help came from a different sources..... thats awesome... Thanks everybody... 功能以及获取在数据库列上引用的表,每个帮助都来自不同的来源.....太棒了...谢谢大家...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM