在Access中查询记录 [英] Quering records in Access

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

问题描述

你好,

请帮我提供示例代码,我可以使用该示例代码为表中的每个记录生成自动查询.我希望将命令作为按钮单击事件,以便当我选择记录ID(例如从comboBox或记录选择器中)时,会自动创建一个包含查询记录的所有详细信息的查询.

谢谢

Hi there,

please help me with a sample code I can use to generate automated queries for each of the records in my table. I would like to have the command as a button click event so that when I choose a record ID, say from a comboBox or record selector, a query is automatically created comprising of all the details of the queries record.

Thanks

推荐答案

您好,根据您的查询,我在C#中为您编写了一个代码,该代码将使用访问数据库,
现在,此代码中发生的事情是,我在表单加载时将员工ID加载到组合框中,然后用户必须从组合框中选择特定的ID,然后单击一个按钮.在按钮上单击,用户将在文本框中获取雇员的姓名

Hi as per your query i have made a code for you in C# which will use the access database,
Now what happens in this code is that i am loading the employees id in a combobox on a form load and then the user has to select a particular id from the combobox and click a button. On button click user will get the name of an employee in a textbox

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb; //make sure u add this namespace

namespace WindowsApplication3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Database1.mdb;Persist Security Info=False");
            con.Open();
            string query = "Select name1 from mytable where id=''"+comboBox1.Text+"''";
            OleDbCommand cmd = new OleDbCommand(query,con);
            OleDbDataReader dr = cmd.ExecuteReader();
            if(dr.Read()==true)
            {
                textBox1.Text = dr[0].ToString();
            }
            else
            {
                MessageBox.Show("No such record");
            }
            con.Close();
            con.Dispose();

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Database1.mdb;Persist Security Info=False");
            con.Open();
            string query = "select id from mytable";
            OleDbCommand cmd = new OleDbCommand(query, con);
            OleDbDataReader dr = cmd.ExecuteReader();
            while (dr.Read() == true)
            {
                comboBox1.Items.Add(dr[0].ToString());
            }
            con.Close();
            con.Dispose();

        }
    }
}


一旦找到有用的答案,就对我的答案进行评分
谢谢&问候
Radix:)


Do rate my answer once you find it useful
Thanks & Regards
Radix :)


在将访问数据库与C#结合使用时,请参见链接:
On using access database with C#, see the link: Google Search: access database with C#[^].

katumbamartin写道:
katumbamartin wrote:

希望将命令作为按钮单击事件,以便当我选择一个记录ID时,例如从comboBox或记录中说选择器,将自动创建一个包含查询记录的所有详细信息的查询.

would like to have the command as a button click event so that when i choose an a record id, say from a comboBox or record selector, a query is automatically created comprising of all the details of the queries record.



您需要将记录id作为参数传递给数据库.因此,我们的查询将类似于:



You need to pass the record id as the parameter to the database. So our query will be like:

SELECT * FROM Table_Name WHERE [Id] = @RecordID


其中id是主键字段名称,@ RecordID是您将从程序中传递的参数.
*这是一个简单的sql查询

这些是非常基本的东西,因此我建议您在实际开始编码之前先阅读网络上的一些书籍或文章/教程.
希望你本着良好的心情. :thumbsup:


where id is the primary key field name and @RecordID will be the parameter which you will pass from your program.
* this is a simple sql query

These are very basic things and so I would suggest you to read some books or articles/tutorials on the web before you actually start coding.
Hope you take that in a good spirit. :thumbsup:


这篇关于在Access中查询记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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