分享
 
 
 

分层体系结构(MVC)二

王朝vc·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

二、EntityClass

EntityClass中的每一个文件对应数据库中的一张表,对应其字段,以及对表的操作的封装,如查询,增加、删除等。

所有的EntityClass继承了一个基类BaseEntity,基类的代码如下,主要是一些接口

Imports System.Data

Namespace EntityClass

Public Class BaseEntity

Public strSql As String

Public database As Data.DataFactory

Public dt As DataTable

'构造函数

Public Sub New()

Me.database = New Data.DataFactory

Me.dt = New DataTable

End Sub

'应该做成抽象的abstract

Public Function Load() As Integer

End Function

Public Function Creat() As Integer

End Function

Public Function Updata() As Integer

End Function

Public Function Delete() As Integer

End Function

Public Function GetDataOnTable() As DataTable

Me.dt = Me.database.DataTableResult(Me.strSql)

Return dt

End Function

Public Function GetOneObj() As Object

Return Me.database.ReadOneLog(Me.strSql)

End Function

Public Function ExecuteSql() As Integer

Return Me.database.ExecuteNonQuery(Me.strSql)

End Function

End Class

End Namespace

这里以对用户表的操作为例,来说明EntityClass的结构,代码如下:

Imports System.Data

Namespace EntityClass

Public Class S_User

Inherits BaseEntity

Const ARRAYLENGTH = 100

Public Id_I As String

' 用户名

Public UserName_S As String

'用户密码

Public Password_S As String

'add by gb 20050725

Dim array1(ARRAYLENGTH) As String

Dim array2(ARRAYLENGTH, 1) As String

Dim sqlarray(ARRAYLENGTH, 1) As String

Dim len1 As Integer = 0

Dim len2 As Integer = 0

Dim sqllen As Integer = 0

Private mydataset As New DataSet

'对Array2赋初值

Private Sub SetArray2()

array2(0, 0) = "Id_I"

array2(1, 0) = "UserName_S"

array2(2, 0) = "Password_S"

array2(0, 1) = Id_I

array2(1, 1) = UserName_S

array2(2, 1) = Password_S

End Sub

'通过读取数据库中的列名,对array1赋值

Private Sub SetDBColoum()

LoadDynamicDataset(mydataset)

Dim m_i As Integer = 0

Dim m_len As Integer = 0

'm_len = Me.mydataset.Tables.Item(0).Select.Length()

m_len = Me.mydataset.Tables.Item(0).Columns.Count

'For m_i = 0 To ARRAYLENGTH

' If Not Me.mydataset.Tables.Item(0).Columns(m_i) Is Nothing Then

' m_len = m_len + 1

' End If

'Next

'MsgBox("m_len" + m_len.ToString)

For m_i = 0 To m_len - 1

array1(m_i) = Me.mydataset.Tables.Item(0).Columns(m_i).ColumnName.ToString

Next

'array1(0) = Me.mydataset.Tables.Item(0).Columns(0).ColumnName.ToString

'array1(0) = "Id_I"

'array1(1) = "Password_S"

End Sub

'通过array1和array2的处理,对sqlarray赋值;最后的结果是sqlarray中,依次对应数据库中的列的值

Public Function SetDynamicColoum()

SetArray2()

SetDBColoum()

len1 = 0

len2 = 0

sqllen = 0

Dim i As Integer

Dim j As Integer = 0

For i = 0 To ARRAYLENGTH

If Not array1(i) Is Nothing Then

len1 = len1 + 1

Else

Exit For

End If

Next

'MsgBox("len1=" + len1.ToString)

For i = 0 To ARRAYLENGTH

If Not array2(i, 0) Is Nothing Then

len2 = len2 + 1

Else

Exit For

End If

Next

'MsgBox("len2=" + len2.ToString)

For i = 0 To len1 - 1

For j = 0 To len2 - 1

If array1(i) = array2(j, 0) Then

sqlarray(i, 0) = array2(j, 0)

sqlarray(i, 1) = array2(j, 1)

Exit For

End If

Next

Next

For i = 0 To ARRAYLENGTH

If Not sqlarray(i, 0) Is Nothing Then

sqllen = sqllen + 1

Else

Exit For

End If

Next

'For i = 0 To sqllen - 1

' MsgBox(sqlarray(i, 1))

'Next

'MsgBox(sqlarray(0))

'MsgBox(sqlarray(1))

End Function

'得到对应数据库中的表的信息

Public Sub LoadDynamicDataset(ByRef dataset As DataSet)

MyBase.strSql = "select * from S_User"

MyBase.database.SelectSqlSrvRows(dataset, MyBase.strSql)

End Sub

'add end

Public Function Count() As Integer

Return Convert.ToInt32(MyBase.database.ReadOneLog("select count(*) from S_User"))

End Function

Public Sub New()

' TODO: 在此处添加构造函数逻辑

Me.Init()

SetDynamicColoum()

End Sub

Public Function Init()

Me.Id_I = ""

Me.UserName_S = ""

Me.Password_S = ""

End Function

'根据id得到一条用户信息

Public Shadows Function Load() As Integer

