RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

How do i search SQl database

jocan2003

Sorceror
Find partial matchs in string.

Im using the visual C# studio express and im trying to make an phone number rgram that will hold multiple name phone number town etc entry, i did all the tutorial about how to connect the SQL to my application how to create one etc, ut the only part they didnt say is how to search an item in the database.

Here is a quick explanation of my forms

2 radiobutton one is opname and one is oplastname
textbox ( for entering the search request.
one quit button
one edit button ( to add and remove entry for database )
one search button

actually i want to be able to search either in the name or lastname columns in the database Name and the other Lastname depending on wich radiobutton they choose so far my code look like that.

public void bcsearch_OnClick()
{
string searchtype = "";
if ( opname.Checked == true )
searchtype = "Name";
else if ( oplastname.Checked == true )
searchtype = "Lastname":
}

and... thats about it.... i think you figure out why i made these if statement it will be to use the variable searchtype instead of the name of the column. can somone help me please?
 

mordero

Knight
Ok, Im confused about what your question is exactly? You dont know how to run and handle queries to a database in C#?
 

jocan2003

Sorceror
i know how to get them show on my forms one by one or in table, and also how to modify them, but i dont know how to actualy search for a specific entry
 

jocan2003

Sorceror
i dont think i can write that in the bcsearch_OnClick event can i ? using sql code in that CSharp OnClick event wont be a problem?
 

jocan2003

Sorceror
well the only things i know is from this tutorial

Beginner Developer Learning Center: Absolute Beginner's Series 8

i dont know much else about making query as he only show basic stuff, from the OOP wich mean didnt show anything relate to SQL coding....

***edit*** if you dont mind, can you help me going step by step into that? i dont even know where to start....

I thik i found soimething out, im using visual C# studio studio 2005 wich is free from microsoft, right now im binding a listbox to the database, but while doing that i saw a little add query so i tought i could write the *search* in that, but look like i cant, here is what i see.

can you guide me from here?
 

jocan2003

Sorceror
Well after some tries i gave upop, i will look at it later, instead im using XML file to store my data, and its now working so far, but only one problem, here is my code for the event.
Code:
private void button3_Click(object sender, EventArgs e)
        {
            

            Form2 fm = new Form2();
            XmlDocument docXML = new XmlDocument();

            docXML.Load("Liste.xml");
            string strsearchentry = this.textBox1.Text.ToString();

            XmlElement nodRoot = docXML.DocumentElement;
            XmlNodeList nodItems = nodRoot.GetElementsByTagName("nom");
            int p = nodItems.Count;
            string[] itemsarray = new string[p];

            for (int i = 0; i < nodItems.Count; i++)
            {
                //fm.listBox1.Items.Add(nodItems[i].InnerXml);                
                 itemsarray[i] = nodItems[i].InnerText.ToString();

            }
            foreach (string item in itemsarray)
            {
                if (strsearchentry == item)
                    fm.listBox1.Items.Add(item);
            }
            /*for (int i = 0; i < fm.listBox1.Items.Count; i++)
            {
                
                string stradd = fm.listBox1.FindString(strsearchentry);

            }*/
            fm.Show();
        }

in this one i search and return any item that match the strsearchentry ( wich is what the ppl enter in the search text box ) but how can i verify each item that they include the search entry in their name but not totally.

I.E.; i search only for joc, but it doesnt return the jocan item, how can i make it so it will return jocan even if the strsearchentry only contain joc?
 

mordero

Knight
You can use String.Contains();
Like so:
Code:
string search = "Joc";
string[] data = new string[] { "Jocan", "Jock", "Bob", "George" };
foreach (string s in data)
{
     if (s.Contains(search))
       //its there
     else
       //its not
}
 

mordero

Knight
yep no problem, and keep looking into the SQL though, its really not that hard (although ive only worked with SQLite and c# before) and its definately a better answer to what you are doing.
 

jocan2003

Sorceror
im only writing up a small phone book as a personal project trying to learn everything in same time, so i will go where it look like a bit easier the time i get better, then i will go on SQL.

also i will be making some game using forms later on, and save game will be save in encoded xml format ( to avoid editing hehe ).

but i aso know that iw ill need SQL database to store multple variable that i may use in these game so let start somewhere i wont get lost first at hehe.

++karma fried :)
 
Top