从Form1动态调用Access 2007记录 [英] Calling access 2007 records dynamically from Form1

查看:83
本文介绍了从Form1动态调用Access 2007记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的先生/女士:

我已经在该项目中创建了一个带有几个Forms的C#项目.在一种特定的形式(让我们说Form1)中,我有10个按钮.我还创建了一个Access 2007数据库,在此数据库中,我有一个包含10条记录的表.

现在我的问题是:

是否可以从Form1动态调用这10个数据库记录(但不能一对一地调用).例如,我知道如何在单击(让我说Button1)时调用数据库记录,例如

Dear Sir/Madam

I have created a C# Project with couple of Forms inside this project. In one particular form (lets say Form1) I have 10 Buttons. I also created a Access 2007 DataBase and inside this database I have a table with 10 records.

Now my question is:

Is there a possibility to call these 10 database records dynamically from Form1 (but not one-by-one). For instance I know how to call a db record when clicking (lets say Button1) e.g.

Button1_Click(object sender, EventArgs e)
{
    //to do database stuff.....
}



但这不是我想要的.

基本上我想要的是,当Form1加载(此表单内有10个按钮)以创建SQL查询时,从数据库中获取那10条记录,而我单击的每个按钮都希望该db记录与相应的按钮匹配并显示该记录在我的列表视图中.


亲切的问候


Agron



but this is not the case I want.

Basically what I want is, when Form1 Loads (where inside this form are 10 buttons) to create an SQL Query, get those 10 records from database, and each button I click I want that db record to match the corresponding button and display that record on my list view.


Kind Regards


Agron

推荐答案

一种方法是在Form.Load事件中将数据加载到DataTable/DataSet(您说您知道如何做)中.

然后,仍在Form.Load中,从每个记录中获取要使用的值(例如ID或名称),并将该值用作每个按钮的Text.然后处理DataTable/DataSet.

然后,当单击按钮时,使用参数化查询 [ ^ ](向下滚动,直到您点击 Select 部分.)

您所处环境的主要区别是您将使用
One way is to load the data into a DataTable/DataSet (you say you know how to do this) in the Form.Load event.

Then, still in Form.Load, get the value you want to use, ID say or Name, from each record and use that value as the Text of each button. Then dispose of the DataTable/DataSet.

Then when a button is clicked use a parameterized query[^] (scroll down till you hit the Select section.)

The main difference in your situation is that you would use
cmd.Parameters.AddWithValue("ID", Button.Text);



希望这可以帮助. :)



Hope this helps. :)


这篇关于从Form1动态调用Access 2007记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