我已经尝试了一段时间了, 所以在我的MS访问数据库中我有一个
Table named "reservations" and inside the table are
Name(sid,jen)
Roomtype(single,double)
Arrivaldate(3/20/17, 3/20/17 )
Departuredate(3/21/17, 3/21/17)
在我的视觉工作室形式中我有
2 labels = lblsingle , lbldouble
1 button named btnok and
Datetimepicker named Datetimepicker1 (properties format set to "Short")
所以这是我的代码:
Private sub Form1_Load
Dim time as DateTime = DateTime.Now
Dim format As String = "MM/d/yyyy"
Datetimepicker1.Text = time.ToString(format)
Private sub btnok_click
Con.open
Dim cmb As new OleDbCommand("SELECT COUNT (*) FROM [reservations] WHERE [Roomtype] = 'Single' AND [Arrivaldate] = " & Datetimepicker1.Text & " ",Con)
Dim dr As OleDbDataReader = cmb.ExecuteReader
Dim userfound As Boolean = False
While dr.Read
userfound = True
lblsingle.text = (dr(0).ToString())
End While
Con.Close()
End Sub
End Class
。 所以我想要发生的是当我在datetimepicker1中选择日期3/20/17时。我的lblsingle.text应该计入" 1"因为在我的数据库中有一个与datetkmepicker1具有相同日期的单个数据库。但结果是" 0" .........我真的觉得有些东西让我的datetimepicker1和我的ms中的日期访问不同, 请帮助....我需要在某处更改时间格式吗?
答案 0 :(得分:0)
首先,您的“Private sub btnok_click”将无法处理按钮点击。
要处理“按钮点击”,您的实现应如下所示(proto):
Private Sub button1_Click(sender As Object, e As System.EventArgs) Handles button1.Click
..
..
..
..
End Sub.
来到业务逻辑:
如果要更新/增加标签?
根据您问题中的描述,您希望在“DatTimePicker”值更改后立即更新/增加标签。
但是在代码中,您正尝试在“确定”按钮单击中执行此操作。
如果您想更新标签,当您从“datetimepicker”中选择日期时,请处理以下事件(proto):
Private Sub DateTimePicker1_ValueChanged(sender as Object, e as EventArgs) _
Handles DateTimePicker1.ValueChanged
..
..
..
..
End Sub
答案 1 :(得分:0)
您必须将日期作为日期时间处理,而不是文本:
Dim time as DateTime = DateTime.Today
Dim format As String = "yyyy'/'MM'/'/dd"
Datetimepicker1.Value = time
Private sub btnok_click
Con.open
Dim cmb As new OleDbCommand("SELECT COUNT (*) FROM [reservations] WHERE [Roomtype] = 'Single' AND [Arrivaldate] = #" & time.ToString(format) & "# ",Con)
答案 2 :(得分:0)
不使用日期和时间的文字表示形式 - 将DateTime用于除用户的渲染输出之外的所有目的(或序列化为CSV文件等文本输出)。
处理控件的DateTime值
使用.Value属性来获取和设置控件值:
Datetimepicker1.Value = DateTime.Now
处理数据库查询的DateTime值
使用(typed)参数将DateTime值传递给数据库查询:
Dim cmb As new OleDbCommand("SELECT COUNT (*) FROM [reservations] WHERE [Roomtype] = 'Single' AND [Arrivaldate] = ?", Con)
cmb.Parameters.Add(Datetimepicker1.Value)
OleDbCommand使用位置参数 - 这意味着您需要在添加参数时匹配占位符的顺序:
Dim roomtype As String
roomtype = "Single"
Dim cmb As new OleDbCommand("SELECT COUNT (*) FROM [reservations] WHERE [Roomtype] = ? AND [Arrivaldate] = ?", Con)
cmb.Parameters.Add(roomtype)
cmb.Parameters.Add(Datetimepicker1.Value)
简化检索标量结果,如count(*)
如果您尝试获取的只是count(*)
,则不需要OleDbDataReader - 让您的命令ExecuteScalar
lblsingle.Text = cmb.ExecuteScalar()