我正在尝试从表中获取数据并将其插入到数组中。但是,当我调试我的代码时,Visual Studio会返回此错误:
Microsoft.VisualBasic.dll中出现未处理的“System.MissingMemberException”类型异常
其他信息:找不到类型为“DBNull”的默认成员。
运行并导致此错误的过程如下:
Sub characterChecked(ByVal pronunciation As String)
Dim query As String = "SELECT Character FROM [Hiragana List] WHERE Pronunciation='" & pronunciation & "';" 'Selects the character from the SQL table
Dim instruction = New SqlCommand(query, sqlCon)
Dim da As New SqlDataAdapter
da.SelectCommand = instruction
HiraganaList = New DataTable
da.Fill(HiraganaList)
dataArray(columnCount, rowCount) = HiraganaList.NewRow(0)("column") 'fills the first column in a row with the charater
columnCount += 1 'Increments the coulmn count so that the pronunciation is in the same row as the character
Dim query2 As String = "SELECT Pronunciation FROM [Hiragana List] WHERE Pronunciation='" & pronunciation & "';"
Dim instruction2 = New SqlCommand(query, sqlCon)
Dim da2 As New SqlDataAdapter
da2.SelectCommand = instruction
HiraganaList = New DataTable
da2.Fill(HiraganaList)
dataArray(columnCount, rowCount) = HiraganaList.NewRow(0)("row")
rowCount += 1
调试在这一行中断:
dataArray(columnCount, rowCount) = HiraganaList.NewRow(0)("column") 'fills the first column in a row with the charater
数组,数据表等的声明在这里:
Dim connectionString As String = "Server=my_server;Database=name_of_db;User Id=user_name;Password=my_password"
Dim sqlCon = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename='J:\Computing Coursework\real project\KES\KES\Kana List.mdf';Integrated Security=True;Connect Timeout=30")
Dim HiraganaList As DataTable
Dim KanjiList As DataTable
Dim Katakana As DataTable
Dim YoonList As DataTable
Dim YoonKataList As DataTable
Dim columnCount As Integer = 1 'initialising the column and row counter for when data is inserted
Dim rowCount As Integer = 1
Dim varFileName As String = "J:\Computing Coursework\real project\KES\Resources\csv.txt"
Dim dataArray(1, 1) As String
如果有人能在我的代码中帮助发现问题,那将非常有帮助。就个人而言,我认为错误是由于没有以正确的方式将数据插入到数组中引起的,但是我不知道我在做什么是不正确的还是确实有效。
答案 0 :(得分:0)
你要做的事情并不是很清楚,但NewRow电话似乎完全错了。它不仅创建了一个最初为空的新行,而且似乎没有接受参数的重载 所以也许这就是你想要做的。 (不测试数组中可用的空间来复制返回的值)
Sub characterChecked(ByVal pronunciation As String)
Dim query As String = "SELECT Character FROM [Hiragana List] WHERE Pronunciation=@pro"
Dim instruction = New SqlCommand(query, sqlCon)
instruction.Parameters.AddWithValue("@pro", pronunciation)
Dim da As New SqlDataAdapter(instruction)
HiraganaList = New DataTable
da.Fill(HiraganaList)
if HiraganaList.Rows.Count > 0 Then
dataArray(0, rowCount) = HiraganaList.Rows(0)("Character")
dataArray(1, rowCount) = pronunciation
End If
End Sub
但在这一点上,我怀疑你是否真的需要它
DataTable
已经是一个美化的数组,具有比简单数组更多的功能。
所以只需要调用
Function characterChecked(ByVal pronunciation As String) As DataTable
Dim query As String = "SELECT Pronunciation, Character " & _
"FROM [Hiragana List] WHERE Pronunciation=@pro"
Dim instruction = New SqlCommand(query, sqlCon)
instruction.Parameters.AddWithValue("@pro", pronunciation)
Dim da As New SqlDataAdapter(instruction)
HiraganaList = New DataTable
da.Fill(HiraganaList)
return HiraganaList
End Function
将为您提供一个DataTable,其中包含查询返回的多行和两列
(索引0处为Pronunciation
,索引处为Character
)
Dim hira As DataTable = characterChecked(pronciation.Text)
Dim hiraChar = hira.Rows(0)("Character")