从SQL Server中获取数据行给定该行中某列的一个单元格的值 - Visual Basic

时间:2016-05-19 15:10:27

标签: sql-server vb.net visual-studio

我检查了一个值是否存在

Dim connectionString = [connection string ]

Using exist As New SqlConnection(connectionString)
    Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM Employees WHERE WorkEmail = @WorkEmail", exist)
    cmd.Parameters.AddWithValue("@WorkEmail", DataObjects.Contacts.ElectronicAddress.Email)

    If cmd.ExecuteScalar > 0 Then
        //return row so that I can grab values from it, given column names

我如何在最后一行做这个评论部分?

1 个答案:

答案 0 :(得分:0)

您应该执行命令,然后检查它是否返回任何行。

请参阅Retrieving Data Using a Data Reader

为您的用例略微调整的摘录:

Private Sub HasRows(ByVal connection As SqlConnection)
    Using connection
        Using cmd As SqlCommand = New SqlCommand("SELECT * FROM Employees WHERE WorkEmail = @WorkEmail", connection)
            cmd.Parameters.AddWithValue("@WorkEmail", DataObjects.Contacts.ElectronicAddress.Email)
            connection.Open()

            Using reader As SqlDataReader = cmd.ExecuteReader()
                While reader.Read()
                    REM Your code to pull the data you want from the returned data goes here
                End While
            End Using
        End Using
    End Using
End Sub

将来尝试查看Microsoft文档。你通常会在那里找到你需要的东西。