1

我想获取 的行数DataSet,但这里的代码抛出异常

Dim con As New SqlConnection("server=localhost;database=MSDSS;Integrated Security=True")
Dim i As Integer
con.Open()
Dim qstr As String = "Select * From SJournal"
Dim sqladp As New SqlDataAdapter(qstr, con)
Dim dt As New DataSet

sqladp.Fill(dt, "SJ")
Dim j As Integer = dt.Tables("SJ").Rows.Count
For i = 0 To j Step 1
    Dim rowname As String = dt.Tables("SJ").Rows(i)("JournalName")
Next

这是我计算字符串相似度的方法

Dim Kq As Double = 2
Dim Kr As Double = 1
Dim Ks As Double = 1
Dim ss() As Char = rowname.ToCharArray()
Dim st() As Char = journalname.ToCharArray()
Dim q As Integer = ss.Intersect(st).Count()
Dim s As Integer = ss.Length - q
Dim r As Integer = st.Length - q
Dim total As Double = (Kq * q) / (Kq * q + Kr * r + Ks * s)

请给我解决方案

4

3 回答 3

1

Datatable像数组一样工作(在某种程度上,当你在 for 上使用它时),数组的索引编号从 0 到 N- 1

考虑到 N 是行数,你应该修改这个

Dim j As Integer = dt.Tables("SJ").Rows.Count

Dim j As Integer = dt.Tables("SJ").Rows.Count -1

因此,如果得到 3 rows,for 循环将从 0 变为 2

于 2012-09-04T03:48:42.617 回答
0

我希望你的 for 循环

For i = 0 To j Step 1
    Dim rowname As String = dt.Tables("SJ").Rows(i)("JournalName")
Next

应该有 j-1 作为上限,即。

For i = 0 To j-1 Step 1
    Dim rowname As String = dt.Tables("SJ").Rows(i)("JournalName")
Next
于 2012-09-04T03:27:40.530 回答
0

如果您使用数据适配器,则无需显式打开连接。

更多参考请访问: http ://forums.asp.net/t/1663393.aspx/1

于 2012-09-04T06:13:14.580 回答