VB从SQL返回单个值

时间:2013-06-19 21:35:42

标签: sql vb.net

我正在尝试检索单个整数值。对于此选择,DB中始终只有一条记录。我想连接,检索商店并显示此值。这是执行标准的方法吗?

  Dim sqlquery As String
        Dim ConnectionString As String
        ConnectionString = "Server=" + ServerName + "\" + InstanceName + "; Database=" + DatabaseName + "; User Id=" + UserId + ";Password=" + Password + ";"
        sqlquery = "SELECT severity FROM from dbo.SettingsSub where SEB_SettingsID = 'Severity"

        'Connect
        Using conn As SqlConnection = New SqlConnection(ConnectionString)
            conn.Open()
            Using comm As SqlCommand = New SqlCommand(sqlquery, conn)
                CurrentSeverity = Convert.ToInt32(comm.ExecuteScalar())
                txtCurrentSeverity.Text = CurrentSeverity
                conn.Close()
            End Using 'comm

1 个答案:

答案 0 :(得分:0)

“此选择中,数据库中始终只有一条记录。”

即使你知道这种情况,你应该总是在这个场景中使用“选择前1”语句,因为你永远不知道将来会发生什么。