none
Searching a database RRS feed

  • Question

  • I have created a win form with c# that just has a details view for 10 fields in a database.  I need to be able to search this database with any of the fields.

     

    Is there a tutorial that someone knows about that I can view.  I'm fairly new to this.

     

    Thanks,

     

    Dylan

     

    Wednesday, November 7, 2007 2:02 PM

Answers

  • Ok, understand what you want now.  Not sure if there is some pre-fabricated pattern out there for this.  We used to call this "Query By Form" (QBF) or "Query By Example" (QBE) back in the old days of com sci classes, not sure what they call it now.

     

    Microsoft Access now has this built into the product, it's pretty cool.  Zero lines of code and it works.  In .NET you have to roll up your sleeves and write a bit of code I suspect.

     

    What I would do is write a generic solution.  Write some code to scan all controls on the form and read the tag property of the control.  In the tag property I would put some info like field name, maybe a datatype hint.  Then I would build a parameterized query based on this information, create a dataset and forward the dataset to the new form which would bind to it.

     

    You could create the dataset bound form using the Wizard, it will automatically do this for you.

     

    Hmmm, better yet you could copy what Access does, it might actually be simpler. 

     

    Create a data bound form using the wizard.  Then in form open don't populate the dataset, instead create an empty one and bind it.  Let the user type n stuff (it will go into the dataset) then when they click search, you call some code that uses the values in the dataset to filter the data.

     

    In general if you are just getting started look around for tutorials in QFE of QBE for .NET I'm sure there is something out there.

     

     

     

    Sunday, November 11, 2007 10:36 PM

All replies

  • anyone??

    Thursday, November 8, 2007 5:02 PM
  • Hi,

    Exactly I dont know what you want , but I think DataView class will help in your task

     

    Friday, November 9, 2007 11:38 AM
  • I think you want to search any and all of the fields like a google type search?

     

    If so, then you want to investigate SQL Server "Full Text Search" feature, it lets you do this.

     

    If this is not the case, please explain in more details about what you want to do.

    In other words tell us what you want the user to be able to do and we can figure out the database details part.

    Friday, November 9, 2007 7:04 PM
  • Sorry guys for the confusion.

     

    I have a database with 10 fields.  Name, date, age, etc.  I want a front end that will search the records with any or all of the fields.

     

    So my form will have 10 text boxes searching each field in the database.  I want to be able to search with as many of the textboxes as I want (ie using age, name date or just name or just age.  Any combination.  When I fill in the text boxes I just hit a search button and the records show in another control.

     

    I just need to know how to make the text box search the field in the database. 

     

    Hope this makes a little more sense.

    Sunday, November 11, 2007 9:44 PM
  • Ok, understand what you want now.  Not sure if there is some pre-fabricated pattern out there for this.  We used to call this "Query By Form" (QBF) or "Query By Example" (QBE) back in the old days of com sci classes, not sure what they call it now.

     

    Microsoft Access now has this built into the product, it's pretty cool.  Zero lines of code and it works.  In .NET you have to roll up your sleeves and write a bit of code I suspect.

     

    What I would do is write a generic solution.  Write some code to scan all controls on the form and read the tag property of the control.  In the tag property I would put some info like field name, maybe a datatype hint.  Then I would build a parameterized query based on this information, create a dataset and forward the dataset to the new form which would bind to it.

     

    You could create the dataset bound form using the Wizard, it will automatically do this for you.

     

    Hmmm, better yet you could copy what Access does, it might actually be simpler. 

     

    Create a data bound form using the wizard.  Then in form open don't populate the dataset, instead create an empty one and bind it.  Let the user type n stuff (it will go into the dataset) then when they click search, you call some code that uses the values in the dataset to filter the data.

     

    In general if you are just getting started look around for tutorials in QFE of QBE for .NET I'm sure there is something out there.

     

     

     

    Sunday, November 11, 2007 10:36 PM
  • Hi,

    You may build the search sql statment by your self ,  I perfer that and see thats eaiser

    just do that:

    string sqlSearchStatment = " SELECT * FROM tablename ";

     then check the value for each TextBox if it isn't null -> add this value for the sqlSearchStatment .. like that

     

    if (nameTextBox.Text != "" )

    sqlSearchStatment += " AND Name LIKE ' " + nameTextBox.Text + " ' " ;

    if (ageTextBox.Text != "" )

    sqlSearchStatment += " AND Age LIKE ' " + ageTextBox.Text + " ' " ;

    .

    .

    .

    and so on

    finally excute OleDbCommand using sqlSearchStatment.

     but take care you will have to add <WHERE> clause at its appropirate position

    Monday, November 12, 2007 11:38 AM