收费系统需要这样一个功能:首先用DataGridView浏览所有用户信息,然后可以删除指定行的用户信息,最后更新这一变化。界面如图:
<span style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px">'查看改用户是否正在被使用,若正在使用则不能删除 If Trim(DGVuser.CurrentRow.Cells(0).Value) = frmLogin.txtUID.Text Then MsgBox("该用户正在使用,不能删除!","正在使用") Exit Sub End If</span>
2、然后在删除指定数据行:
- <SPANstyle="FONT-FAMILY:KaiTi_GB2312;FONT-SIZE:18px">'删除datagridview中的数据行
- 'DGVuser.Rows.Remove(DGVuser.CurrentRow)也可
- DGVuser.Rows.RemoveAt(DGVuser.CurrentRow.Index)</SPAN>
<span style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px"> '删除datagridview中的数据行 'DGVuser.Rows.Remove(DGVuser.CurrentRow) 也可 DGVuser.Rows.RemoveAt(DGVuser.CurrentRow.Index)</span>
3、建立一个模板类,用于传递数据:
- <SPANstyle="FONT-FAMILY:KaiTi_GB2312;FONT-SIZE:18px">PublicClassdm_datatable
- Private_dtAsDataTable
- PublicPropertyDTAsDataTable
- Get
- Return_dt
- EndGet
- Set(ByValvalueAsDataTable)
- _dt=value
- EndSet
- EndProperty
- EndClass</SPAN>
<span style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px">Public Class dm_datatable Private _dt As DataTable Public Property DT As DataTable Get Return _dt End Get Set(ByVal value As DataTable) _dt = value End Set End Property End Class</span>
- <SPANstyle="WHITE-SPACE:normal"><SPANstyle="FONT-FAMILY:KaiTi_GB2312;FONT-SIZE:18px"></SPAN></SPAN>
<span style="WHITE-SPACE: normal"><span style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px"> </span></span>4、用表示层,用模板类传递数据:
- <SPANstyle="FONT-FAMILY:KaiTi_GB2312;FONT-SIZE:18px">DimdtAsNewDataModel.dm_datatable
- dt.DT=DGVuser.DataSource</SPAN>
<span style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px">Dim dt As New DataModel.dm_datatable dt.DT = DGVuser.DataSource</span>
- <SPANstyle="WHITE-SPACE:normal"><SPANstyle="FONT-FAMILY:KaiTi_GB2312;FONT-SIZE:18px"></SPAN></SPAN>
<span style="WHITE-SPACE: normal"><span style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px"> </span></span>5、在数据访问层,编写修改数据库的函数:
- <SPANstyle="FONT-FAMILY:KaiTi_GB2312;FONT-SIZE:18px">'删除指定用户
- PublicFunctiondeleteUser(ByValdm_dtAsDataModel.dm_datatable)AsBoolean
- DimconnStringAsString=_
- "PersistSecurityInfo=true;DataSource=192.168.24.158;InitialCatalog=CR_Charge_SYS;UserID=sa;PWD=123456"
- DimsqlconnAsNewsqlConnection(connString)
- DimsqltxtAsString="select*fromUser_info"
- DimsqlcmdAsNewsqlCommand(sqltxt,sqlconn)
- DimdaAsNewsqlDataAdapter(sqlcmd)
- DimdsAsNewDataSet
- DimdtAsNewDataTable
- da.UpdateCommand=sqlcmd
- DimmybuilderAsNewsqlCommandBuilder(da)
- Try
- dt=dm_dt.DT.Copy
- ds.Tables.Add(dt)
- sqlconn.Open()
- da.Update(ds,"User_info")
- ReturnTrue
- CatchexAsException
- MsgBox(ex.Message)
- ReturnFalse
- Finally
- sqlconn.Close()
- sqlcmd=Nothing
- EndTry
- ReturnNothing
- EndFunction</SPAN>
<span style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px">'删除指定用户 Public Function deleteUser(ByVal dm_dt As DataModel.dm_datatable) As Boolean Dim connString As String = _ "Persist Security Info=true;Data Source=192.168.24.158;Initial Catalog=CR_Charge_SYS;User ID=sa;PWD=123456" Dim sqlconn As New sqlConnection(connString) Dim sqltxt As String = "select * from User_info" Dim sqlcmd As New sqlCommand(sqltxt,sqlconn) Dim da As New sqlDataAdapter(sqlcmd) Dim ds As New DataSet Dim dt As New DataTable da.UpdateCommand = sqlcmd Dim mybuilder As New sqlCommandBuilder(da) Try dt = dm_dt.DT.Copy ds.Tables.Add(dt) sqlconn.Open() da.Update(ds,"User_info") Return True Catch ex As Exception MsgBox(ex.Message) Return False Finally sqlconn.Close() sqlcmd = Nothing End Try Return Nothing End Function</span>6、在业务逻辑层,编写传递数据访问层的函数:
<span style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px">'删除指定用户 Public Function deleteUser(ByVal dt As DataModel.dm_datatable) As Boolean Dim sqlUserInfo As New ChargeSystemDAL.dal_User_info Return sqlUserInfo.deleteUser(dt) End Function</span>7、最后,在表示层,调用业务逻辑层的函数:
<span style="FONT-FAMILY: KaiTi_GB2312; FONT-SIZE: 18px">Dim dt As New DataModel.dm_datatable '数据更改更新到数据库 If bllUserinfo.deleteUser(dt) = True Then MsgBox("成功删除用户!","成功") Exit Sub Else MsgBox("未能成功删除用户,详情请咨询管理员!","失败") Exit Sub End If</span>在5中,如果直接这样调用:ds.Tables.Add(dm_dt.DT),则会产生错误:DT已存在DataSet。所以dm_dt.DT复制后才能为ds所使用。 原文链接:https://www.f2er.com/vb/257293.html