C# 获取错误为“输入数组长于此表中的列数”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2350634/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting Error as "input array is longer than the number of columns in this table"
提问by Gopal
Code.
代码。
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
While loading the page, the error was thrown as "input array is longer than the number of columns in this table"
加载页面时,抛出错误“输入数组比此表中的列数长”
What wrong in my code.
我的代码有什么问题。
Need Help
需要帮忙
采纳答案by Darin Dimitrov
You need to add columns to this data table first:
您需要先向此数据表添加列:
Dim dt As New DataTable
dt.Columns.Add("RName", GetType(String))
Also I don't know much about the con
, cmd
and dr2
variables in your code but I would strongly recommend you to dispose them properly:
此外,我对代码中的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