我使用此函数填充组合框,它在sql server中工作,但在sql server express中不工作,返回错误: Conversion from type "DBNUll" to type 'String' is not valid
获取sql示例的代码
SqlDataSourceEnumerator.Instance.GetDataSources()
但当手动放置示例时,它正在工作。
我需要vb.net中的代码来调用 get SqlDataSourceinstance
没有来自任何sql server的错误(express或…)
Public Sub searchServer(ByVal cbx As combobox)
Dim dt As DataTable = Nothing, dr As DataRow = Nothing
Try
'get sql server instances in to DataTable object
dt = SqlDataSourceEnumerator.Instance.GetDataSources()
'load data in to ComboBox
If dt.HasErrors Then
MessageBox("has Error", "Error!")
End If
If dt.Rows.Count = 0 Then
cbx.DataSource = Nothing
Else
Dim comboSource As New Dictionary(Of String, String)()
comboSource.Add("0", "Local")
For Each dr In dt.Rows
If comp = False Then
comboSource.Add(CType(dr.Item(0), String) & "\" & CType(dr.Item(1), String), CType(dr.Item(1), String))
Else
comboSource.Add(CType(dr.Item(0), String) & "\" & CType(dr.Item(1), String), CType(dr.Item(0), String) & "\" & CType(dr.Item(1), String))
End If
Next
cbx.DataSource = New BindingSource(comboSource, Nothing)
cbx.DisplayMember = "Value"
cbx.ValueMember = "Key"
End If
Catch ex As SqlException
MessageBox(ex.Message, "Error!")
Catch ex As Exception
MessageBox(ex.Message, "Error!")
End Try
End Sub
1条答案
按热度按时间bqucvtff1#
把这个班
并添加此方法