Create another project under the bin folder of the target folder
'Create two files
'One is "myxml. xml"
'One is "myxml. mdb"
'Here is a table named "users" with three field names "nameid", "Age", "faverity"
'Then go to the graphic interface
Click "data" in the "toolbox" to set "oledbconnection", "oledbcomman", "dataset", "oledbdataadapter"
'Pull the form Interface
'Perform operations on these controls in sequence
'My folder is D: \ vbproject \ windowsapplication1
'D: \ vbproject \ windowsapplication1/bin/myxml. xml
'D: \ vbproject \ windowsapplication1/bin/myxml. MDB
// =======================================================
(1) first, the. NET Framework must be available on the machine.
Open vs. NET and expand in sequence: file-New-project;
Select visual-Basic-project-Windows Application Program
This is a small interface.
Now, write it step by step Code To complete its functions
First, complete the adding function:
Our goal is to add these three text boxes (names, ages, hobbies) to the database. Then, save the table of the database in XML format.
Double-click "add ".
Write down the following generation: (the language written in VB, not C #)
If textbox1.text <> "" And textbox2.text <> "" And textbox3.text <> "" then
'If none of the three text boxes is blank, perform the following operations,
'Otherwise, an error occurs.
Dim strsel as string
Strsel = "select * from users where nameid = '" & textbox1.text &"'"
'Create a query string to check whether the name to be added already exists in the data. If it does not exist, add
Oledbcommand1 = new oledbcommand
Me. oledbcommand1.commandtext = strsel
Me. oledbcommand1.connection = oledbconnection1
Oledbconnection1.open ()
Try
'Handle exceptions
Dim reader as oledbdatareader = oledbcommand1.executereader ()
If reader. Read () then
'Read through datareader. If read, it indicates that the data exists with this name and is not added;
Listbox1.items. Add ("this record already exists! ")
Else
Reader. Close ()
'To operate the database, first turn off the datareader;
Dim insert as string
Insert = "insert into users (nameid, age, faverity) values ('" & textbox1.text & "', '" & textbox2.text & "', '" & textbox3.text &"')"
'Create an insert string
Oledbcommand1 = new oledbcommand
Me. oledbcommand1.commandtext = insert
Me. oledbcommand1.connection = me. oledbconnection1
Me. oledbcommand1.executenonquery ()
Listbox1.items. Add ("added successfully !! ")
'Below is to save the database data in XML format
Dataset1 = new dataset
Oledbdataadapter1 = new oledbdataadapter ("select * from users", oledbconnection1)
Oledbdataadapter1.fill (dataset1, "users ")
Dataset1.writexml ("myxml. xml ")
End if
Catch ex as exception
Listbox1.items. Add ("errors! ")
End try
Oledbconnection1.close ()
Else
MessageBox. Show ("Enter complete !! ")
End if
// ============================== Next, query the database ==================== =====
Double-click "Search ".
'Write the following code
If textbox4.text <> "" then
'Still the same as above. If the text box is empty, do not execute. If it is not empty, perform the following operations.
Listbox1.items. Clear ()
'First clear The ListBox
Dim searchtext as string
Searchtext = textbox4.text
Oledbconnection1.open ()
Dim selstring as string
Selstring = "select * from users where nameid like '%" & searchtext & "% '"
'Create a query string and support fuzzy query
Oledbcommand1 = new oledbcommand
Me. oledbcommand1.commandtext = selstring
Me. oledbcommand1.connection = oledbconnection1
'The following statements show the number of matched entries
Dataset1 = new dataset
Oledbdataadapter1 = new oledbdataadapter (selstring, oledbconnection1)
Oledbdataadapter1.fill (dataset1, "users ")
Listbox1.items. Add ("Total" & dataset1.tables ("users"). Rows. Count & "matched records ")
Listbox1.items. Add ("-------------------------------------------------------------")
Try
'Handle exceptions
Dim cmdreader as oledbdatareader = oledbcommand1.executereader ()
While cmdreader. Read
'Note that the while
'Otherwise, the query can only be performed once.
Listbox1.items. Add (cmdreader ("nameid"). tostring ())
Listbox1.items. Add (cmdreader ("Age"). tostring ())
Listbox1.items. Add (cmdreader ("faverity"). tostring ())
Listbox1.items. Add ("----------------------------------")
End while
Cmdreader. Close ()
Oledbconnection1.close ()
Catch ex as exception
Listbox1.items. Add ("errors ")
End try
Else
End if
// ========================== Then, display the XML document in The ListBox as XML.
Double-click "XML document" to write the following code:
Listbox1.items. Clear ()
'Clear ListBox
Dim XTR as xmltextreader = new xmltextreader ("myxml. xml ")
'Create an xmltextreader to read the "myxml. xml" document.
While XTR. Read
Select case (XTR. nodetype)
'Let's use the select case form to select the XML node type.
Case xmlnodetype. xmldeclaration
'Write the XML declaration ==== xmldeclaration from ListBox first
Listbox1.items. Add ("<? XML version = '1. 0' encoding = 'gb2312 '?> ")
'Then, the node name and value are displayed in sequence.
'Includes the root node
Case xmlnodetype. Element
Listbox1.items. Add ("<" & XTR. Name & "> ")
Case xmlnodetype. Text
Listbox1.items. Add (XTR. value)
Case xmlnodetype. endelement
Listbox1.items. Add ("</" & XTR. Name & "> ")
End select
End while
XTR. Close ()
'Close xmltextreader
// ========== Then display all the data in the database in ListBox ==========
'Double-click the "view all" button.
'Write the following code:
Listbox1.items. Clear ()
'The code is understandable.
Oledbconnection1.open ()
Dim selall as string
Selall = "select * from users"
Oledbcommand1 = new oledbcommand
Me. oledbcommand1.commandtext = selall
Me. oledbcommand1.connection = oledbconnection1
Try
Dim creader as oledbdatareader = oledbcommand1.executereader ()
While creader. Read
Listbox1.items. add ("name:" & creader ("nameid "). tostring () & "; age:" & creader ("Age "). tostring () & "; faverity:" & creader ("faverity "). tostring ())
End while
Creader. Close ()
Catch ex as exception
Listbox1.items. Add ("errors ")
End try
Oledbconnection1.close ()
// ===================================================== Click to complete the deletion ============
'Double-click the "delete" button.
'Write the following code
If textbox4.text <> "" then
'If it is not empty, perform the following operations:
Listbox1.items. Clear ()
'Clear ListBox first
Oledbconnection1.open ()
'Establish a connection
Dim delstring as string
Delstring = textbox4.text
Dim delsel as string
Delsel = "select * from users where nameid = '" & delstring &"'"
'Create a query string
Oledbcommand1 = new oledbcommand
Me. oledbcommand1.commandtext = delsel
Me. oledbcommand1.connection = oledbconnection1
Try
Dim selreader as oledbdatareader = oledbcommand1.executereader ()
If not selreader. Read then
'The database does not have this data and cannot be deleted if it cannot be read.
MessageBox. Show ("this record is not found in the database! ")
Else
Selreader. Close ()
'To operate the data, you must first turn off the datareader
Dim delrecord as string
Delrecord = "delete * from users where nameid = '" & delstring &"'"
'Create and delete a string SQL statement.
Oledbcommand1 = new oledbcommand
Me. oledbcommand1.commandtext = delrecord
Me. oledbcommand1.connection = oledbconnection1
Me. oledbcommand1.executenonquery ()
MessageBox. Show ("deleted successfully !! ")
'Then, save the deleted database as an XML document.
Dataset1 = new dataset
Oledbdataadapter1 = new oledbdataadapter ("select * from users", oledbconnection1)
Oledbdataadapter1.fill (dataset1, "users ")
Dataset1.writexml ("myxml. xml ")
End if
Catch ex as exception
MessageBox. Show (ex. Message)
Finally
Oledbconnection1.close ()
End try
Else
MessageBox. Show ("Enter the record you want to delete! ")
End if
// ================================== Reset button ======
Textbox1.text = ""
Textbox2.text = ""
Textbox3.text = ""
Listbox1.items. Clear ()
// =================================================== ======
Me. Close ()
'Okay, this small software is complete. If you have any questions, leave a message.
'This program is created in. NET Framework 2003.
'Has been tested and can be run completely