首页 > 编程 > .NET > 正文

转:Visual Basic.NET快速开发MIS系统(1)

2024-07-10 13:02:21
字体:
来源:转载
供稿:网友
注册会员,创建你的web开发资料库,visual basic.net快速开发mis系统

2002-11-29· ·黎宇 ··yesky

  【摘 要】 本文介绍微软最新技术visual basic.net在数据库开发方面的应用。结合数据库系统开发的知识,介绍了物理表操作的方法,利用visual basic.net的面向对象的特征,利用类的继承知识,简化了数据库系统开发过程。

  引言

  以前版本的visual basic虽然号称自己是一种oop(面向对象)编程语言,但却不是一个地地道道的oop编程语言,最多只是半个面向对象的编程语言。但visual basic.net已经是一种完全的面向对象的编程语言。他支持面向对象的所有基本特征:继承、多态和重载。这使得以前在visual basic中很难或根本实现不了的问题,在visual basic.net中可以顺利的用简单的方法实现。

  自定义数据操作类

  定义一个数据访问的基类,并编写有关数据库操作的必要方法。

  定义一个数据访问类,类名为cdata。定义连接oracle数据库的方法connoracle,获取数据集的方法getdataset, 获取物理表的方法getdatatable, 向物理表中插入一行数据的方法insert, 向物理表中删除数据的方法delete, 向物理表中更新数据的方法update。其实现方法不是本文的重点,在此仅给出代码,不作详细分析。代码如下:

public class cdatabase
dim olecnndb as new oledbconnection()

'连接oracle数据库,servername:服务器名,userid:用户名,userpwd:用户密码
public function connoracle(byval servername as string, byval userid as string, byval userpwd as string) as oledbconnection

 dim olecnndb as new oledbconnection()
 with olecnndb
  .connectionstring = "provider=msdaora.1;password='" & userpwd & "';user id='" & userid & "';data source='" & servername & "'"
  try
   .open()
  catch er as exception
   msgbox(er.tostring)
  end try
 end with
 molecnndb = olecnndb
 return olecnndb
end function

'获取数据集。tablename:表名,strwhere:条件
public overloads function getdataset(byval tablename as string, byval strwhere as string) as dataset

 dim strsql as string
 dim mydataset as new dataset()
 dim myoledataadapter as new oledbdataadapter()

 myoledataadapter.tablemappings.add(tablename, tablename)
 strsql = "select * from " & tablename & " where " & strwhere

 myoledataadapter.selectcommand = new oledbcommand(strsql, molecnndb)
 try
  myoledataadapter.fill(mydataset)
 catch er as exception
  msgbox(er.tostring)
 end try
 return mydataset
end function

'获取物理表。tablename:表名
public overloads function getdatatable(byval tablename as string) as datatable
 dim mydataset as new dataset()
 mydataset = getdataset(tablename)
 return mydataset.tables(0)
end function

'获取物理表。tablename:表名,strwhere:条件
public overloads function getdatatable(byval tablename as string, byval strwhere as string) as datatable

 dim mydataset as new dataset()
 mydataset = getdataset(tablename, strwhere)
 return mydataset.tables(0)
end function

 '向物理表中插入一行数据。tablename:表名,value:行数据,begincolumnindex:开始列
public overloads function insert(byval tablename as string, byval value as object, optional byval begincolumnindex as int16 = 0) as boolean

 dim mydataadapter as new oledbdataadapter()
 dim strsql as string
 dim mydataset as new dataset()
 dim drow as datarow
 dim i, len as int16

 strsql = "select * from " & tablename
 mydataadapter.selectcommand = new oledbcommand(strsql, molecnndb)
 dim custcb as oledbcommandbuilder = new oledbcommandbuilder(mydataadapter)
 mydataset.tables.add(tablename)
 mydataadapter.fill(mydataset, tablename)
 drow = mydataset.tables(tablename).newrow
 len = value.length
 for i = begincolumnindex to len - 1
  if not (isdbnull(value(i)) or isnothing(value(i))) then
   drow.item(i) = value(i)
  end if
 next
 mydataset.tables(tablename).rows.add(drow)
 try
  mydataadapter.update(mydataset, tablename)
 catch er as exception
  msgbox(er.tostring)
  return false
 end try
 mydataset.tables.remove(tablename)
 return true
end function

