解释:
三层结构:表示层,业务层,数据层
1)数据层:代表物理数据库。
2)业务层:负责数据层与表示层之间的数据传输。
3)表示层:应用程序的客户端,它通过业务层来访问数据库。
表示层所操作的是驻留在内存中的本地数据,当需要更新数据库数据时,要通过业务层提供的更新方法实现。这样可以大大提高应用程序的性能,提高了编程的灵活性。
―――――――――――――――
1)数据层:代表物理数据库 ( Module 里定义全局模块)
Module Global
Public constr As String = "Provider=SQLOLEDB.1;Data Source=(local);Initial Catalog=Store;User ID=sa;Password=sa"
End Module
―――――――――――――――
2)业务层:负责数据层与表示层之间的数据传输。(Module 里定义全局模块)
Imports System.Data
Imports System.Data.OleDb
――――――
Module DataBase
举例一:
Public Function GetESDataSource() As DataTable
'得到显示某表的 DataSource
Dim strSql As String
Try
con = New OleDbConnection(constr)
con.Open()
strSql = "select语句"
dap = New OleDbDataAdapter(strSql, con)
Dim tb As New DataTable
dap.Fill(tb)
Return tb
Catch ex As Exception
MsgBox(ex.Message.ToString)
Finally
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Function
―――――――
举例二:
Public Function GetStoreItemGoodsNum(ByVal Good As String, ByVal store As String) As Integer
'返回指定仓库 指定物品的数量
Try
con = New OleDbConnection(constr)
If con.State = ConnectionState.Open Then
con.Close()
End If
con.Open()
Dim sqlstr As String = "select iGoodsNum from TMM_StoreItem where cStoreID=N'" & store & "' and cGoodsID=N'" & Good & "'"
cmd = New OleDbCommand(sqlstr, con)
Dim read As OleDbDataReader = cmd.ExecuteReader()
If Not read.HasRows Then
Return 0
Else
read.Read()
Return read.Item(0)
End If
Return -1
Catch ex As Exception
MsgBox(ex.Message.ToString)
Return -1
Finally
If con.State = ConnectionState.Open Then
con.Close()
End If
End Try
End Function
End Module
――――――
3)表示层:应用程序的客户端,它通过业务层来访问数据库。
举例:调用业务层的举例二:
Dim DBOutNum As Integer = GetStoreItemGoodsNum(goods, outstore)
Dim DBEnterNum As Integer = GetStoreItemGoodsNum(goods, enterstore)
//注意里面的参数
If num > DBOutNum Then
MsgBox("调出数量大于库存数量!", MsgBoxStyle.Critical, Me.Text)
Return
Else
InsertChangeStore(id, goods, num, outstore, enterstore, eDate, iDate)
If num = DBOutNum Then
flag = DeleteStoreItem(goods, outstore)
ElseIf num < DBOutNum Then
flag = UpdateStoreItem(goods, DBOutNum - num, outstore)
End If
If DBEnterNum = 0 Then
flag = InsertStoreItem(goods, num, enterstore)
Else
flag = UpdateStoreItem(goods, DBEnterNum + num, enterstore)
End If
MsgBox("操作成功!", MsgBoxStyle.Information, Me.Text)
End If