MyBase.strSql = "select * from S_User where Id_I=" + Me.Id_I

Dim dt As DataTable = MyBase.database.DataTableResult(strSql)

If (dt.Rows.Count > 0) Then

If FindInSqlarray("UserName_S", sqlarray) > 0 Then

Me.UserName_S = dt.Rows(0)("UserName_S").ToString()

End If

If FindInSqlarray("Password_S", sqlarray) > 0 Then

Password_S = dt.Rows(0)("Password_S").ToString()

End If

'Me.UserName_S = dt.Rows(0)("UserName_S").ToString()

'Me.Password_S = dt.Rows(0)("Password_S").ToString()

Return dt.Rows.Count

Else

Return 0

End If

End Function

Public Function GetUser(ByVal UserName As String, ByVal Password As String) As Integer

'MsgBox(3)

MyBase.strSql = "select * from S_User where UserName_S='" + UserName + "' and Password_S='" + Password + "'"

Dim dt As DataTable = MyBase.database.DataTableResult(strSql)

If (dt.Rows.Count > 0) Then

Me.Id_I = dt.Rows(0)("ID_I").ToString()

Me.UserName_S = dt.Rows(0)("UserName_S").ToString()

Me.Password_S = dt.Rows(0)("Password_S").ToString()

Return dt.Rows.Count

Else

Return 0

End If

End Function

'添加用户

Public Shadows Function CreatUser() As Integer

Dim i As Integer

Dim col As String

Dim colval As String

For i = 1 To sqllen - 2

col = col + sqlarray(i, 0) + ","

colval = colval + sqlarray(i, 1) + "','"

Next

col = col + sqlarray(sqllen - 1, 0)

colval = colval + sqlarray(sqllen - 1, 1)

MyBase.strSql = "insert into S_User (" + col + ")" + "values ('" + colval + "')"

'MyBase.strSql = "insert into S_User (" + _

'"UserName_S,Password_S" + ")" + _

'"values ('" + UserName_S + "','" + Password_S + "')"

'MsgBox("sql2=" + strSql)

Return MyBase.database.ExecuteNonQuery(MyBase.strSql)

End Function

'更新用户

Public Shadows Function Updata() As Integer

Dim i As Integer

Dim str As String

For i = 1 To sqllen - 2

str = str + sqlarray(i, 0) + "='" + sqlarray(i, 1) + "',"

Next

str = str + sqlarray(sqllen - 1, 0) + "='" + sqlarray(sqllen - 1, 1) + "'"

MyBase.strSql = "update S_User set " + str + " where ID_I=" + Me.Id_I

MsgBox(strSql)

'MyBase.strSql = "update S_User set UserName_S='" + Me.UserName_S + "',Password_S='" + Me.Password_S + "' where ID_I=" + Me.Id_I

Return MyBase.database.ExecuteNonQuery(MyBase.strSql)

End Function

'删除用户

Public Shadows Function Delete() As Integer

MyBase.strSql = "delete from S_User where Id_I=" + Me.Id_I.ToString()

Return MyBase.database.ExecuteNonQuery(MyBase.strSql)

End Function

Public Sub loadusers(ByRef dataset As DataSet)

MyBase.strSql = "select * from S_User"

MyBase.database.SelectSqlSrvRows(dataset, MyBase.strSql)

End Sub

Public Function setEntity(ByRef myParameter As business.login.UserParameter) As Integer

Try

Me.Id_I = myParameter.get_ID

Me.UserName_S = myParameter.get_UserName

Me.Password_S = myParameter.get_Password

SetDynamicColoum()

Catch ex As Exception

Return 0

End Try

Return 1

End Function

Public Function FindInSqlarray(ByVal prop As String, ByVal sqlarray(,) As String) As Integer

Dim m As Integer = 0

For m = 0 To ARRAYLENGTH

If prop.Equals(sqlarray(m, 0)) Then

Return 1

End If

Next

Return 0

End Function

'根据sql语句查询

Public Function GetObjByQry(ByVal str As String, ByRef mp() As business.login.UserParameter) As Integer

MyBase.strSql = "select * from " + "S_User " + str

Dim dt As DataTable = MyBase.database.DataTableResult(strSql)

Dim m_i As Integer

ReDim mp(dt.Rows.Count - 1)

If (dt.Rows.Count > 0) Then

For m_i = 0 To dt.Rows.Count() - 1

mp(m_i) = New business.login.UserParameter

If FindInSqlarray("UserName_S", sqlarray) > 0 Then

mp(m_i).set_UserName(dt.Rows(m_i)("UserName_S").ToString())

End If

If FindInSqlarray("Password_S", sqlarray) > 0 Then

mp(m_i).set_Password(dt.Rows(0)("Password_S").ToString())

End If

Next

Else

Return 0

End If

End Function

End Class

End Namespace

可以看到,代码中定义了一系列的属性值,对应于用户表中的各个字段。还有对如添加用户,删除用户等各种操作的封装,调用了Data层中的方法,并且向以后将要涉及到的Business层提供了接口。所有的sql语句都是在这里拼写的。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有