'更新物理表的一个字段的值。strsql:查询语句,fieldname_value:字段及与对应的值
public overloads sub update(byval strsql as string, byval fieldname_value as string)

 dim mydataadapter as new oledbdataadapter()
 dim mydataset as new dataset()
 dim drow as datarow
 dim tablename, fieldname as string
 dim value as object
 dim a() as string

 a = strsql.split(" ")
 tablename = a(3)
 a = fieldname_value.split("=")
 fieldname = a(0).trim
 value = a(1)
 mydataadapter.selectcommand = new oledbcommand(strsql, molecnndb)
 dim custcb as oledbcommandbuilder = new oledbcommandbuilder(mydataadapter)
 mydataset.tables.add(tablename)
 mydataadapter.fill(mydataset, tablename)
 drow = mydataset.tables(tablename).rows(0)
 if value <> nothing then
  drow.item(fieldname) = value
 end if
 try
  mydataadapter.update(mydataset, tablename)
  mydataset.tables.remove(tablename)
 catch er as exception
  msgbox(er.tostring)
 end try
end sub

'删除物理表的数据。tablename:表名,strwhere:条件
public overloads sub delete(byval tablename as string, byval strwhere as string)
 dim myreader as oledbdatareader
 dim mycommand as new oledbcommand()
 dim strsql as string
 strsql = "delete from " & tablename & " where " & strwhere
 mycommand.connection = molecnndb
 mycommand.commandtext = strsql
 try
  myreader = mycommand.executereader()
  myreader.close()
 catch er as exception
  msgbox(er.tostring)
 end try
end sub
end class  

  定义一操作数据库中物理表的类cdata,此类继承cdatabase,即:

public class cdata:inherits cdatabase

  此类应该由供用户提供所操作的物理表的表名,指定了表名就可取得该表的所有性质。该表主要完成插入、删除、更新功能。定义其属性、方法如下:

  申明类cdata的变量:

'所要操作的表名
private shared updatetablename as string
'所要操作的表对象
public shared updatedatatable as new datatable()
'对应表的一行数据197
public shared objfields() as object
'表的字段数
public shared fieldcount as int16
'主关键字。我们假设每个物理表都有一个主关键字字段fsystemid
public shared systemid as string

  说明:shared 关键字指示一个或多个被声明的编程元素将被共享。共享元素不关联于某类或结构的特定实例。可以通过使用类名或结构名称或者类或结构的特定实例的变量名称限定共享元素来访问它们。

  申明类cdata的属性updatetable,当向updatetable赋给了一个已知表的表名,就可确定表的字段数,定义出数据行。这里,先打开表,再重新定义数据行.

public property updatetable() as string
 get
  updatetable = updatetablename
 end get
 set(byval value as string)
  updatetablename = value.trim
  updatedatatable = db.getdatatable(updatetablename)
  updatetablefieldnames = updatedatatable.clone
  fieldcount = updatedatatable.columns.count
  redim objfields(fieldcount - 1)
 end set
end property

'删除由主关键值fsystemid指定的数据行
public sub delete()
 dim strsql as string

 strsql = "delete from " & updatetablename & " where fsystemid=" & systemid
 db.delete(strsql)
 updatedatatable.rows.remove(getrow)
end sub

'向表updatetablename中插入一行数据。数据由objfields给出
public function insert() as boolean
 db.insert(updatetablename, objfields)
end function

'更新表updatetablename所指定的行
public shadows sub update()
 dim setfield as string
 dim i as int16

 for i = 1 to fieldcount - 1
  setfield = updatetablefieldnames.columns(i).columnname & "=" & objfields(i)
  updatefield(setfield)
 next
end sub

public sub updatefield(byval setfield as string)
 dim strsql as string
 strsql = "select * from " & updatetablename & " where fsystemid= " & systemid
 db.update(strsql, setfield)
end sub

'填充网络数据
public overloads sub fillgrid(byval gridname as datagrid)
 gridname.datasource = updatedatatable
end sub

'把数据网格的当前行数据定写入到输入控件中
public sub datagridtotext(byval frm as form)
 dim rowindex, i as int16
 dim value
 dim obj as control
 dim datagrid as new datagrid()
 if fieldcount = 0 then exit sub
  for each obj in frm.controls
   if obj.gettype.name = "datagrid" then
    datagrid = obj
    exit for
   end if
  next
  rowindex = datagrid.currentrowindex
  for i = 1 to fieldcount - 1
   value = datagrid.item(rowindex, i)
   if isdbnull(value) = true then
    value = ""
   end if
   for each obj in frm.controls '
    if obj.tabindex = i then
     obj.text = value
     exit for
    end if
   next
  next
end sub


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表