📄 frmcustomermanager.vb
字号:
Private Sub btnQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuery.Click
Try
Dim mGuid As Guid
Dim mCustomer As CustomersManager.CustomerInfo
If (Me.txtAccount.Tag Is Nothing) And (Me.txtIdenty.Text.Trim = String.Empty) Then
Return
End If
If (Me.txtAccount.Tag Is Nothing) And Not (Me.txtIdenty.Text.Trim = String.Empty) Then
mGuid = CustomersManager.QueryCustomerByIdenty(Me.txtIdenty.Text.Trim)
If mGuid.Equals(Guid.Empty) Then
MsgBox("嘿,没有这个人哪,他没有注册吧。", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "会员查询失败")
Return
End If
mCustomer = CustomersManager.QueryCustomerByCustomerID(mGuid)
Else
mCustomer = CustomersManager.QueryCustomerByCustomerID(CType(Me.txtAccount.Tag, Guid))
End If
DisplayCustomerInfo(mCustomer)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
Dim mCustomer As CustomersManager.CustomerInfo = _
New CustomersManager.CustomerInfo(txtIdenty.Text.Trim, _
CDec(txtMoney.Text.Trim), CInt(txtCount.Text.Trim), _
CSng(txtOffRatio.Text.Trim), Now.Today)
Dim Result As Guid = CustomersManager.AddCustomer(mCustomer)
If Not Result.Equals(Guid.Empty) Then
Me.txtAccount.Text = Result.ToString.ToUpper
Me.txtAccount.Tag = Result
Me.txtAccount.Refresh()
End If
End Sub
Private Sub btnLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogout.Click
If (Me.txtAccount.Tag Is Nothing) And (Me.txtIdenty.Text.Trim = String.Empty) Then
Return
End If
If Me.txtAccount.Tag Is Nothing Then
CustomersManager.DeletCustomer(Guid.Empty, Me.txtIdenty.Text.Trim)
Else
CustomersManager.DeletCustomer(CType(Me.txtAccount.Tag, Guid), Me.txtIdenty.Text.Trim)
End If
MsgBox("您已经成功的将该会员注销。", MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "会员注销成功")
Me.ClearAll()
End Sub
Private Sub DisplayCustomerInfo(ByVal mCustomer As CustomersManager.CustomerInfo)
If mCustomer Is Nothing Then
Return
End If
Me.txtAccount.Tag = mCustomer.CustomerID
Me.txtAccount.Text = mCustomer.CustomerID.ToString.ToUpper
Me.txtIdenty.Text = mCustomer.CustomerIdenty
Me.txtMoney.Text = CDec(mCustomer.ConsumeSummary).ToString("C")
Me.txtCount.Text = mCustomer.ConsumeTimes
Me.txtOffRatio.Text = Format(mCustomer.CanOffRatio, "0.00")
Me.txtBuildDate.Text = mCustomer.BuildDate.ToLongDateString
End Sub
Private Sub ClearAll()
Me.txtAccount.Tag = Nothing
Me.txtAccount.Text = String.Empty
Me.txtIdenty.Text = String.Empty
Me.txtMoney.Text = String.Empty
Me.txtCount.Text = String.Empty
Me.txtOffRatio.Text = String.Empty
Me.txtBuildDate.Text = String.Empty
End Sub
End Class
Friend Class CustomersManager
Friend Class CustomerInfo
Public CustomerID As Guid
Public CustomerIdenty As String
Public ConsumeSummary As Double
Public ConsumeTimes As Integer
Public CanOffRatio As Double
Public BuildDate As Date
Public Sub New()
End Sub
Public Sub New(ByVal ID As String, ByVal CSUM As Decimal, ByVal CTIM As Integer, ByVal CORT As Decimal, ByVal BLDT As Date)
Me.CustomerIdenty = ID
Me.ConsumeSummary = CSUM
Me.ConsumeTimes = CTIM
Me.CanOffRatio = CORT
Me.BuildDate = BLDT
End Sub
End Class
Friend Shared Function AddCustomer(ByVal mCustomer As CustomerInfo) As Guid
'// 判断会员信息是否为空值,否则抛出错误信息,
'// 提供给后面的代码使用Try Catch End Try错误捕获结构体
If (mCustomer Is Nothing) Or (mCustomer.CustomerID.ToString = String.Empty) Then
Throw New ArgumentNullException
End If
'// 判断是否对数据库的连接已经初始化
If AccessToDatabase.objSqlConnection Is Nothing Then
Throw New ApplicationException("没有初始化数据库连接")
End If
Try
If AccessToDatabase.objSqlConnection.State <> ConnectionState.Open Then
AccessToDatabase.objSqlConnection.Open()
End If
Dim Result As Guid = QueryCustomerByIdenty(mCustomer.CustomerIdenty)
If Not Result.Equals(Guid.Empty) Then
MsgBox("对不起,您已经是注册会员了。", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "注册失败")
Return Result
End If
Dim sqlAddCommand As SqlCommand = New SqlCommand("sp_CustomerInfo_INS")
sqlAddCommand.CommandType = CommandType.StoredProcedure
With sqlAddCommand.Parameters
.Add("@CustomerID", SqlDbType.Char, 38)
.Add("@Identy", SqlDbType.Char, 18)
.Add("@Summary", SqlDbType.Money, 8)
.Add("@Counts", SqlDbType.Int, 4)
.Add("@DisCount", SqlDbType.Real, 4)
.Add("@LoginDate", SqlDbType.DateTime, 8)
.Item("@CustomerID").Value = String.Empty
.Item("@Identy").Value = mCustomer.CustomerIdenty
.Item("@Summary").Value = mCustomer.ConsumeSummary
.Item("@Counts").Value = mCustomer.ConsumeTimes
.Item("@DisCount").Value = mCustomer.CanOffRatio
.Item("@LoginDate").Value = mCustomer.BuildDate
End With
If AccessToDatabase.objSqlConnection.State <> ConnectionState.Open Then
AccessToDatabase.objSqlConnection.Open()
End If
sqlAddCommand.Connection = AccessToDatabase.objSqlConnection
sqlAddCommand.ExecuteNonQuery()
'// 下面一行代码是关闭数据库连接,为了不影响速度,建议不频繁
'// 打开和关闭数据库连接,但对安全性有一定影响,需要权衡利弊
'AccessToDatabase.DisConnectToDataBase()
MsgBox("祝贺您,您已经成功的增添了一个新会员。", MsgBoxStyle.OKOnly + MsgBoxStyle.Information, "增添新会员成功")
Return QueryCustomerByIdenty(mCustomer.CustomerIdenty)
Catch ex As Exception
MsgBox("太遗憾了,您增添新会员时失败了,请检查会员编码。", MsgBoxStyle.OKOnly + MsgBoxStyle.Exclamation, "增添新会员失败")
Return Guid.Empty
End Try
End Function
Friend Shared Function QueryCustomerByCustomerID(ByVal CustomerID As Guid) As CustomerInfo
Dim tempDS As New DataSet
Dim mCustomer As New CustomerInfo
tempDS.Clear()
If AccessToDatabase.objSqlConnection Is Nothing Then
Throw New ApplicationException("没有初始化数据库连接")
Return Nothing
End If
Try
If AccessToDatabase.objSqlConnection.State <> ConnectionState.Open Then
AccessToDatabase.objSqlConnection.Open()
End If
Dim sqlstr As String = "SELECT * FROM CustomerInfo WHERE CustomerID='" & CustomerID.ToString & "'"
tempDS = AccessToDatabase.GetDataFromDB(sqlstr)
If Not tempDS Is Nothing Then
If tempDS.Tables(0).Rows.Count > 0 Then
mCustomer.CustomerID = CustomerID
mCustomer.CustomerIdenty = CStr(tempDS.Tables(0).Rows(0)("Identy")).Trim
mCustomer.ConsumeSummary = CDec(tempDS.Tables(0).Rows(0)("ConsumeSummary"))
mCustomer.ConsumeTimes = CInt(tempDS.Tables(0).Rows(0)("ConsumeCount"))
mCustomer.CanOffRatio = CSng(tempDS.Tables(0).Rows(0)("DisCountRatio"))
mCustomer.BuildDate = CStr(tempDS.Tables(0).Rows(0)("LoginDate")).Trim
Return mCustomer
Else
Return Nothing
End If
Else
Return Nothing
End If
Catch ex As Exception
Return Nothing
End Try
End Function
Friend Shared Function DeletCustomer(ByVal CustomerID As Guid, ByVal Identy As String) As Boolean
Dim sqlstr As String = String.Empty
If CustomerID.Equals(Guid.Empty) And Identy = String.Empty Then
Throw New ArgumentNullException
End If
If AccessToDatabase.objSqlConnection Is Nothing Then
Throw New ApplicationException("没有初始化数据库连接")
End If
If CustomerID.Equals(Guid.Empty) Then
sqlstr = "DELETE CustomerInfo WHERE Identy='" & Identy & "'"
Else
sqlstr = "DELETE CustomerInfo WHERE CustomerID='" & CustomerID.ToString & "'"
End If
Try
If AccessToDatabase.objSqlConnection.State <> ConnectionState.Open Then
AccessToDatabase.objSqlConnection.Open()
End If
Return AccessToDatabase.UpdateData(sqlstr)
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
Friend Shared Function QueryCustomerByIdenty(ByVal Identy As String) As Guid
If Identy Is Nothing Or Identy = String.Empty Then
Throw New ArgumentNullException
End If
Try
If AccessToDatabase.objSqlConnection.State <> ConnectionState.Open Then
AccessToDatabase.objSqlConnection.Open()
End If
Dim sqlstr As String = "SELECT CustomerID FROM CustomerInfo WHERE Identy='" & Identy & "'"
Dim tempDS As New DataSet
Dim tempGUID As Guid
tempDS.Clear()
tempDS = AccessToDatabase.GetDataFromDB(sqlstr)
If Not tempDS Is Nothing Then
If tempDS.Tables(0).Rows.Count > 0 Then
tempGUID = CType(tempDS.Tables(0).Rows(0)("CustomerID"), Guid)
Return tempGUID
Else
Return Guid.Empty
End If
Else
Return Guid.Empty
End If
Catch ex As Exception
MsgBox(ex.Message)
Return Guid.Empty
End Try
End Function
Friend Shared Function UpdateByCustomerID(ByVal CustomerId As Guid, ByVal Summary As Double) As Boolean
If CustomerId.Equals(Guid.Empty) Then
Throw New ArgumentNullException
End If
If AccessToDatabase.objSqlConnection Is Nothing Then
Throw New ApplicationException("没有初始化数据库连接")
End If
Try
If AccessToDatabase.objSqlConnection.State <> ConnectionState.Open Then
AccessToDatabase.objSqlConnection.Open()
End If
Dim sqlUpdateCommand As SqlCommand = New SqlCommand("sp_CustomerInfo_UPD_BY_CustomerID")
sqlUpdateCommand.CommandType = CommandType.StoredProcedure
With sqlUpdateCommand.Parameters
.Add("@CustomerID", SqlDbType.Char, 38)
.Add("@Summary", SqlDbType.Money, 8)
.Item("@CustomerID").Value = CustomerId
.Item("@Summary").Value = Summary
End With
sqlUpdateCommand.Connection = AccessToDatabase.objSqlConnection
sqlUpdateCommand.ExecuteNonQuery()
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
Friend Shared Function UpdateByIdenty(ByVal Identy As Guid, ByVal Summary As Double) As Boolean
If Identy.Equals(String.Empty) Then
Throw New ArgumentNullException
End If
If AccessToDatabase.objSqlConnection Is Nothing Then
Throw New ApplicationException("没有初始化数据库连接")
End If
Try
If AccessToDatabase.objSqlConnection.State <> ConnectionState.Open Then
AccessToDatabase.objSqlConnection.Open()
End If
Dim sqlUpdateCommand As SqlCommand = New SqlCommand("sp_CustomerInfo_UPD_BY_Identy")
sqlUpdateCommand.CommandType = CommandType.StoredProcedure
With sqlUpdateCommand.Parameters
.Add("@Identy", SqlDbType.Char, 38)
.Add("@Summary", SqlDbType.Money, 8)
.Item("@Identy").Value = Identy
.Item("@Summary").Value = Summary
End With
sqlUpdateCommand.Connection = AccessToDatabase.objSqlConnection
sqlUpdateCommand.ExecuteNonQuery()
Return True
Catch ex As Exception
MsgBox(ex.Message)
Return False
End Try
End Function
End Class
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -