0

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

  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
4

1 回答 1

0

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

即使您知道这种情况,您也应该在这种情况下始终使用“Select top 1”语句,因为您永远不知道将来会发生什么。

于 2013-06-27T08:05:23.333 回答