使用 OleDbDataReader 检索值

发布于 2024-09-13 20:57:30 字数 483 浏览 1 评论 0原文

让我们认为我的 sql 查询是

  select customerDetials.custid,TestTable.col1 from CustomerDetails INNER JOIN TestTable  on CustomerDetails.custid=TestTables.custid where CustomerDetails.custid>0

我想使用 OleDbDataReader 来检索 this 的行。

我使用这种方式

while (dataReader.Read())
{
       string str= dataReader["customerDetials.custid"].ToString();
}

,但问题是这里连接在那里,所以如果我给出像上面这样的列名,它会抛出异常,并且我不能使用索引或者我不能更改sql查询。那么有什么方法可以使用检索数据列名?

Let us think my sql query is

  select customerDetials.custid,TestTable.col1 from CustomerDetails INNER JOIN TestTable  on CustomerDetails.custid=TestTables.custid where CustomerDetails.custid>0

I wanna use OleDbDataReader to retrieve rows of this .

I used this way

while (dataReader.Read())
{
       string str= dataReader["customerDetials.custid"].ToString();
}

but the prob is here join is there so if I give column name like above its throwing me an exception and I can't use index or I cant change the sql query .So is there any way to retrieve the data using the column name?

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

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

发布评论

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

评论(3

桃气十足 2024-09-20 20:57:30

你有没有尝试过只使用

while (dataReader.Read()) { 
    string str= dataReader["custid"].ToString();    
}

Have you tried using just

while (dataReader.Read()) { 
    string str= dataReader["custid"].ToString();    
}
诠释孤独 2024-09-20 20:57:30

我认为你想要的是...

string str = dataReader.GetInt32(0).ToString();

其中 (0) 指的是查询中的列的从零开始的序数位置

I THINK what you want is...

string str = dataReader.GetInt32(0).ToString();

where the (0) refers to the zero-based ordinal position of the columns as in the query

如果您不知道查询将返回什么,则必须:

获取 FieldCount(结果集中的列数),然后循环访问 DataReader 的每行中的字段(列)以检索数据。

您将需要以下一种或多种方法:

  • GetFieldType(int) - 返回列的类型。
  • GetDataTypeName(int) - 返回后端数据库的名称
    列类型。
  • GetName(int) - 返回列名称。

If you don't know what the query will return, you will have to:

Get the FieldCount (number of columns in the result set) and then loop through the fields (columns) in each row of the DataReader retrieving the data.

You will need one or more of the following medthods:

  • GetFieldType(int) - returns the Type of the column.
  • GetDataTypeName(int) - returns the Name of the back-end database
    column type.
  • GetName(int) - returns the Column Name.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文