毕业设计超市系统(四)数据访问层
接着上一篇,由于用到的是linq来做数据处理,所以我为数据访问层建立一个名为DBBase的类它有一个Marketdc的属性为子类所有代码如下:
DBBase
Imports SiYaoMin.ProForLeave.MyLINQ
Public Class DBBase
Protected _marketdc As MarketDataContext
Public Sub New()
_marketdc = New MarketDataContext(ConnectionInfo.ConnectionString)
End Sub
Protected Property Marketdc() As MarketDataContext
Get
Return _marketdc
End Get
Set(ByVal value As MarketDataContext)
_marketdc = value
End Set
End Property
Public Sub UpdataSource()
_marketdc.SubmitChanges()
End Sub
End Class
Public Class DBBase
Protected _marketdc As MarketDataContext
Public Sub New()
_marketdc = New MarketDataContext(ConnectionInfo.ConnectionString)
End Sub
Protected Property Marketdc() As MarketDataContext
Get
Return _marketdc
End Get
Set(ByVal value As MarketDataContext)
_marketdc = value
End Set
End Property
Public Sub UpdataSource()
_marketdc.SubmitChanges()
End Sub
End Class
DBBase类中涉及到一个ConnectionInfo类,它的代码如下:
ConnectionInfo类
Public Class ConnectionInfo
Private Shared _DataSource As String
Public Shared Property DataSource() As String
Get
Return _DataSource
End Get
Set(ByVal value As String)
If value.Trim = String.Empty Then
_DataSource = "."
End If
_DataSource = value
End Set
End Property
Private Shared _DataBaseName As String
Public Shared Property DataBaseName() As String
Get
Return _DataBaseName
End Get
Set(ByVal value As String)
_DataBaseName = value
End Set
End Property
Private Shared _Trusted_Connection As Boolean
Public Shared Property Trusted_Connection() As Boolean
Get
Return _Trusted_Connection
End Get
Set(ByVal value As Boolean)
_Trusted_Connection = value
End Set
End Property
Private Shared _UserID As String
Public Shared Property UserID() As String
Get
Return _UserID
End Get
Set(ByVal value As String)
_UserID = value
End Set
End Property
Private Shared _Password As String
Public Shared Property Password() As String
Get
Return _Password
End Get
Set(ByVal value As String)
_Password = value
End Set
End Property
Public Shared ReadOnly Property ConnectionString() As String
Get
If _Trusted_Connection Then
Return "Data Source=" + _DataSource + ";Initial Catalog=" + _DataBaseName + ";trusted_connection=true"
Else
Return "Data Source=" + _DataSource + ";Initial Catalog=" + _DataBaseName + ";Persist Security Info=True;User ID=" + _UserID + ";Password=" + _Password
End If
End Get
End Property
End Class
主要是生成数据库连接字符串,这里只写了ms sql server的数据库连接字符串。
我采用每一个类只有一种单独的功能的方法。
贴个图:
由于数据访问涉及到了增、删、改、查。下面各有一个例子:
添加
Imports SiYaoMin.ProForLeave.MyLINQ
Public Class AddGoodsData
Inherits DBBase
Private _goods As GoodsDetail
Public Property Goods() As GoodsDetail
Get
Return _goods
End Get
Set(ByVal value As GoodsDetail)
_goods = value
End Set
End Property
Public Function Add() As Integer
Dim a As Integer = _marketdc.AddGoodsDetail(_goods.goodsid, _goods.goodsname, _goods.types, _goods.price, _goods.shelflife, _goods.madein, _goods.company, _goods.info)
Return a
End Function
End Class
Public Class AddGoodsData
Inherits DBBase
Private _goods As GoodsDetail
Public Property Goods() As GoodsDetail
Get
Return _goods
End Get
Set(ByVal value As GoodsDetail)
_goods = value
End Set
End Property
Public Function Add() As Integer
Dim a As Integer = _marketdc.AddGoodsDetail(_goods.goodsid, _goods.goodsname, _goods.types, _goods.price, _goods.shelflife, _goods.madein, _goods.company, _goods.info)
Return a
End Function
End Class
删除
Imports SiYaoMin.ProForLeave.MyLINQ
Public Class DeleteLoginInName
Inherits DBBase
Private _logininfo As Login
Public Property LoginInfo() As Login
Get
Return _logininfo
End Get
Set(ByVal value As Login)
_logininfo = value
End Set
End Property
Public Function Delete() As Integer
Dim a As Integer = _marketdc.DeleteLoginInfo(_logininfo.loginname)
Return a
End Function
End Class
Public Class DeleteLoginInName
Inherits DBBase
Private _logininfo As Login
Public Property LoginInfo() As Login
Get
Return _logininfo
End Get
Set(ByVal value As Login)
_logininfo = value
End Set
End Property
Public Function Delete() As Integer
Dim a As Integer = _marketdc.DeleteLoginInfo(_logininfo.loginname)
Return a
End Function
End Class
更新
Imports SiYaoMin.ProForLeave.MyLINQ
Public Class UpdateGoodsData
Inherits DBBase
Private _goods As GoodsDetail
Public Property Goods() As GoodsDetail
Get
Return _goods
End Get
Set(ByVal value As GoodsDetail)
_goods = value
End Set
End Property
Public Function Update() As Integer
Dim a = _marketdc.UpdateGoodsDetail(_goods.goodsid, _goods.goodsname, _goods.types, _goods.price, _goods.shelflife, _goods.madein, _goods.company, _goods.info)
Return a
End Function
End Class
Public Class UpdateGoodsData
Inherits DBBase
Private _goods As GoodsDetail
Public Property Goods() As GoodsDetail
Get
Return _goods
End Get
Set(ByVal value As GoodsDetail)
_goods = value
End Set
End Property
Public Function Update() As Integer
Dim a = _marketdc.UpdateGoodsDetail(_goods.goodsid, _goods.goodsname, _goods.types, _goods.price, _goods.shelflife, _goods.madein, _goods.company, _goods.info)
Return a
End Function
End Class
查询
Imports SiYaoMin.ProForLeave.MyLINQ
Public Class GetGoodsInfo
Inherits DBBase
Public Function GetAllGoods() As IList
Return _marketdc.GetAllGoodsInfo().ToList()
End Function
Public Function GetFilterGoods() As IList
Return _marketdc.FilterGoodsInfo(_goods.goodsname, _goods.company, _goods.types).ToList
End Function
Public Function GetGoodsCompanyList() As IList
Return _marketdc.GetGoodsComany().ToList
End Function
Public Function GetGoodsNameList() As IList
Return _marketdc.GetGoodsNames().ToList
End Function
Public Function GetGoodsTypesList() As IList
Return _marketdc.GetGoodsTypes().ToList
End Function
Public Function GetGoodsNamesListInType() As IList
Return _marketdc.GetGoodsNamesInTypes(_goods.types).ToList
End Function
Public Function GetGoodsTypeForSale() As IList
Return _marketdc.GetGoodsTypesForSale().ToList
End Function
Public Function GetGoodsPriceForSaleInName()
Return _marketdc.GetGoodsPriceListInName(_goods.goodsname).ToList
End Function
Public Function GetGoodsPriceForSaleInGoodsId()
Return _marketdc.GetGoodsPriceListInGoodsId(_goods.goodsid).ToList
End Function
Private _goods As GoodsDetail
Public Property Goods() As GoodsDetail
Get
Return _goods
End Get
Set(ByVal value As GoodsDetail)
_goods = value
End Set
End Property
End Class
Public Class GetGoodsInfo
Inherits DBBase
Public Function GetAllGoods() As IList
Return _marketdc.GetAllGoodsInfo().ToList()
End Function
Public Function GetFilterGoods() As IList
Return _marketdc.FilterGoodsInfo(_goods.goodsname, _goods.company, _goods.types).ToList
End Function
Public Function GetGoodsCompanyList() As IList
Return _marketdc.GetGoodsComany().ToList
End Function
Public Function GetGoodsNameList() As IList
Return _marketdc.GetGoodsNames().ToList
End Function
Public Function GetGoodsTypesList() As IList
Return _marketdc.GetGoodsTypes().ToList
End Function
Public Function GetGoodsNamesListInType() As IList
Return _marketdc.GetGoodsNamesInTypes(_goods.types).ToList
End Function
Public Function GetGoodsTypeForSale() As IList
Return _marketdc.GetGoodsTypesForSale().ToList
End Function
Public Function GetGoodsPriceForSaleInName()
Return _marketdc.GetGoodsPriceListInName(_goods.goodsname).ToList
End Function
Public Function GetGoodsPriceForSaleInGoodsId()
Return _marketdc.GetGoodsPriceListInGoodsId(_goods.goodsid).ToList
End Function
Private _goods As GoodsDetail
Public Property Goods() As GoodsDetail
Get
Return _goods
End Get
Set(ByVal value As GoodsDetail)
_goods = value
End Set
End Property
End Class


浙公网安备 33010602011771号