我将检索到的值返回到变量中但是在检查行是否存在之后。但if条件失败,因为表中当前没有记录。
Dim sqlstr as string
Dim da As SqlClient.SqlDataAdapter
sqlstr = "select max(mat_req_no) as mat_req_no from pos_mrq_hdr"
If dt.Rows.Count > 0 Then
ltino = dt.Rows(0)("mat_req_no").tostring
End If
the if dt.rows.count > 0
答案 0 :(得分:1)
你可能会忘记代码中的一些语句,但是当我在Max
中进行测试时,它将始终返回(1)行数,因此我们应该测试返回值,如果为NULL或NOT
完整的代码将是
Imports System.IO
Imports System.Data.SqlClient
Public Class Form1
Dim cnn As SqlConnection
Dim connectionString As String
Dim sqlAdp As SqlDataAdapter
Dim ds As New DataSet
Dim dt As New DataSet
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
connectionString = "Data Source=servername; Initial Catalog=databasename; User ID=userid; Password=password"
cnn = New SqlConnection(connectionString)
cnn.Open()
sqlAdp = New SqlDataAdapter("select max(mat_req_no) as mat_req_no from pos_mrq_hdr", cnn)
cnn.Close() 'connection close here , that is disconnected from data source
sqlAdp.Fill(ds)
sqlAdp.Fill(dt)
'fetching data from dataset in disconnected mode
' MsgBox(ds.Tables(0).Rows.Count)
If IsDBNull(ds.Tables(0).Rows(0).Item(0)) Then
' MsgBox("no")
Else
Dim ltino = ds.Tables(0).Rows(0)("mat_req_no").ToString
' MsgBox(ltino)
End If
End Sub
End Class