4
Reply

C# Access How to return multiple results from database??

Yifei Hou

Yifei Hou

Jul 21 2011 4:52 PM
1.6k
Hello,

I'm using visual c# to write an application which can search key word in database. How can I get all match results?

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;
//
namespace Command2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            string connStr, selectName, selectCmd;
            selectName = txtName.Text;
            selectCmd ="SELECT * FROM product WHERE skid = '" + selectName.Replace("'", "''") + "'";
            connStr ="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=product.mdb";
            OleDbConnection conn;
            OleDbCommand cmd;
            OleDbDataReader myReader; 
            conn = new OleDbConnection(connStr);
            conn.Open();
            cmd = new OleDbCommand(selectCmd, conn);
            myReader = cmd.ExecuteReader();
            if (myReader.Read())
            {
                rtbShow.Text = "Skid" + "\t" + myReader["skid"] + "\n";
                rtbShow.Text += "Weight" + "\t" + myReader["weight"] + "\n";
                rtbShow.Text += "Quantity" + "\t" + myReader["quantity"] + "\n";
                rtbShow.Text += "Description" + "\t" + myReader["description"] + "\n";
              
             
            }
            else
            {
               rtbShow.Text = "Can not find record!";
            }
            myReader.Close();
            conn.Close();

        }

        private void btnEnd_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'productDataSet.product' table. You can move, or remove it, as needed.
            this.productTableAdapter.Fill(this.productDataSet.product);

        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

       
    }
}

For example if there are more than one data in "skid" column which value is A001, how can I display them all?? Thanks! 

Answers (4)