我有這樣的SQL:SQL從多個一份聲明中檢索值選擇
SELECT count (1) FROM users AS total_drafts WHERE version_replace = @sid
SELECT count (1) AS unpublished_drafts FROM users WHERE version_replace = @sid AND moderated = 0
SELECT * FROM users WHERE id = @sid ORDER By ID DESC
這似乎是正確的。不過,我很難從結果中提取字段。在vb.net我使用這個代碼片段:
While r.Read()
Dim o_email As String = CStr(r("email"))
Dim o_first_name As String = CStr(r("first_name"))
Dim o_last_name As String = CStr(r("last_name"))
這是造成此錯誤:System.IndexOutOfRangeException:電子郵件 我已經檢查了SQL被正確exucuted。我發佈的SQL只是簡單地替換了一個簡單的語句,它完美地提供給代碼片段。
這是爲什麼?如何糾正?
正確的方法:
While r.Read()
total_drafts = CInt(r("total_drafts"))
End While
r.NextResult()
While r.Read()
unpublished_drafts = CInt(r("unpublished_drafts"))
End While
error_status.Text = total_drafts & " " & unpublished_drafts
r.NextResult()
While r.Read()
Dim o_email As String = CStr(r("email"))
Dim o_first_name As String = CStr(r("first_name"))
Dim o_last_name As String = CStr(r("last_name"))
爲什麼你不試一試列索引而不是列名,這可能會幫助你。 –
你能發佈一個示例輸出(來自SQL)嗎? – StevieG