Sql 参数匹配参数';文本';无法从';DBNull';至';字符串';
正在尝试从Access数据库文件导入记录并在listview中显示这些记录(在Visual Basic中)。它在50%的时间内都能正常工作,但似乎在说:Sql 参数匹配参数';文本';无法从';DBNull';至';字符串';,sql,vb.net,ms-access,Sql,Vb.net,Ms Access,正在尝试从Access数据库文件导入记录并在listview中显示这些记录(在Visual Basic中)。它在50%的时间内都能正常工作,但似乎在说: An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in Microsoft.VisualBasic.dll Additional information: Overload resolution failed
An unhandled exception of type 'System.Reflection.AmbiguousMatchException' occurred in Microsoft.VisualBasic.dll
Additional information: Overload resolution failed because no Public 'Add' can be called with these arguments:
'Public Overrides Function Add(text As String) As System.Windows.Forms.ListViewItem':
Argument matching parameter 'text' cannot convert from 'DBNull' to 'String'.
'Public Overrides Function Add(value As System.Windows.Forms.ListViewItem) As System.Windows.Forms.ListViewItem':
Argument matching parameter 'value' cannot convert from 'DBNull' to 'ListViewItem'.
有关守则如下:
ds.Clear()
LVResults.Items.Clear()
con.ConnectionString = dbProvider & dbSource
con.Open() 'Open connection to the database
sqlstatement = "SELECT * FROM records WHERE checkoutdate is NULL"
da = New OleDb.OleDbDataAdapter(sqlstatement, con)
da.Fill(ds, "allmembers") 'Fill the data adapter
con.Close()
Dim recordCount, x As Short
recordCount = 0
x = 0
recordCount = ds.Tables("allmembers").Rows.Count
With ds.Tables("allmembers")
Do Until x = recordCount
LVResults.Items.Add(.Rows(x).Item(0))
LVResults.Items(x).SubItems.Add(.Rows(x).Item(1))
LVResults.Items(x).SubItems.Add(.Rows(x).Item(2))
LVResults.Items(x).SubItems.Add(.Rows(x).Item(3))
LVResults.Items(x).SubItems.Add(.Rows(x).Item(4))
x = x + 1
Loop
End With
了解我,这是非常明显的事情,但非常感谢您的帮助:)
最令人恼火的是,它有时会工作,但另一些时候会抛出错误。老实说,错误消息是不言自明的——有些字段包含空值,VB不会自动将其转换为空字符串或零。一种解决方案是编辑SQL语句以显式避免返回null:
SELECT IIf(Surname Is Null, '', Surname), IIf(Forename Is Null, '', Forename),
IIf(SomeIntField Is Null, 0, SomeIntField) FROM records WHERE CheckoutDate Is Null
老实说,错误消息是不言自明的——有些字段包含空值,VB不会自动将其转换为空字符串或零。一种解决方案是编辑SQL语句以显式避免返回null:
SELECT IIf(Surname Is Null, '', Surname), IIf(Forename Is Null, '', Forename),
IIf(SomeIntField Is Null, 0, SomeIntField) FROM records WHERE CheckoutDate Is Null
顺便说一句,它导入的数据是整数和字符串。它导入的数据是整数和字符串。但是access数据库中没有一个字段是空的,唯一的空字段是checkoutdate。@user2366605-您没有返回checkoutdate,但是,您正在筛选它不为空的位置。您如何确定其他列都没有空值?@user2366605-另外,您是说您已经尝试了我建议的方法,但仍然得到了错误?但是access数据库中没有一个字段是空的,唯一的空字段是checkoutdate。@user2366605-但是您没有返回checkoutdate,您正在筛选它不为null的位置。您如何确定其他列都没有空值?@user2366605-另外,您是说您已经尝试了我建议的方法,但仍然得到了错误?