这是我第一次在这里提出问题,但我对此完全感到困惑。我目前正在制作一个小型调度应用程序作为辅助项目,但在运行程序后我一直遇到这个错误。错误是我对数据库进行了一些更改,例如添加列 Day_Night_Shift 但在运行程序后有时会删除该行。就像它恢复到旧数据库一样。我目前正在使用带有 accdb 文件的访问数据库。
到目前为止,我在程序中做了一些插入和选择语句。第一个是 select 语句,它转到这个函数来填充一个 dgv 框。
Public Function executeQuery(ByVal querystr As String) As DataTable
'opens accdb database file and fills datatable with wanted query. returns datatable for use.
Dim dt As New DataTable
Using da As New OleDb.OleDbDataAdapter(querystr, connstr)
da.Fill(dt)
da.Dispose()
End Using
Return dt
End Function
我使用的另一个功能是插入
Public Function InsertNewEmp(ByVal firstname As String, ByVal lastname As String, ByVal position As String, ByVal shift As String) As String
Dim insertstring As String = "INSERT INTO Tbl_Employee ([Employee First Name], [Employee Last Name], [Position ID], [Day or night]) VALUES ('" & _
firstname & "', '" & lastname & "', " & position & ", '" & shift & "') "
Try
cnnOLEDB = New OleDbConnection(connstr)
cnnOLEDB.Open()
cmdOLEDB = New OleDbCommand(insertstring, cnnOLEDB)
cmdOLEDB.ExecuteNonQuery()
cnnOLEDB.Close()
Catch ex As Exception
Return "insert failed," & ex.Message
End Try
Dim data As DataTable = executeQuery("SELECT [Employee ID] from Tbl_Employee where [Employee First Name] = '" & firstname & _
"' and [Employee Last Name] = '" & lastname & "' and [Position ID] = " & position & _
" and [Day or Night] = '" & shift & "'")
Dim successmsg As String
Dim rows() As DataRow = data.Select()
Dim lastrow As Integer = rows.GetUpperBound(0)
successmsg = (rows(rows.GetUpperBound(0))(0))
Return "insert successful Employee id=" & successmsg
如果有人能够对此提供帮助,将不胜感激。它不会一直重置数据库,但是当我尝试调试等时它有点烦人。
先感谢您