来自Access MS DB的表查询 [英] Table Query from an Access MS DB
本文介绍了来自Access MS DB的表查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从当前月份的Access数据库导入记录,并且需要在表单加载而不是单击按钮时在我的datagridview中显示记录。我的理由是,如果最终用户打开程序,可能需要一些时间来加载所有记录,所以我只想检索当前月份输入的记录,显然是表单加载时的年份。我是编码的新手,因此我问这里的人有很多经验。我不知道该怎么做。
公共 Sub GetData()
con.Open()
Dim dt As 新 DataTable( tbLMPH)
Dim 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()
如果 Val(Label1.Text)= 1 然后
Dim i 作为 整数
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
结束 如果
' Display_Data()
结束 Sub
解决方案
首先,不要将文本框中的值连接到SQL查询,而是始终使用参数,请参阅 OleDbParameter [ ^ ]
获取您的内容可以使用年份和月份功能。例如,如果IDday列是您想要比较的列,那么类似
Dim rs 作为 新 OleDb.OleDbDataAdapter( SELECT * FROM tbLMPH WHERE年(IDday)=年(Now())和月(IDday)=月(Now()),con)
I am trying to import the records from an Access database for the current month and need the records to be displayed in my datagridview on form load not a button click. My reason for this is if a end user opens the program it could take some time to load all the records, so I want to only retrieve the records entered for the current month and obviously year when the form loads. I am new to coding hence the reason I'm asking the people on here with loads of experience. I have no idea of who to do this.
Public Sub GetData()
con.Open()
Dim dt As New DataTable("tbLMPH")
Dim rs As New OleDb.OleDbDataAdapter("Select * from tbLMPH where IDday='" & TextBox1.Text & "' ", 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
解决方案
First of all instead of concatenating a value from textbox to your SQL query always use parameters, see OleDbParameter[^]
What comes to fetching the you can use Year and Month functions. For example if the IDday column is the one you want to compare then something like
Dim rs As New OleDb.OleDbDataAdapter("SELECT * FROM tbLMPH WHERE Year(IDday) = Year(Now()) AND Month(IDday) = Month(Now())", con)
这篇关于来自Access MS DB的表查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文