作者:fx_blog(csdn)
日期:2005-4-27
内容:.net与xml的小软件(100多行左右)
注意:如果转贴,请一定注明出处,以及作者
//=================================开始===============
//======================准备工作===========================
' 再建立项目的文件夹的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("添加成功!!")