我有一个按钮点击运行的查询
我想设置它,如果系统日期=" DateColumn"更新数据库。
其他
插入。
Dim provider As String
Dim dataFile As String = My.Application.Info.DirectoryPath
provider = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source ="
dataFile = ("Database\Base-Original.accdb")
Using myconnection As New OleDbConnection(provider & dataFile)
myconnection.Open()
Dim sqlQry1 As String = "INSERT INTO [Table] ([Column1], [Column2], [Column3], [Column4], [Date]) VALUES (@1, @2, @3, @4, @5)"
Using cmd As New OleDbCommand(sqlQry1, myconnection)
cmd.Parameters.AddWithValue("@1", Combobox1.SelectedItem.ToString)
cmd.Parameters.AddWithValue("@2", textbox1.Text)
cmd.Parameters.AddWithValue("@3", Textbox2.Text)
cmd.Parameters.AddWithValue("@4", Combobox2.SelectedItem.ToString)
cmd.Parameters.AddWithValue("@5", Date.Now.ToShortDateString)
If cmd.ExecuteNonQuery() Then
myconnection.Close()
MsgBox("successfull. ", MsgBoxStyle.Information, "Record Saved")
Else
MsgBox("failed. ", MsgBoxStyle.Critical, "Registration failed")
Return
End If
End Using
End Using
我有INSERT INTO,但我不知道如何更新数据库而不是插入新记录...我知道如何更新,但我想要有一列记录名称(Column1)每天一次。
我是否需要使用阅读器检查日期栏? 或者还有另一种方法......我只需指向正确的方向 非常感谢..
答案 0 :(得分:0)
此处的代码将在数据库中搜索日期值等于今天日期的记录(请参阅下面的重要说明)。如果找到一个,则更新值字段并将其保存回数据库。
Private Sub SOExample2()
Using myconnection As New OleDbConnection(provider & dataFile)
myconnection.Open()
Dim TodaysRecordSQL As String = String.Format("SELECT [Column1], [Column2], [Column3], [Column4], [Date] FROM [Table] WHERE [Date] = '{0:o}' ORDER BY [Date] DESC", Date.Today.Date)
Dim UpdateDS As New DataSet
Using UpdateDA As New OleDb.OleDbDataAdapter(TodaysRecordSQL, myconnection)
UpdateDA.Fill(UpdateDS)
End Using
Dim UpdateTable As DataTable = UpdateDS.Tables("Table")
If UpdateTable IsNot Nothing AndAlso UpdateTable.Rows.Count = 1 Then
' Update existing record
Dim UpdateRow As DataRow = UpdateTable.Rows(0)
UpdateRow("Column1") = "NewValue1"
UpdateRow("Column2") = "NewValue2"
Using UpdateDA As New OleDb.OleDbDataAdapter()
Using UpdateCmd As New OleDb.OleDbCommand()
UpdateCmd.Connection = myconnection
UpdateCmd.CommandText = "UPDATE [Table] SET [Column1] = @1, [Column2] = @2, [Column3] = @3, [Column4] = @4 WHERE [Date] = @5"
Dim UpdateParam As OleDb.OleDbParameter = New OleDb.OleDbParameter()
UpdateParam.ParameterName = "@1"
UpdateParam.SourceColumn = "Column1"
UpdateCmd.Parameters.Add(UpdateParam)
UpdateParam = New OleDb.OleDbParameter()
UpdateParam.ParameterName = "@2"
UpdateParam.SourceColumn = "Column2"
UpdateCmd.Parameters.Add(UpdateParam)
UpdateParam = New OleDb.OleDbParameter()
UpdateParam.ParameterName = "@3"
UpdateParam.SourceColumn = "Column3"
UpdateCmd.Parameters.Add(UpdateParam)
UpdateParam = New OleDb.OleDbParameter()
UpdateParam.ParameterName = "@4"
UpdateParam.SourceColumn = "Column4"
UpdateCmd.Parameters.Add(UpdateParam)
UpdateParam = New OleDb.OleDbParameter()
UpdateParam.ParameterName = "@5"
UpdateParam.SourceColumn = "Date"
UpdateCmd.Parameters.Add(UpdateParam)
UpdateDA.UpdateCommand = UpdateCmd
UpdateDA.Update(UpdateDS) ' This does the actual updating of the modified rows in the DataSet
End Using
End Using
Else
' Insert new record
End If
End Using
End Sub
注意:此代码假设您将记录保存到数据库中,时间戳为00:00小时(即午夜)。如果保存的记录的时间为14:12(下午2:12),那么找到“今天的”记录会比较困难,因为您不知道“今天”条目的时间部分可能是什么