none
how to search an item in database?

    Question

  • I'm making a database project, it's n access database.

    It contains many items and I want to make search,such that the user writes something and the program should search the table for anything that may contain it.

    So please provide me with a code

    Thursday, July 11, 2013 10:49 AM

Answers

  • I assume you want a query that searches all fields in a database

    You could use it like this:

    myCmd.CommandText = "Select * from DatabaseName where field1 like '%" & Textbox1.text & "%' OR field2 like '%" & Textbox1.text & "%' OR field3 like '%" & Textbox1.text & "%' OR field4 like '%" & Textbox1.text & "%'"

    Thursday, July 11, 2013 11:11 AM

All replies

  • Hello,

    For better assistance, you should provide more detail information. There are many different ways to facilitate a search starting with. Do you want to show data in a DataGridView then a) filter b) search using TextBox controls for input with say a ComboBox for the user to select which field or fields to search on or start with not showing data then only show data that matches the user’s search criteria?  

    Also, if you have not chosen a method to connect to your database you need to do this up front. For instance, connect via a data provider and hand code everything or use the IDE data wizard to create classes for your database operations.

    Please also indicate which version of Visual Studio is being used i.e. VS2012 Ultimate or perhaps VS2010 Express.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, July 11, 2013 11:04 AM
  • I assume you want a query that searches all fields in a database

    You could use it like this:

    myCmd.CommandText = "Select * from DatabaseName where field1 like '%" & Textbox1.text & "%' OR field2 like '%" & Textbox1.text & "%' OR field3 like '%" & Textbox1.text & "%' OR field4 like '%" & Textbox1.text & "%'"

    Thursday, July 11, 2013 11:11 AM
  • John,

    If Titanium man11 is just starting out posting an example like this would be unwise for several reasons. First off you would need to explain how to work a multi field search where one time it might be one field another time two field etc. Secondly your code is doing one flavor for a like condition (contains, how about starts with, ends with etc) where this may not fit their needs. Lastly for someone starting out they should be taught proper assertion and to use parameterized SQL statements.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, July 11, 2013 11:18 AM
  • Hi kevininstructor

    I quote the core of his question: "program should search the table for anything that may contain it."

    Is there someway I can see that he is starting out?

    But you're right, his question does not contain enough information to answer it.

    C Ya

    john

    Thursday, July 11, 2013 12:02 PM