再建立項目的文件夾的bin文件夾下
'創建兩個文件
'一個是"myxml.XML"
'一個是"myXML.mdb"
'這裡面有一個表"users"三個字段名"nameid","age","faverity"
'再轉到圖形界面
'單擊"工具箱"的"數據"把"OleDbConnection"," OleDbComman","DataSet","OleDbDataAdapter"
'拉入窗體界面
'依次對這幾個控件進行操作
'我的文件夾是D:\vbproject\WindowsApplication1
'D:\vbproject\WindowsApplication1/bin/myxml.XML
'D:\vbproject\WindowsApplication1/bin/myXML.mdb
//======================准備結束==============
(1)首先在機子上要有.Net FrameWork
打開VS.Net依次展開:文件-新建-項目;
再選擇VISUAL-BASIC-項目-Windows應用程序
這就是所需要一個小界面
現在一步步的來寫代碼,完成它的功能
首先來完成添加功能:
我們目標是:通過這三個文本框(姓名,年齡,愛好)來添加到數據庫中。然後再把這個數據庫的表保存成XML格式
雙擊"添加"按鈕
在裡面寫下以下這些代:(VB寫的語言,不是C#)
If TextBox1.Text <> "" And TextBox2.Text <> "" And TextBox3.Text <> "" Then
'在三個文本框都不為空時,執行以下操作,
'否則出錯
Dim strsel As String
strsel = "select * from users where nameid='" & TextBox1.Text & "'"
'建立一個查詢字符串,看要要添加的姓名,是否已在數據中存在,如果不存在就可以添加
OleDbCommand1 = New OleDbCommand
Me.OleDbCommand1.CommandText = strsel
Me.OleDbCommand1.Connection = OleDbConnection1
OleDbConnection1.Open()
Try
'進行異常處理
Dim reader As OleDbDataReader = OleDbCommand1.ExecuteReader()
If reader.Read() Then
'通過DataReader來讀取,如果讀得到,表明數據在有這個姓名存在,不添加;
ListBox1.Items.Add("已經有該記錄!")
Else
reader.Close()
'要對數據庫進行操作,首先把DataReader關掉;
Dim insert As String
insert = "insert into users(nameid,age,faverity) values('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')"
'建立一個插入字符串
OleDbCommand1 = New OleDbCommand
Me.OleDbCommand1.CommandText = insert
Me.OleDbCommand1.Connection = Me.OleDbConnection1
Me.OleDbCommand1.ExecuteNonQuery()
ListBox1.Items.Add("添加成功!!")
'以下是把數據庫的數據保存為XML格式
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("請輸入完整!!")
End If
//==================接下來是對數據庫進行查詢===================
'雙擊"查找"按鈕
'寫入以下代碼
If TextBox4.Text <> "" Then
'還是和上面一樣如果文本框為空,就不執行,如果不為空,就執行下面的操作
ListBox1.Items.Clear()
'首先把listBox清空
Dim searchtext As String
searchtext = TextBox4.Text
OleDbConnection1.Open()
Dim selstring As String
selstring = "select * from users where nameid like '%" & searchtext & "%'"
' 建立查詢字符串,可以支持模糊查詢
OleDbCommand1 = New OleDbCommand
Me.OleDbCommand1.CommandText = selstring
Me.OleDbCommand1.Connection = OleDbConnection1
'以下幾句是顯示匹配的條數
DataSet1 = New DataSet
OleDbDataAdapter1 = New OleDbDataAdapter(selstring, OleDbConnection1)
OleDbDataAdapter1.Fill(DataSet1, "users")
ListBox1.Items.Add("共有" & DataSet1.Tables("users").Rows.Count & "條匹配的記錄")
ListBox1.Items.Add("-------------------------------------------------------------")
Try
'進行異常處理
Dim cmdreader As OleDbDataReader = OleDbCommand1.ExecuteReader()
While cmdreader.Read
'注意這裡要用while
'不然就無法進行循環,就只能進行一次查詢
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
//================再把來XML文檔,以XML形式顯示在ListBox裡面
雙擊"XML文檔" 寫進下面這些代碼:
ListBox1.Items.Clear()
' 清空listBox
Dim xtr As XmlTextReader = New XmlTextReader("myxml.XML")
'創建成一個XmlTextReader讀取"myxml.XML"文檔
While xtr.Read
Select Case (xtr.NodeType)
' 咱們用select case 形式來選擇XML節點類型
Case XmlNodeType.XMLDeclaration
'先從ListBox裡寫進xml聲明=====XMLdeclaration
ListBox1.Items.Add("<?XML version='1.0' encoding='gb2312'?>")
'再依次顯示節點的名稱,值
'包括根節點
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()
'關閉XMLTextReader
//========再把數據庫中所有數據顯示在ListBox中========
'雙擊"查看全部"按鈕
'寫入下面的一些代碼:
ListBox1.Items.Clear()
'這些代碼大家應該可以看得懂了
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()
//==========================再來進完成刪除按鈕=================
'雙擊"刪除"按鈕
'寫入以下的代碼
If TextBox4.Text <> "" Then
'如果不為空,進行以下操作
ListBox1.Items.Clear()
'先清空ListBox
OleDbConnection1.Open()
' 建立連接
Dim delstring As String
delstring = TextBox4.Text
Dim delsel As String
delsel = "select * from users where nameid='" & delstring & "'"
'創建查詢字符串
OleDbCommand1 = New OleDbCommand
Me.OleDbCommand1.CommandText = delsel
Me.OleDbCommand1.Connection = OleDbConnection1
Try
Dim selreader As OleDbDataReader = OleDbCommand1.ExecuteReader()
If Not selreader.Read Then
'假如讀不到,就表明數據庫無此數據,無法進行刪除操作
MessageBox.Show("數據庫中無該記錄!")
Else
selreader.Close()
'要對數據進行操作,必須首把DataReader關掉
Dim delrecord As String
delrecord = "delete * from users where nameid='" & delstring & "'"
'建立刪除字符串sql語句,以上都是
OleDbCommand1 = New OleDbCommand
Me.OleDbCommand1.CommandText = delrecord
Me.OleDbCommand1.Connection = OleDbConnection1
Me.OleDbCommand1.ExecuteNonQuery()
MessageBox.Show("刪除成功!!")
'再把進行刪除操作的數據庫,再次把數據庫中數據保存成XML文檔
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("請輸入你想刪除的記錄!")
End If
//======================= 重設按鈕=====
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
ListBox1.Items.Clear()
//============================關閉===============
me.Close()
'好了這個小軟件完成了,有問題的話留言
'本程序在.Net framework 2003創建
'已經進行過測試,完全可以運行