C#:无法从记录集中检索数据

发布于 2024-11-06 02:26:26 字数 1797 浏览 1 评论 0原文

我得到的例外是“位置 0 处没有行”:

我将消息框存根放在那里以验证我是否确实从数据库中获取了结果。尝试了不同的查询,它似乎工作正常。但是,当我尝试从结果集中检索该信息时,我不断收到该错误。我做错了什么?这是我加载的当前代码,它返回 1 个结果。我正在尝试获取该结果并将其作为双精度结果返回给程序。但无论我用它做什么,我仍然得到的错误是“位置 0 处没有行”。我已经尝试过位置1和位置2,结果相同。

查询是:“从productInfo中选择不同的sweetTarget,其中flavor = 'Classic Coke'”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

    namespace DatasetTest1
    {
        class dataLayer
        {

            static string connectionString = "Data Source=HALEY;Initial Catalog=cokeDatabaseSQL;Integrated Security=True";
            static SqlConnection conn = new SqlConnection(connectionString);
            static string sql = "select distinct sweetTarget from productInfo where flavor = 'Classic Coke'";
            static SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            static cokeDatabaseSQLDataSet dataTable = new cokeDatabaseSQLDataSet();

            public static double getTargetBrix()
            {
                double value = 0.00;
                try
                {

                    conn.Open();
                    int affectedRows = da.Fill(dataTable);

                    if (affectedRows > 0)
                    {
                        MessageBox.Show("Rows Returned: " + affectedRows);
                        value = dataTable.productInfo[0].sweetTarget;
                    }
                    else
                        MessageBox.Show("Empty Set");

                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
                finally
                {
                    conn.Close();
                }



                return value;
            }
        }
    }

The exception that im getting is "There is no row at position 0":

I put the messagebox stub in there to verify that im actually getting a result from the database. Tried different queries and it appears to be working fine. However when I try to retrieve that information from the result set, i keep getting that error. What am i doing wrong? Here is the current code i have loaded, it returns 1 result. I'm trying to take that one result and return it to the program as a double. But regardless of what I'm doing with it, the error im still getting is "no row at position 0". Ive tried position 1 and 2, and the same result.

the query is: "select distinct sweetTarget from productInfo where flavor = 'Classic Coke'"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;

    namespace DatasetTest1
    {
        class dataLayer
        {

            static string connectionString = "Data Source=HALEY;Initial Catalog=cokeDatabaseSQL;Integrated Security=True";
            static SqlConnection conn = new SqlConnection(connectionString);
            static string sql = "select distinct sweetTarget from productInfo where flavor = 'Classic Coke'";
            static SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            static cokeDatabaseSQLDataSet dataTable = new cokeDatabaseSQLDataSet();

            public static double getTargetBrix()
            {
                double value = 0.00;
                try
                {

                    conn.Open();
                    int affectedRows = da.Fill(dataTable);

                    if (affectedRows > 0)
                    {
                        MessageBox.Show("Rows Returned: " + affectedRows);
                        value = dataTable.productInfo[0].sweetTarget;
                    }
                    else
                        MessageBox.Show("Empty Set");

                }
                catch (Exception ex) { MessageBox.Show(ex.Message); }
                finally
                {
                    conn.Close();
                }



                return value;
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

征棹 2024-11-13 02:26:26

问题是您的数据集不包含您尝试执行的查询的定义,并且数据最终位于不同的表上。也就是说,select unique ... from ProductInfo 不会填充 cokeDatabaseSQLDataSet 内的 productInfo 数据表。您需要编写类似这样的内容来提取值:

value = (double) dataTable.Tables[dataTable.Tables.Count - 1].Rows[0][0];

现在,如果您不将 dataTable 声明为 cokeDatabaseSQLDataSet,而是这样做:

static DataTable dataTable = new DataTable();

那么您将能够使用此行获取您想要的值:

value = (double) dataTable.Rows[0][0];

The problem is that your DataSet does not contain a definition for the query you are trying to execute, and the data ends up on a different table. That is, select distinct ... from productInfo does not populate the productInfo data table inside the cokeDatabaseSQLDataSet. You’d need to write something like this to extract the value:

value = (double) dataTable.Tables[dataTable.Tables.Count - 1].Rows[0][0];

Now, if instead of declaring dataTable as cokeDatabaseSQLDataSet you do this:

static DataTable dataTable = new DataTable();

Then you’ll be able to get the value you’re after with this line:

value = (double) dataTable.Rows[0][0];
独孤求败 2024-11-13 02:26:26

da.Fill(dataTable) 用于向 DataSet 添加行。因此,考虑到 dataTable 是一个 DataSet,请尝试以下操作:

value = dataTable.Tables[0].Rows[0][0]

OR

value = dataTable.Tables[0].Rows[0][ColumnName]

da.Fill(dataTable) is used for adding rows to the DataSet. So considering that dataTable is a DataSet, try this:

value = dataTable.Tables[0].Rows[0][0]

OR

value = dataTable.Tables[0].Rows[0][ColumnName]
烟凡古楼 2024-11-13 02:26:26

检查表数和行数

dataTable.Tables.Count

dataTable.Tables[0].Rows.Count

Check the Table Count and Row Count

dataTable.Tables.Count

dataTable.Tables[0].Rows.Count
触ぅ动初心 2024-11-13 02:26:26

呃!错误代码

您必须从数据集中读取表。

例子:

foreach (DataRow dRow in affectedRows.Tables[0].Rows)
{
    var retval   = dRow["Keyword"].ToString();
    MessageBox.Show(retval );
}

hic ! wrong code

You must read table from Dataset.

Example:

foreach (DataRow dRow in affectedRows.Tables[0].Rows)
{
    var retval   = dRow["Keyword"].ToString();
    MessageBox.Show(retval );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文