如何刷新插入和选择之间的数据库

时间:2019-02-02 15:14:29

标签: sql vb.net refresh oledb

我正在尝试从刚创建的数据库中的记录中获取数据,但是从中找不到任何数据。

我尝试使用不同的连接,希望它能够“查看”新数据,但这不起作用,我被卡住了!

Using oCmd As New OleDbCommand("Insert Into Users (FirstName, LastName, Username, `Password`, Teacher) Values (@firstName, @lastName, @username, @password, 0)", myConnection)
   oCmd.Parameters.Add("@firstName", OleDbType.VarChar, 255).Value = txtFirstName.Text
   oCmd.Parameters.Add("@lastNamee", OleDbType.VarChar, 255).Value = txtLastName.Text
   oCmd.Parameters.Add("@username", OleDbType.VarChar, 255).Value = txtUsername.Text
   oCmd.Parameters.Add("@password", OleDbType.VarChar, 255).Value = txtPassword.Text
   oCmd.ExecuteNonQuery()
End Using
Dim userID As String = ""
Using ocmd As New OleDbCommand("Select * From Users Where FirstName = @firstName And LastName = @lastName", myConnection)
   ocmd.Parameters.Add("@firstName", OleDbType.Char, 255).Value = txtFirstName.Text
   ocmd.Parameters.Add("@lastName", OleDbType.Char, 255).Value = txtLastName.Text
   Dim dataReader As OleDbDataReader = ocmd.ExecuteReader()
   While dataReader.Read
      userID = dr("UserID")
   End While

显示的错误是

  

System.InvalidOperationException:'该行/列没有数据。'

我要做的就是获取已创建记录的用户ID。任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:1)

获取最后输入的ID。

Dim cmd1 As New OleDbCommand("Select @@IDENTITY", myConnection)
Dim itgID As Integer = CInt(cmd1.ExecuteScalar)