数据读取器检索数据 [英] Datareader Retrieving Data

查看:35
本文介绍了数据读取器检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在包含属性 cust_id 的数据库中有 Memberships 和 Bookings 表,它是 Memberships 中的主键和 Bookings 中的引用键.当我执行数据读取器时,我希望它从成员资格表中读取 cust_id 值,但它正在从预订表中读取它.

I have Memberships and Bookings tables in a database containing an attribute cust_id, which is the primary key in Memberships and reference key in Bookings. When I am executing a data reader I want it to read cust_id values from membership table but it is reading it from the bookings table.

此外,当我比较两个 cust_id 值时,1 个取自文本​​框,另一个取自数据库列,即使两者相同,但比较结果为 false.我已经使用 string.equals(str1, str2) 进行了比较,并且还直接使用 if 语句比较了两者.但在这两种情况下,即使字符串相同,结果也是不同的.

Also, when I compare two cust_id values, 1 taken from a textbox and the other taken a from database column, even though both are the same, but the comparison result is false. I have compared using string.equals(str1, str2) and have also compared the two directly using if statement. But in both cases, even if the string is the same, the result is otherwise.

我的查询是:

 str2 = "select Memberships.cust_id from Memberships, Bookings where   Memberships.cust_id = Bookings.cust_id"
Dim cmd2 As New SqlCommand(str2, con)
con.Open()

Dim bookchk As SqlDataReader = cmd2.ExecuteReader  
While bookchk.Read()
Dim str1 As String = MskdTxtCustId.Text
Dim str3 As String = bookchk("cust_id")
MessageBox.Show(str1 & "," & str3 & String.Equals(str1, str3))

End While
    bookchk.Close()
    con.Close()

推荐答案

尝试明确声明您想要一个 INNER JOIN 代替:

Try explicitly stating that you want an INNER JOIN instead:

str2 = "select m.cust_id from Memberships AS M " &_
       " INNER JOIN Bookings AS B ON B.cust_id = M.cust_id;"

比较时,使用String.Compare(),并确保您正在修剪空格,以防万一.

When comparing, use String.Compare(), and make sure you're trimming whitespace, just in case.

Dim str1 As String = MskdTxtCustId.Text.Trim()
Dim str3 As String = bookchk("cust_id").ToString().Trim()

Dim compareResult As Integer = String.Compare(s1, s2)
MessageBox.Show("compare str1: {0}, str3: {1}, result: {2}" , str1, str3, compareResult)

compareResult = String.Compare(s1, s2, True)
MessageBox.Show("Compare insensitive. result: {0}", compareResult)

这篇关于数据读取器检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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