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!
Zoran HorvatPosted Jul 21, 2011, 5:26 PM
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;
conn = new OleDbConnection(connStr);
conn.Open();
cmd = new OleDbCommand(selectCmd, conn);
using (OleDbDataReader myReader = cmd.ExecuteReader())
{
StringBuilder sb = new StringBuilder();
while(myReader.Read())
{
sb.AppendFormat("Skid\t{0}\n", myReader["skid"]);
sb.AppendFormat("Weight\t{0}\n", myReader["weight"]);
sb.AppendFormat("Quantity\t{0}\n", myReader["quantity"]);
sb.AppendFormat("Description\t{0}\n", myReader["description"]);
}
if (sb.Length == 0)
sb.Append("Cannot find record!");
rtbShow.Text = sb.ToString();
}
conn.Close();
}
That will iterate through all records returned by the query and append them to control's text
Zoran
Yifei HouPosted Jul 22, 2011, 9:01 AM
Zoran HorvatPosted Jul 22, 2011, 4:27 AM
Please don't forget to check "This is correct answer" if this post has solved your problem. It helps authors collect community points.
Thanks,
Zoran
Yifei HouPosted Jul 21, 2011, 8:15 PM
Best Regards,
Yifei