Results 1 to 16 of 16

Thread: [RESOLVED] Search button with databases

  1. #1

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Resolved [RESOLVED] Search button with databases

    Hi!!

    I've 4 databases which one have 4 tables. I've a form with a textbox (Searchbox) and a search button (Search). What i want is when u click in search button, he adds to an array all items in the tables of the DB and by colunms by ADO and finaly to seach in the array the text of the textbox and for each found result to add it to a new array also by colunms. U get the point, if not will try to be clear.

    Thks!!!
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Search button with databases

    Sounds like you need to do some reading on ADO.NET. There are numerous tutorials on the subject in my signature.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: Search button with databases

    Thks jmcilhinney!

    But i already know how to conect and use recordset, what i dont know is arrays, arrayslist, multidimensional arrays...
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Search button with databases

    Recordsets are ADO. You should dump ADO and learn how to use ADO.NET for all your new work. Arrays are a pretty standard part of any programming langauge, so you should do some reading that subject also. The Start VB.NET link in my signature has a section on arrays. An ArrayList is just a simple collection. You create one as you would any other type of object and you call its Add method to add items. You should go to the MSDN library, which you should have installed locally if you're using VB Express or you can use the online version, and read the help topic for the ArrayList class (search for "arraylist class" without quotes). It will give an overview of how to use the class and a link to its member listing for more detailed information.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Arrow Re: Search button with databases

    Thks for the answer but i prefer ADO even if ADO.NET is best because: with ADO.NET i cant change the conection string in runtime mode, i have to add to the project many table adapters in both forms and i'm working with +/- 40 tables and that only slows me down.

    One question with ADO can i make a new database file (mdb) with various tables?

    PS: The answer about arrays make me thing and i get to the conclusion that with arrays will only make my work harder so i get another way of solving the problem. Thks again
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  6. #6
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Search button with databases

    ADO.NET i cant change the conection string in runtime mode
    Yes you can.
    i have to add to the project many table adapters
    No you don't.
    i'm working with +/- 40 tables
    Even more of a reason to use a typed dataset.

    But back to what you're asking about ADO.
    You can do this with ADOX. Reference "Microsoft ADO Ext. 2.8 for..." and the following code should work:
    VB Code:
    1. Dim cat As New ADOX.Catalog
    2.         cat.Create("Provider='Microsoft.Jet.OLEDB.4.0';Data Source='C:\new.mdb'")
    More info on ADOX:
    http://msdn.microsoft.com/library/de...ireference.asp

  7. #7

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: Search button with databases

    Thks a lot for the answer but one thing how can i add a ADOX API Reference

    For curiosity how can u change the conection string in runtime mode with ADO.NET? If i dont have to use table adapters what do i use then?
    Last edited by Lasering; May 30th, 2006 at 05:00 PM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  8. #8
    Banned
    Join Date
    Nov 2005
    Posts
    2,367

    Re: Search button with databases

    The same way as you would change any property or value in runtime mode? I guess I'm not following where you think the issue lies in changing this property?

  9. #9

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: Search button with databases

    I go to Data Menu - Add New Data Source, and then create a new conection that stays stored in settings of the project. When i try to change the conection he says this propertie is readonly

    Only a Example
    VB Code:
    1. My.Settings.LevelsConnectionString = Application.StartupPath
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Search button with databases

    You're using a lot of the built-in functionality of the IDE to create your data access code. Like all wizards and code generation mechanisms it will make various assumptions about what you want to do. There's a lot more to ADO.NET than that. You can create a typed DataSet without creating a Data Source. You can also create all your connections and data adapters yourself, which allows more control than letting the IDE do it for you. That's how ADO.NET worked in the last two versions of .NET and whle it's not what Microsoft is pushing it is still available in .NET 2.0. You can simply create an OleDbConnection and set its ConnectionString property whenever you like. You should do some reading on ADO.NET, particularly as it was before .NET 2.0, to get an idea of what you can do yourself. TechGnome's ADO.NET tutorials (see my signature for links) are a good place to start.

    Edit: Plus, even if you can't change what's in the settings at run time you can still change the ConnectionString of the connection. If you want to change what's in the settings you can either do so by manually editing the config file in Notepad or do it in code with an XmlDocument object. Forget ADO. ADO.NET can do exactly what you want and much more besides.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Arrow Re: Search button with databases

    So how can do this:
    VB Code:
    1. Conect.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Chr(34) & Application.StartupPath & ActualDB & Chr(34))
    2.         Conect.Execute("INSERT INTO" & " " & TabelaActual & " " & "VALUES ('" & ItemName.Text & "', '" & Category.Text _
    3.         & "', '" & ItemNumber.Text & "', '" & ItemLevel.Text & "', '255', '0', '0', '0', '" & Altura.Text & "', '" & Largura.Text & "')")
    4.         RS.Open(TabelaActual, Conect, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockPessimistic)
    5.         RS.Update()
    6.         RS.MoveFirst()
    7.         For a As Short = 0 To RS.Fields.Count
    8.             Do Until RS.EOF = True
    9.                 L1.Items.Add(RS.Fields.Item("Category").Value & " " & RS.Fields.Item("Number").Value & " " & RS.Fields.Item("Level").Value _
    10.                 & " " & RS.Fields.Item("Durability").Value & " " & RS.Fields.Item("Skill").Value & " " & RS.Fields.Item("Luck").Value _
    11.                 & " " & RS.Fields.Item("Options").Value & " - " & RS.Fields.Item("Name").Value)
    12.  
    13.                 TabAux.Items.Add(RS.Fields.Item("Name").Value)
    14.                 RS.MoveNext()
    15.             Loop
    16.         Next
    17.         RS.Close()
    18.         Conect.Close()

    With ADO.NET instead of ADO because i can't simply don't understand
    Last edited by Lasering; May 31st, 2006 at 04:07 PM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Search button with databases

    As always there are a number of different ways to achieve the same end result in ADO.NET so this is not definitive. Also, I've never used ADO so I don't know exactly how RecordSets behave. Having said that, this is a pretty good approximation of what you have there:
    VB Code:
    1. Dim ActualDB As String
    2.         Dim TabelaActual As String
    3.  
    4.         Dim myConnectionStringBuilder As New OleDbConnectionStringBuilder
    5.  
    6.         myConnectionStringBuilder.Provider = "Microsoft.Jet.OLEDB.4.0"
    7.         myConnectionStringBuilder.DataSource = IO.Path.Combine("|DataDirectory|", ActualDB)
    8.  
    9.         Dim myConnection As New OleDbConnection(myConnectionStringBuilder.ConnectionString)
    10.         Dim myInsertCommand As New OleDbCommand("INSERT INTO @Table (<you really should put the column names here>) VALUES (@Name, @Category, @Number, @Level, 255, 0, 0, 0, @Altura, @Largura)", myConnection)
    11.  
    12.         myInsertCommand.Parameters.AddWithValue("@Table", TabelaActual)
    13.         myInsertCommand.Parameters.AddWithValue("@Name", ItemName.Text)
    14.         myInsertCommand.Parameters.AddWithValue("@Category", Category.Text)
    15.         myInsertCommand.Parameters.AddWithValue("@Number", ItemNumber.Text)
    16.         myInsertCommand.Parameters.AddWithValue("@Level", ItemLevel.Text)
    17.         myInsertCommand.Parameters.AddWithValue("@Altura", Altura.Text)
    18.         myInsertCommand.Parameters.AddWithValue("@Largura", Largura.Text)
    19.  
    20.         myConnection.Open()
    21.         myInsertCommand.ExecuteNonQuery()
    22.  
    23.         Dim myQuery As New OleDbCommand("SELECT Category, Number, Level, Durability, Skill, Luck, Options, Name FROM @Table", myConnection)
    24.  
    25.         myQuery.Parameters.AddWithValue("@Table", TabelaActual)
    26.  
    27.         Dim myDataReader As OleDbDataReader = myQuery.ExecuteReader(CommandBehavior.CloseConnection)
    28.  
    29.         While myDataReader.Read()
    30.             L1.Items.Add(String.Format("{0} {1} {2} {3} {4} {5} {6} - {7}", _
    31.                                        myDataReader("Category"), _
    32.                                        myDataReader("Number"), _
    33.                                        myDataReader("Level"), _
    34.                                        myDataReader("Durability"), _
    35.                                        myDataReader("Skill"), _
    36.                                        myDataReader("Luck"), _
    37.                                        myDataReader("Options"), _
    38.                                        myDataReader("Name")))
    39.             TabAux.Items.Add(myDataReader("Name"))
    40.         End While
    41.  
    42.         myDataReader.Close()
    You may need to make a few adjustments and you should add some exception handling. Also, you really should specify the column names in your INSERT statement.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  13. #13

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Resolved Re: Search button with databases

    Thks a lot Again!!! (You a pro X2)

    I prefer ADO because it is more simple and you have to write less lines, but with ADO u dont have the possiblity to create new DB and with ADO.NET u can. So i will have to surrender to the fact that in my case ADO.NET is best for the job!
    Last edited by Lasering; May 31st, 2006 at 06:18 PM.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Search button with databases

    From what I understand of ADO, ADO.NET is much more flexible. Like I said, there are numerous ways to achieve the same result. You could get away with writing less code than I have here, plus you can do a lot of the work visually in the designer if you like. The IDE will also do a lot for you you if you want it to, especially in 2005.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  15. #15

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: Search button with databases

    With the code that u give me before i could change and i was getting what i wanted till i try to change this ado code to ado.net:

    VB Code:
    1. Conect.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " & Chr(34) & Application.StartupPath & ActualDB & Chr(34))
    2. RS.Open(TabelaActual, Conect, ADODB.CursorTypeEnum.adOpenKeyset, ADODB.LockTypeEnum.adLockPessimistic)
    3. RS.Update()
    4. RS.Move(ActualIndex)
    5. RS.Fields.Item("Name").Value = NovoNome
    6. TabAux.Items(ActualIndex).Text = NovoNome
    7. RS.Save(TabelaActual)
    8. RS.Close()

    I hope u could help me again because i really need help!
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

  16. #16

    Thread Starter
    Fanatic Member Lasering's Avatar
    Join Date
    May 2006
    Location
    Lisboa
    Posts
    559

    Re: Search button with databases

    Thks but i've found a way to do that through UPDATE SQL Statement thks anyway for the help.
    Controls: XPCC|Quantum
    Windows API'sLINQ to XML SamplesRegex Tutorial

    Albert Einstein:
    "Imagination is more important than knowledge."
    "Everything should be made as simple as possible, but not simpler."
    "Great spirits have often encountered violent opposition from weak minds."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width