這非常緊迫,我需要在3個半小時內呈現此應用程序。SqlDataReader在沒有數據時獲取行
我的應用程序根據數據源檢查數據庫中是否存在值,並根據是否找到有問題的值更改值。 問題是我已經在SSMS中運行帶有問題的值的sql查詢並且沒有行被返回,但是,我的DataReader說它有行。
這意味着我的應用程序報告不準確。 這裏是我的代碼:
Using conn As New SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=Testing; Integrated Security=True;")
Dim cmd As New SqlCommand(sql, conn)
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader
If reader.HasRows Then
Value = True
AcctNumber = reader(1)
End If
reader.Close()
End Using
我已經刪除代碼是不相關的這篇文章,但你可能想知道的是:
值是布爾 ACCTNUMBER是一個String
由於這是一個工作的應用程序,我寧願不包括SQL查詢。問題是讀者。如果我註釋掉Value = True
,我會得到正確的信息,但如果將Value設置爲True,那麼它將不準確地報告。
在此先感謝!
編輯:完整的源代碼:
Case "Business"
' Change the number format to local because that's what it is in the db
If Microsoft.VisualBasic.Left(NumberToCheck, 2) = "27" Then
NumberToCheck = NumberToCheck.Replace(Microsoft.VisualBasic.Left(NumberToCheck, 2), "0")
End If
Dim sql As String = "SELECT a.TelNumber, c.AccountNumber " & _
"FROM TelInfo a " & _
"INNER JOIN Customers b ON a.CustID = b.pkguidId " & _
"INNER JOIN Accounts c ON b.pkguidId = c.CustID " & _
"WHERE a.TelNumber = '" & NumberToCheck & "'"
Using conn As New SqlConnection("Data Source=.\SQLEXPRESS; Initial Catalog=Testing; Persist Security Info=True; " & _
"User Id=JoeSoap; Password=paoseoj;")
Dim cmd As New SqlCommand(sql, conn)
conn.Open()
Dim reader As SqlDataReader = cmd.ExecuteReader
If reader.Read Then
Value = True
AcctNumber = reader(1)
End If
reader.Close()
End Using
在08/02/10之前(MM/DD/YY)下面提出的意見:
Value
僅僅是得到由返回的布爾功能來指示搜索到的電話號碼(NumberToCheck
)存在於數據庫中。
所以......
Private AcctNumber As String
Dim val As Boolean = False
val = CheckNumber("3235553469")
If val Then
' AcctNumber will have been set by CheckNumber
Label1.Text = AcctNumber
End If
val
如果NumberToCheck(在本例3235553469)數據庫中存在纔會返回真。
將NumberToCheck的值複製到SSMS中並在那裏測試查詢後,我可以驗證查詢是否按預期工作。
不,我無法填充數據集,因爲表中的信息量(+/- 9.5m行)。即使使用「WHERE」過濾器,查詢對資源來說過於沉重,最終以OutOfMemory Exception
結尾,這就是我使用DataReader的原因。
我打算嘗試ExecuteScalar
選項,現在由Darryl建議作爲答案,將更新結果。
您能向我們展示SQL嗎? – egrunin 2010-07-29 09:48:41
我知道編輯中的ConnectionString與原始的不同。爲了合法目的,我必須改變某些事情......我正在使用的信息非常敏感。 – 2010-07-29 10:03:11
連接字符串是最不可能的問題。 – egrunin 2010-07-29 10:14:13