C#:无法从记录集中检索数据
我得到的例外是“位置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是您的数据集不包含您尝试执行的查询的定义,并且数据最终位于不同的表上。也就是说,
select unique ... from ProductInfo
不会填充cokeDatabaseSQLDataSet
内的productInfo
数据表。您需要编写类似这样的内容来提取值:现在,如果您不将 dataTable 声明为 cokeDatabaseSQLDataSet,而是这样做:
那么您将能够使用此行获取您想要的值:
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 theproductInfo
data table inside thecokeDatabaseSQLDataSet
. You’d need to write something like this to extract the value:Now, if instead of declaring dataTable as cokeDatabaseSQLDataSet you do this:
Then you’ll be able to get the value you’re after with this line:
da.Fill(dataTable)
用于向 DataSet 添加行。因此,考虑到 dataTable 是一个 DataSet,请尝试以下操作:OR
da.Fill(dataTable)
is used for adding rows to the DataSet. So considering that dataTable is a DataSet, try this:OR
检查表数和行数
Check the Table Count and Row Count
呃!错误代码
您必须从数据集中读取表。
例子:
hic ! wrong code
You must read table from Dataset.
Example: