更新时间:2023-10-19 18:30:52
您想从数据库中检索数据并将其输出到Windows窗体控件或什么?..如果那是您要执行的操作,则必须执行以下操作..
1.首先在Access数据库(2003/2007)中创建表.
2.现在,您可以使用以下代码获取所需的数据
You want to retrieve data from a database and output it unto a windows form control or what?..If thats what you want to do then you''d have to do the following..
1. first create your table in an Access database (either 2003/2007).
2. Now you can use the following code to get the data you need
//Add the following namespaces to the calling class
using System.Data;
using System.Data.OLEdb;
using System.Windows.Forms;
..
class DB
{
public void getData([form controls here as parameters eg.] Textbox tbxFname, Textbox tbxLname)
{
OLEdbConnection conn=new OLEdbConnection();
conn.ConnectionString="Provider=Microsoft.JET.4.0; Data Source=[database name here].mdb(if access 2003)/.accdb(if access 2007)";
OLEdbCommand cmd=new OLEdbCommand();
cmd.Connection=conn;
cmd.CommandType=CommandType.Text;
cmd.CommandText="select * from [table name here] where [specify condition here]";
OLEdbDataReader rd;
rd=cmd.ExecuteReader();
while(rd.Read())
{
tbxFname.Text=(rd[0].Value).toString();
tbxLname.Text=(rd[1].Value).toString();
.....
// other controls will be linked to various reader values and also the numbers o,1 etc signify the field position in the database
}
conn.Close();
cmd.Dispose();
}
}
要调用表单上的代码,
1.首先在form_load事件中为类创建一个对象,例如:
To call the code on the form,
1. First create an object to the class in the form_load event, Example :
DB _object = new DB();
_object.getData([input argument list here]);
// but make sure arguments reference the form controls
而且,当您构建项目时,我认为在进行正确的更改后它应该可以工作!
And when you build the Project I think it should work after you make the right changes!