如何将数据库中的最后一个userID放入asp.net的文本框中

时间:2013-03-08 18:03:32

标签: sql-server

谢谢我使用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

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

1 个答案:

答案 0 :(得分:0)

SELECT MAX(UserId) FROM User

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