如何在保存按钮时将此代码与超时和超时冲突?
示例在ms访问中有数据存储,例如7:00-8:00
,然后我将保存另一个数据存储,例如7:30-8:30
,它一定是冲突的,但是在我的程序中它将被保存。请我帮忙。
Dim scmd As New OledbCommand
with scmd
.connection = cn
.commandtext = "select * from deptsched where. [dtimein] <= #" & combo1.text & "# and [dtimeout] >= #" & combo2.text "# "
end with
Dim srdr As OleDbDataReader
srdr = scmd.ExecuteReader
If srdr.HasRows Then
msg("conflict")
exit sub
end if
with cmds
.connection = cn
.commandtext = insert into deptsched values('"& c1.text. &"', '"& c2.text &"')"
.executenonquery()
end with
msg(" save")
答案 0 :(得分:-1)
说实话,您的Select语句对我来说意义不大,但这是您可以用来执行sql语句的代码。至少,这将为您提供遵循的模式。您将必须检查数据库中的数据类型并相应地调整代码。
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
Dim RecordCount As Integer
'Keep your data objects local so you can be sure they are closed and disposed.
' A Using...End Using block ensures this even if there is an error
Using cn As New OleDbConnection("Your Connection String")
'Pass the command text and the connection directly to the constructor of the command
'Always use Parameters to avoid Sql injection
'If all you need is a count of records then ask for Count no all the data
Using scmd As New OleDbCommand("Select Count(*) From deptsched where [dtimein] <= @TimeIn And [dtimeout] >= @TimeOut;", cn) ' #" & combo1.text & "# and#" & combo2.text "# ")
scmd.Parameters.Add("@TimeIn", OleDbType.Date).Value = CDate(combo1.Text)
scmd.Parameters.Add("@TimeOut", OleDbType.Date).Value = CDate(combo2.Text)
cn.Open()
RecordCount = CInt(scmd.ExecuteScalar)
End Using
End Using
If RecordCount > 0 Then
MessageBox.Show("conflict")
Exit Sub
End If
Using cn As New OleDbConnection("Your Connection String")
Using cmds As New OleDbCommand("insert into deptsched values(@Field1, @Field2);", cn)
cmds.Parameters.Add("@Field1", OleDbType.VarChar).Value = c1.text
cmds.Parameters.Add("@Field2", OleDbType.VarChar).Value = c2.text
cn.Open()
cmds.ExecuteNonQuery()
End Using
End Using
MessageBox.Show("Save")
End Sub