保存具有从 Microsoft Access 数据库读取的多个数据的 SQL 结果
Saving a SQL result that has multiple data being read from a Microsoft Access database
我正在尝试发送一封包含来自 SQL 语句的多个数据的电子邮件,但我无法将 SQL 语句的结果保存在变量中。
str = "SELECT Test.*,TestTopicScore.[Easy Question Score],TestTopicScore.[Medium Question Score],TestTopicScore.[Hard Question Score] from Test INNER JOIN TestTopicScore on Test.TestID = TestTopicScore.TestID ORDER BY Test.Score DESC"
mycommand = New OleDbCommand(str, myconnection)
myReader = mycommand.ExecuteReader
myReader.Read()
emailcontents = myReader("emailcontents")
与访问数据库的连接正常,数据已读取,但我无法将其保存在变量中。
我得到的错误:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll
附加信息:电子邮件内容
让我们先看看您的查询,我对它进行了一些格式化,以便于查看发生了什么:
SELECT Test.*
,TestTopicScore.[Easy Question Score]
,TestTopicScore.[Medium Question Score]
,TestTopicScore.[Hard Question Score]
FROM Test
INNER JOIN TestTopicScore
ON Test.TestID = TestTopicScore.TestID
ORDER BY Test.Score DESC
这样做是选择几个不同的字段,而不是一个值。您的代码 emailcontents = myReader("emailcontents")
尝试读取名为 "emailcontents" 的 一个 字段。不幸的是,没有这样的命名字段。
比这更糟糕:您甚至不知道有多少字段 Test.*
将要 return。
因此,首先从 Test
.
枚举您想要的 每个 字段
然后,您似乎只需要一个字符串作为结果,因此您需要将字段连接为字符串,而不是 return 将它们分开。
在 Access 中,连接字符串的运算符是 &
,它应该为您将数值转换为字符串,因此:
SELECT Test.SomeField & Chr(13) & Chr(10)
& Test.SomeOtherField
& Test.AnotherField
& TestTopicScore.[Easy Question Score]
& TestTopicScore.[Medium Question Score]
& TestTopicScore.[Hard Question Score]
AS [emailcontents]
FROM Test
INNER JOIN TestTopicScore
ON Test.TestID = TestTopicScore.TestID
ORDER BY Test.Score DESC
请注意我是如何将 & Chr(13) & Chr(10)
放在 Test.SomeField
和 Test.SomeOtherField
之间的:这会在两者之间换行。您可能希望在所有字段之间执行此操作。
此外,我用 AS
为连接的数据命名。
现在我们不得不将输出的格式设置到数据库中,这让我们陷入了非常混乱的境地。你不想那样做。让我强调一下,让它脱颖而出:更好的方法是单独读取 VB.NET 代码中的所有字段,然后将它们格式化为字符串。
ETA:不仅如此,而且看起来您可能 return 不止一组结果(通过查看 ORDER BY
),在这种情况下,您将不得不循环读取所有结果。
我正在尝试发送一封包含来自 SQL 语句的多个数据的电子邮件,但我无法将 SQL 语句的结果保存在变量中。
str = "SELECT Test.*,TestTopicScore.[Easy Question Score],TestTopicScore.[Medium Question Score],TestTopicScore.[Hard Question Score] from Test INNER JOIN TestTopicScore on Test.TestID = TestTopicScore.TestID ORDER BY Test.Score DESC"
mycommand = New OleDbCommand(str, myconnection)
myReader = mycommand.ExecuteReader
myReader.Read()
emailcontents = myReader("emailcontents")
与访问数据库的连接正常,数据已读取,但我无法将其保存在变量中。
我得到的错误:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in System.Data.dll
附加信息:电子邮件内容
让我们先看看您的查询,我对它进行了一些格式化,以便于查看发生了什么:
SELECT Test.*
,TestTopicScore.[Easy Question Score]
,TestTopicScore.[Medium Question Score]
,TestTopicScore.[Hard Question Score]
FROM Test
INNER JOIN TestTopicScore
ON Test.TestID = TestTopicScore.TestID
ORDER BY Test.Score DESC
这样做是选择几个不同的字段,而不是一个值。您的代码 emailcontents = myReader("emailcontents")
尝试读取名为 "emailcontents" 的 一个 字段。不幸的是,没有这样的命名字段。
比这更糟糕:您甚至不知道有多少字段 Test.*
将要 return。
因此,首先从 Test
.
然后,您似乎只需要一个字符串作为结果,因此您需要将字段连接为字符串,而不是 return 将它们分开。
在 Access 中,连接字符串的运算符是 &
,它应该为您将数值转换为字符串,因此:
SELECT Test.SomeField & Chr(13) & Chr(10)
& Test.SomeOtherField
& Test.AnotherField
& TestTopicScore.[Easy Question Score]
& TestTopicScore.[Medium Question Score]
& TestTopicScore.[Hard Question Score]
AS [emailcontents]
FROM Test
INNER JOIN TestTopicScore
ON Test.TestID = TestTopicScore.TestID
ORDER BY Test.Score DESC
请注意我是如何将 & Chr(13) & Chr(10)
放在 Test.SomeField
和 Test.SomeOtherField
之间的:这会在两者之间换行。您可能希望在所有字段之间执行此操作。
此外,我用 AS
为连接的数据命名。
现在我们不得不将输出的格式设置到数据库中,这让我们陷入了非常混乱的境地。你不想那样做。让我强调一下,让它脱颖而出:更好的方法是单独读取 VB.NET 代码中的所有字段,然后将它们格式化为字符串。
ETA:不仅如此,而且看起来您可能 return 不止一组结果(通过查看 ORDER BY
),在这种情况下,您将不得不循环读取所有结果。