Re: [.NET] 怎麼寫資料庫 日期區間查詢???
※ 引述《yankees35 (班傑米)》之銘言:
: 請輸入專案類型(網站專案或者應用程式專案):Windows Form
: 目前正用visual studio 2010寫資料庫程式
: 因為本身除了以前念書,已經兩年多沒碰程式了
: 有個問題想請教,資料庫的日期區間查詢我該怎麼寫呢?
: 我知道可以用ADO.NET的方式可以寫
: 不過市面上的書幾乎都沒寫到查詢兩個日期之間的資料
: 例如:我要顯示2012年4月1日~2012年5月8日的資料
: 有甚麼方法可以解決嗎?
: 另外如果是ado的方式,我要怎麼設定他的元件呢?SQL Server要設定嗎?
: 再麻煩大家幫我解答 感謝!
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
我照著書上KEY的程式碼
他是使用OleDbConnectionStringBuilder的方法
但我在VB2010的.net framwork 4.0上找不到這元件
不過程式碼裡用KEY的也可以出現元件
程式碼如下
Imports System.Data.OleDb
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim selectCmd As String = ""
selectCmd = "select 識別碼 as 編號 , UhcDate as 日期 ," & _
"UhcTime as 時間 , Sun as 太陽能 , Temp1 as 溫度1 , Temp2 as 溫度2 "
& _
"Inv1 as Inv1 , Inv2 as Inv2 , Inv3 as Inv3 , Total as 合計" & _
" from UHC Where UhcDate Between " & _
DateTimePicker1.Value.ToString("yyyy年MM月dd日") & "' And '"
& _
DateTimePicker2.Value.ToString("yyyy年MM月dd日") & "' And " &
_
"IS_DISPLAY=True And "
selectCmd = selectCmd & "Total Like '%" & TextBox1.Text & _
"%' Order by UhcDate DESC"
Dim ConnString As String
Dim Builder As OleDbConnectionStringBuilder =
New OleDbConnectionStringBuilder()
Builder.Provider = "Microsoft.ACE.OLEDB.12.0"
Builder.DataSource = Application.StartupPath & "\UHCSolar.mdb"
ConnString = Builder.ConnectionString
Dim Conn As OleDbConnection = New OleDbConnection(ConnString)
Conn.Open()
Dim da As OleDbDataAdapter = New OleDbDataAdapter(selectCmd, Conn)
Dim DT As DataTable = New DataTable()
da.Fill(DT)
If (DT.Rows.Count = 0) Then
MessageBox.Show(Me, "搜尋條件並未找到任何福核資料",
"搜尋結果")
Else
MessageBox.Show(Me, "共找到資料筆數" & DT.Rows.Count.ToString(), "
搜尋結果")
End If
Me.DataGridView1.RowsDefaultCellStyle.BackColor = Color.Ivory
Me.DataGridView1.AlternatingRowsDefaultCellStyle.BackColor =
Color.Azure
DataGridView1.DataSource = DT
End Sub
End Class
結果在
da.Fill(DT)
這邊卻出現錯誤
寫說OleDbException未處理
請問我需要修改甚麼嗎?
請板上大家幫忙一下!!!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.33.27.9
→
05/09 22:27, , 1F
05/09 22:27, 1F
推
06/29 21:56, , 2F
06/29 21:56, 2F
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):