数据集计数行

时间:2012-09-04 03:22:18

标签: asp.net vb.net count dataset row

我想获取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)

请给我解决方案

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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

答案 2 :(得分:0)

如果您使用的是dataadapter,则无需显式打开连接。

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