获取标量数据

这个简单的函数可用于从一个字段中获取一个记录查询结果的值

Public Function getDataScalar(ssql As String)
    openConnection()

    Try
        Dim q As New MySqlCommand

        q.Connection = db
        q.CommandText = ssql
        getDataScalar = q.ExecuteScalar

    Catch ex As Exception
        'Exception
    End Try
End Function

如何使用它:

Dim userid as String = getDataScalar("select username from user where userid=99")

作为该查询的结果,变量’username’将填充字段 username 的值。