7 Replies - 1075 Views - Last Post: 22 April 2012 - 01:15 PM Rate Topic: -----

#1 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 27
  • View blog
  • Posts: 509
  • Joined: 10-October 09

Searching Through a SQL Database

Posted 21 April 2012 - 05:00 PM

I am a near complete beginner with the C# programming language, and I have started making a Contacts list in Visual C# Express. It stores all data from table People into seperate columns (Name, Address, Telephone, etc..). What I want to do is I want to implement a search bar what will be able to pull up the records by searching through the SQL database.
Is This A Good Question/Topic? 0
  • +

Replies To: Searching Through a SQL Database

#2 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Searching Through a SQL Database

Posted 21 April 2012 - 05:07 PM

That sounds like a nice project, have you made a start on it? Or got a specific problem?

You should check out the tutorials section of the website, there are several topics related to SQL there. Such as this oneSQL basics.

This post has been edited by negligible: 21 April 2012 - 05:08 PM

Was This Post Helpful? 0
  • +
  • -

#3 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 27
  • View blog
  • Posts: 509
  • Joined: 10-October 09

Re: Searching Through a SQL Database

Posted 21 April 2012 - 05:13 PM

Yes, I've actually gotten pretty far on it. The GUI is, for the most part, laid out. I'm going to modify it a lot later, but for now, it is laid out as it needs to be. All of the code works 100%, and the database works flawlessly. I just need to learn how to add a Search function to my GUI, just to make searching through the contacts list more convenient. Thank you for the link, I will take a look at them

More Specifically, I have a Windows Forms application, and I want it to be able to search each row on a column for a specific name, number, etc.. Is this possible? If so, how?

The tutorial was good, but not exactly what I was looking for.

This post has been edited by optix212: 21 April 2012 - 05:19 PM

Was This Post Helpful? 0
  • +
  • -

#4 negligible  Icon User is offline

  • D.I.C Regular

Reputation: 62
  • View blog
  • Posts: 302
  • Joined: 02-December 10

Re: Searching Through a SQL Database

Posted 21 April 2012 - 05:20 PM

Any command you send to an SQL database is in the form of a query.

You can create them as a string and send them to your SQL database.

Querys are a scripting language itself, so you're going to need to do some research. The WHERE statement is used for searching.

SQL Queries in C#
I googled "query sql database c#" to find this, explains it well. You need to develop some pro googling skills to develop your coding, there is a lot of information available on the web.
It helps to type in some keywords for what your looking for followed by "C#" or "MSDN".

This post has been edited by negligible: 21 April 2012 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5575
  • View blog
  • Posts: 11,916
  • Joined: 02-June 10

Re: Searching Through a SQL Database

Posted 21 April 2012 - 05:36 PM

See FAQ # 22. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated Apr 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#6 optix212  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 27
  • View blog
  • Posts: 509
  • Joined: 10-October 09

Re: Searching Through a SQL Database

Posted 21 April 2012 - 07:06 PM

Okay, So I created a button on the form, and created a search method pretty much the only way I could figure out how.

        private void button6_Click(object sender, EventArgs e)
        {
            String searchFor = textBox1.Text;
            int results = 0;
            DataRow[] returnedRows;

            returnedRows = contactDBDataSet.Tables["People"].Select("Name='" + searchFor + "'");

            results = returnedRows.Length;

            if (results > 0)
            {
                
                
            }
            else
            {
                MessageBox.Show("No such Record");
            }
        }


This method works, and it finds the record just fine. Now, what I want it to do is turn the BindingNavigator to the page that the name is on. Any suggestions on how I may be able to accomplish this? Thanks in advance!

I am following this tutorial: http://www.homeandle...arp_s12p12.html
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5575
  • View blog
  • Posts: 11,916
  • Joined: 02-June 10

Re: Searching Through a SQL Database

Posted 21 April 2012 - 07:16 PM

Quote

01 private void button6_Click(object sender, EventArgs e)



Some of my common tips
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)


  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.


    btnSave(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveMenuItem(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveContextMenu(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    FormMain_Closing(object sender, eventargs e)
    {
        if (IsDirty) SavePreferences();
    }
    


  • Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

    // Try #1 - May 1, 0900hrs
    //    code
    //    code  
    //    code
    // Try #2 - May 2, 1700hrs   Okay, plan B.  What if I do it *this* way
    //    code
    //    code  
    //    code
    // Try #14 - May 3, 0500hrs after 5 cans of RedBull.  Maybe I should get some sleep. I can't think of anything else but this last idea
        code
        code  
        code
    
    

    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

Was This Post Helpful? 0
  • +
  • -

#8 ku3mich  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 11
  • Joined: 22-April 12

Re: Searching Through a SQL Database

Posted 22 April 2012 - 01:15 PM

I think You are using ADO.net with chain DbConnection -> DbAdapter -> DataTable(or DataSet)

If it is so good if not tell how you recieve Data from database.

My advaice is to re-create query using DbAdapter and refill DataTable using SQL clause WHERE

somthing like:
string queryString = 
  "SELECT CustomerID, CompanyName FROM dbo.Customers";

// add WHERE clause
queryString += " WHERE CompanyName like '%"+TextBox.Text+"%'";

SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

DataSet customers = new DataSet();
adapter.Fill(customers, "Customers");



see: http://msdn.microsof...y/bh8kx08z.aspx

or using filters as shown at:
http://stackoverflow...ltering-dataset
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1