0

谢谢我使用了 MAX 语句,它没有返回错误,但仍然不明白为什么它不起作用。我的代码如下所示:

Protected Sub txtuserID_TextChanged(sender As Object, e As System.EventArgs) Handles txtuserID.TextChanged
    Dim strConnection As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=E:\LostPropertyProject\App_Data\LostPropertyDatabase.mdf;Integrated Security=True;User Instance=True"
    'Establish SQL Connection
    Dim con As New SqlConnection(strConnection)
    'Open database connection to connect to SQL Server
    con.Open()
    'Data table is used to bind the resultant data
    Dim dtusers As New DataTable()
    'Create a new data adapter based on the specified query.
    Dim comm As New SqlCommand
    comm.CommandText = "SELECT MAX(UserID) FROM tblUser"
    comm.Connection = con

    Dim MaxUserID As Object = comm.ExecuteScalar()
    con.Close()
End Sub

另外,我希望用户ID 在页面加载后立即显示在文本框中。我该怎么做呢?并感谢所有回答我问题的人:) 非常感谢

4

1 回答 1

0
SELECT MAX(UserId) FROM User

这将为您提供表中的最新用户 ID。

于 2013-03-08T18:13:26.067 回答