1

我将检索到的值返回到变量中,但在检查行是否存在之后。但是 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 
4

1 回答 1

1

您可能会忘记代码中的某些语句,但是当我在其中进行测试时,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
于 2013-11-06T05:31:30.683 回答