我想從當前月份的Access數據庫中導入記錄,並且需要將記錄顯示在我的datagridview
上,表單加載不是按鈕單擊。我的理由是,如果最終用戶打開程序,可能需要一些時間才能加載所有記錄,所以我只想檢索爲當前月份輸入的記錄,顯然是在表單加載時輸入的記錄。我對編碼不熟悉,因此我在這裏要求這裏的人們帶着大量的經驗。來自Access MS DB的表查詢
我的連接代碼如下。
Public Sub GetData()
con.Open()
Dim dt As New DataTable("tbLMPH")
Dim rs As New OleDb.OleDbDataAdapter("SELECT * FROM tbLMPH WHERE Year(IDday) = Year(Now()) AND Month(IDday) = Month(Now())", con) rs.Fill(dt)
DataGridView1.DataSource = dt
DataGridView1.Refresh()
Label1.Text = dt.Rows.Count
rs.Dispose()
con.Close()
If Val(Label1.Text) = 1 Then
Dim i As Integer
i = DataGridView1.CurrentRow.Index
TextBox2.Text = DataGridView1.Item(1, i).Value
TextBox3.Text = DataGridView1.Item(2, i).Value
TextBox4.Text = DataGridView1.Item(3, i).Value
TextBox5.Text = DataGridView1.Item(4, i).Value
TextBox6.Text = DataGridView1.Item(5, i).Value
TextBox7.Text = DataGridView1.Item(6, i).Value
End If
' Display_Data()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.TbLMPHTableAdapter.Fill(Me.MaindbDataSet.tbLMPH)
My.Computer.Audio.Play(My.Resources.jimmy2, AudioPlayMode.Background)
Display_Data()
Timer1.Start()
Label2.ForeColor = Color.White
Display_Data()
End Sub
如果您正在嘗試導入記錄,檢索數據的代碼在哪裏? 而對於一個建議,使用'參數Queries' – Mahadev
公用Sub的GetData() con.Open() 昏暗DT作爲新的DataTable( 「tbLMPH」) 昏暗RS作爲新OleDb.OleDbDataAdapter(「從tbLMPH其中選擇* IDday ='」&TextBox1.Text& 「」」,CON) rs.Fill(DT) DataGridView1.DataSource = DT DataGridView1.Refresh() Label1.Text = dt.Rows.Count rs.Dispose() con.Close() – Jason
如果val(Label1.Text)= 1然後 昏暗我作爲整數 I = DataGridView1.CurrentRow.Index TextBox2.Text = DataGridView1.Item(1,i)的。價值 TextBox3.Text = DataGridView1.Item(2,i)的。價值 TextBox4.Text = DataGridView1.Item(3,i)的。價值 TextBox5.Text = DataGridView1.Item(4,i)的。價值 TextBox6.Text = DataGridView1.Item(5,i).Value TextBox7.Text = DataGridView1.Item(6,i).Value End If 'Display_Data() End Sub – Jason