代码。
Public Function comb1(ByVal SName As String) As DataTable
Dim dt As New DataTable
cmd = New SqlCommand("Select Distinct RName from tb_RS_New", con)
dr2 = cmd.ExecuteReader
While (dr2.Read())
dt.Rows.Add(dr2("RName"))
End While
Return dt
End Function
在加载页面时,错误被抛出为“输入数组长于此表中的列数”
我的代码有什么问题。
需要帮助
答案 0 :(得分:13)
您需要先在此数据表中添加列:
Dim dt As New DataTable
dt.Columns.Add("RName", GetType(String))
此外,我对您的代码中的con
,cmd
和dr2
变量了解不多,但我强烈建议您妥善处理它们:
Dim dt As New DataTable
dt.Columns.Add("RName", GetType(String))
Using con As New SqlConnection("connection string to the database")
Using cmd = con.CreateCommand()
con.Open()
cmd.CommandText = "Select Distinct RName from tb_RS_New"
Using dr = cmd.ExecuteReader()
While (dr.Read())
dt.Rows.Add(dr("RName"))
End While
End Using
End Using
End Using