再建立项目的文件夹的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创建
'已经进行过测试,完全可以运行