简体   繁体   中英

Search Access Database with C#.NET

I have a simple code that can search fields in an access database. Now I need to change it to this parameters:

  1. Search a query in all fields in one textbox something like:

    "select * from Sheet where * like@*"

  2. Show the results in labels instead GridView.

I make an oleDbCommand and connect it to an oleDbConnection and my oleDbConnection is:

Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\\Database.accdb

I use of Visual C# 2010 and an .accdb Access database. This is my code:

private void btnSearch_Click(object sender, EventArgs e)
{ 
    DataSet DSOne = new DataSet();
    OleDbDataAdapter adpSearch = new OleDbDataAdapter();
    adpSearch.SelectCommand = new OleDbCommand();
    adpSearch.SelectCommand.Connection = oleDbConnection1;
    adpSearch.SelectCommand.CommandText = " select * from Sheet where OfficeNumber  like@OfficeNumber ";
    adpSearch.SelectCommand.Parameters.AddWithValue("@OfficeNumber", textBox1.Text + "%");
    adpSearch.Fill(DSOne, "toop");
    dataGridView.DataSource = DSOne;
    dataGridView.DataMember = "toop";
}

Best Regards

adpSearch.SelectCommand.CommandText = " select * from Sheet where OfficeNumber  like %@OfficeNumber%";
adpSearch.SelectCommand.Parameters.AddWithValue("@OfficeNumber", textBox1.Text);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM