请教 VB连SQLServer 日期类数据查询
现有数据库表结构及记录如下:stcd ymdhm dy
50100900 2001-1-1 2:00:00 13.78
50100900 2001-6-9 13:59:00 13.73
50100900 2001-6-9 14:00:00 13.73
50100900 2001-6-9 20:00:00 13.73
50100900 2001-6-10 2:00:00 13.73
50100900 2001-6-10 8:00:00 13.73
50100900 2001-6-10 14:00:00 13.78
50100900 2001-6-10 20:00:00 13.73
50100900 2001-6-11 2:00:00 13.78
50100900 2001-6-11 8:00:00 13.78
50100900 2001-6-11 14:00:00 13.73
50100900 2001-6-11 20:00:00 13.73
50100900 2001-6-12 2:00:00 13.73
50100900 2001-6-12 8:00:00 13.73
50100900 2001-6-12 14:00:00 13.73
50100900 2001-6-13 18:32:00 13.73
50100900 2001-6-13 20:00:00 13.73
50100900 2001-6-14 2:00:00 13.78
50100900 2001-6-14 8:00:00 13.73
50100900 2001-6-14 14:00:00 13.73
50100900 2001-6-14 17:49:00 13.73
50100900 2001-6-14 20:00:00 13.73
50100900 2001-6-15 2:00:00 13.73
50100900 2001-6-15 8:00:00 13.73
50100900 2001-6-15 14:00:00 13.78
50100900 2001-6-15 20:00:00 13.73
50100900 2001-6-16 2:00:00 13.73
50100900 2001-6-16 8:00:00 13.73
50100900 2001-6-16 20:00:00 13.73
50100900 2001-6-17 2:00:00 13.73
50100900 2001-6-17 10:56:00 13.78
50100900 2001-6-17 14:00:00 12.61
我在VB中使用一个comandbutton,两个DTPICKER和一个datagrid控件想实现一个查询某一日期段的数据查询,可总出现这样那样的问题
请教高手!
首先我使用这段代码实现了连库:
Private Sub Form_Load()
Dim CON As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim strConn As String
strConn = "Provider=SQLOLEDB.1;User ID=sa;password=cmq;Initial Catalog=rwdb2008;Data Source=127.0.0.1"
CON.Open strConn
RS.Open "select * from ST_DY_YS", CON, 3, 1
Set DataGrid1.DataSource = RS
End Sub '加载数据源
这样会将表中所有数据显示出来
可在commandbutton的click事件中的代码老是出毛病
请大家帮忙解答!
--------------------编程问答--------------------
老大,要贴你也得贴出毛病的代码啊
--------------------编程问答-------------------- commandbutton的click事件中的代码没有提供。我们怎么判断? --------------------编程问答-------------------- Rs.Open "select * from ST_DY_YS where ymdhm between " & DTPicker1.Value & " and " & DTPicker2.Value, CON, 3, 1 --------------------编程问答-------------------- Rs.Open "select * from ST_DY_YS where ymdhm between '" & DTPicker1.Value & "' and '" & DTPicker2.Value & "'", CON, 3, 3
试试看,不行的话把单引号再去掉 --------------------编程问答-------------------- 呵呵,各位大侠我修改了代码 现在已经可以查询 某一时间段的记录
代码如下:
Private Sub Command1_Click()
Dim CON As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim sqlstring As String
CON.ConnectionString = "Provider=SQLOLEDB.1;User ID=sa;password=cmq;Initial Catalog=rwdb2008;Data Source=127.0.0.1"
CON.ConnectionTimeout = 0
CON.Open
RS.CursorLocation = adUseClient
sqlstring = " select * into ls From st_dy_ys where ymdhm between '" & DTPicker1.Value & "' and '" & DTPicker2.Value & "'"
If Option1 = True Then
RS.Open sqlstring, CON, 3, 3
Set DataGrid1.DataSource = RS
i = RS.RecordCount
End If
Text1.Text = i
End Sub
--------------------编程问答-------------------- 但我要是想把这个查询结果作为一个中间结果 再进行下一步查询该怎么办呢?
我设置了一个option 按钮,本想在option的click事件中再编写代码进行设置,可后来才知道 option只要选中 option.value就为true
所以我想创建一个临时表
代码改后如下:
Private Sub Command1_Click()
Dim CON As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim sqlstring As String
Dim sqlstring1 As String
CON.ConnectionString = "Provider=SQLOLEDB.1;User ID=sa;password=cmq;Initial Catalog=rwdb2008;Data Source=127.0.0.1"
CON.ConnectionTimeout = 0
CON.Open
RS.CursorLocation = adUseClient
sqlstring = " select * into ls From st_dy_ys where ymdhm between '" & DTPicker1.Value & "' and '" & DTPicker2.Value & "'"
sqlstring1 = "select * from ls where datepart(minute,ymdhm)<>'00'"
RS.Open sqlstring1, CON, 3, 3
Set DataGrid1.DataSource = RS
i = RS.RecordCount
Text1.Text = i
End Sub
可没显示结果
请问是要刷新datagrid1的数据吗?
那个临时表 ls 是在sqlserver中还是在VB控件中呢? --------------------编程问答-------------------- 我还用SQL查询分析器试着查询了一下
select * into ls from st_dy_ys where ymdhm between '2008-1-1'and '2008-2-9'
select * from ls where datepart(minute,ymdhm)<>0
查询结果是对的,可再查询第二次就显示显示 表ls已存在
在VB中是这种情况吗?临时表就存在datagrid控件中了吗?所以查询就没结果了?
--------------------编程问答--------------------
select into newtable--------------------编程问答-------------------- 你可以改用标量表 而不是普通表
eg:
declare @t table(cid int)--------------------编程问答-------------------- 上边的大侠可以说的再详细点吗?
insert @t select cid from table
select * from @t
我按照你说的搜了好多网页,说的都看不懂啊....
刚开始学习,见谅啊。。。
我现在明白了如果用我上边的方法就算改变日期段(也就是DTPICKER的值)也只会查到一种结果,因为临时表里边的内容是确定的
我的意思就是如何创建一个动态变化的临时表
单单在临时表名字前加个@不成啊
把我现在的代码发给大家看看,请帮忙!
Private Sub Command1_Click()
Dim CON As New ADODB.Connection
Dim RS As New ADODB.Recordset
Dim sqlstring As String
Dim sqlstring1 As String
Dim sqlstring2 As String
CON.ConnectionString = "Provider=SQLOLEDB.1;User ID=sa;password=cmq;Initial Catalog=rwdb2008;Data Source=127.0.0.1"
CON.ConnectionTimeout = 0
CON.Open
RS.CursorLocation = adUseClient
sqlstring = " select * From st_dy_ys where ymdhm between '" & Format(DTPicker1.Value, "yyyy-mm-dd") & "' and '" & Format(DTPicker2.Value, "yyyy-mm-dd") & "'and len(stcd)=8 and datepart(minute,ymdhm)<>'00'"
sqlstring1 = " select * into ls From st_dy_ys where ymdhm between '" & Format(DTPicker1.Value, "yyyy-mm-dd") & "' and '" & Format(DTPicker2.Value, "yyyy-mm-dd") & "'and len(stcd)=8 and datepart(minute,ymdhm)='00'"
sqlstring2="select * from ls where dy=6"
If Option1 Then RS.Open sqlstring, CON, 3, 3
Set DataGrid1.DataSource = RS
If Option2 Then RS.Open sqlstring2, CON, 3, 3
Set DataGrid1.DataSource = RS
i = RS.RecordCount
Text1.Text = i
就是在那个sqlstring1和sqlstring2那个语句中的临时表ls出的问题,结果选择option2查询改变DTPICKER的值也就一个结果
End Sub --------------------编程问答-------------------- 俺是菜鸟,帮你UP一下。
补充:VB , 基础类