编写vb.net代码,使得在form7中textbox输入resid,boxid和time后,先在sql server的reservations表里查询有无记录与输入的boxid,time两者同时重复且resid不同的,若有则显示“该时段包厢已有人预约!”,若无则修改信息成功
时间: 2024-03-20 16:38:48 浏览: 58
以下是示例代码,实现了上述功能:
```vbnet
Dim connStr As String = "Data Source=yourServerName;Initial Catalog=yourDatabaseName;Integrated Security=True;"
Dim conn As SqlConnection = New SqlConnection(connStr)
Try
conn.Open()
Dim sql As String = "SELECT COUNT(*) FROM reservations WHERE boxid=@boxid AND time=@time AND resid<>@resid"
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@boxid", TextBox1.Text)
cmd.Parameters.AddWithValue("@time", TextBox2.Text)
cmd.Parameters.AddWithValue("@resid", TextBox3.Text)
Dim result As Integer = Convert.ToInt32(cmd.ExecuteScalar())
If result > 0 Then
MessageBox.Show("该时段包厢已有人预约!")
Else
sql = "UPDATE reservations SET boxid=@boxid, time=@time WHERE resid=@resid"
cmd = New SqlCommand(sql, conn)
cmd.Parameters.AddWithValue("@boxid", TextBox1.Text)
cmd.Parameters.AddWithValue("@time", TextBox2.Text)
cmd.Parameters.AddWithValue("@resid", TextBox3.Text)
cmd.ExecuteNonQuery()
MessageBox.Show("修改信息成功!")
End If
Catch ex As Exception
MessageBox.Show("发生错误:" & ex.Message)
Finally
conn.Close()
End Try
```
注意替换示例代码中的连接字符串和SQL语句,以适应你自己的数据库表结构。同时,还需要在窗体7的“确定”按钮的Click事件中添加以上代码。
阅读全文