您可能会忘记代码中的某些语句,但是当我在其中进行测试时,Max
它将始终返回 (1) 的行数,因此我们应该测试返回值是否为 NULL
完整的代码将是
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