. NET tiered login-Data Center charging system, summary,. net Charging System

Source: Internet
Author: User

. NET tiered login-Data Center charging system, summary,. net Charging System

Last year, I wrote a summary article about the login of the data center billing system, which is based on VB and directly queries the database for login. Is an early knowledge. For more information, see VB Database logon form-IDC billing system summary (1 ).

Today, I want to change the angle and method to achieve login. First, the development tool I selected is VB. NET, and the database is SQLSever2008. Second, I use a three-tier idea to separate the interface, logic, and data to reduce mutual influence. At the same time, the interface layer should be as concise as possible. All algorithms and business logic are written to the logic layer. At the same time, the interface and factory mode and appearance mode are used to reduce the dependency between them, reduced coupling. Finally, write the code through the document and UML diagram to achieve login.

First, let's take a look at the Login Sequence diagram I designed (if there is any error, please point it out, I am very grateful ):


After seeing the figure, if you have some experience, I believe the code is not a problem for you. Below, I put my code below for reference only! (By the way, the U-layer interface has not changed. If you want to know, please refer to the connection blog ).

The first is the entity Layer Code:

User entity layer:

Public Class UserInfo    Private _userid As String    Private _password As String        Public Property UserID As String        Get            Return _userid        End Get        Set(value As String)            _userid = value        End Set    End Property    Public Property PassWord As String        Get            Return _password        End Get        Set(ByVal value As String)            _password = value        End Set    End Property   End Class

Worksheet entity layer: 

<Span style = "font-size: 18px;"> Imports SystemImports System. data ''' <summary> ''' object Class ''' for the OpeLineRecord database table </summary> Public Class OpeLineRecordEntity Private m_Opercomprecordid As String Private m_Operatorid As String Private m_Landdate As String private m_Landtime As String Private m_Outdate As String Private m_Outtime As String Private m_Status As String Private m_Computerid As String Public Sub OpeLineRecordEntity () m_Opercomprecordid = "" m_Operatorid = "" m_Landdate = "" m_Landtime = "" m_Outdate = "" m_Outtime = "" m_Status = "" m_Computerid = "" End Sub ''' <summary> '''set or Return opercomprecordid''' </summary> Public Property Opercomprecordid As String Get Return m_Opercomprecordid End Get Set (value As String) m_Opercomprecordid = value End Set End Property ''' <summary> ''' sets or returns Operatorid ''' </summary> Public Property Operatorid As String Get Return m_Operatorid End Get Set (value as String) m_Operatorid = value End Set End Property ''' <summary> ''' Set or Return value Landdate ''' </summary> Public Property Landdate As String Get Return m_Landdate End Get Set (value as String) m_Landdate = value End Set End Property ''' <summary> ''' sets or returns Landtime ''' </summary> Public Property Landtime As String Get Return m_Landtime End Get Set (value as String) m_Landtime = value End Set End Property ''' <summary> ''' Set or Return value Outdate ''' </summary> Public Property Outdate As String Get Return m_Outdate End Get Set (value as String) m_Outdate = value End Set End Property ''' <summary> ''' Set or Return value Outtime ''' </summary> Public Property Outtime As String Get Return m_Outtime End Get Set (value as String) m_Outtime = value End Set End Property ''' <summary> ''' sets or returns Status ''' </summary> Public Property Status As String Get Return m_Status End Get Set (value as String) m_Status = value End Set End Property ''' <summary> ''' sets or returns Computerid ''' </summary> Public Property Computerid As String Get Return m_Computerid End Get Set (value as String) m_Computerid = value End Set End PropertyEnd Class </span>
Convert Datatable to generic

<Span style = "font-size: 18px;"> Imports System. collections. generic 'adds the Generic namespace Imports System. reflectionPublic Class EntityHelper Public Shared Function convertTolist (Of T As {New}) (ByVal dt As DataTable) As IList (Of T) 'convert able to generic collect' Note: 1, convertToList (Of T As {New} The New here is used to constrain T and must have it. Otherwise, the new T will encounter the error ''2, and the new constraint is in C # and VB. NET is written differently. C # uses where to add constraints to T Dim myList As New List (Of T) 'define the final returned collection Dim myType As Type = GetType (T) 'to get the object class Type Dim dr As DataRow' to define the row set Dim tempName As String = String. empty 'defines a temporary variable 'to traverse all data rows In the DataTable For Each dr In dt. rows Dim myT As New t' defines the object Dim propertys () As PropertyInfo = myT of an object class. getType (). getProperties () 'define the property set Dim Pr As propertyinfo' to traverse all properties of this object For Each Pr In propertys tempName = Pr. name' assign the attribute Name to the Temporary Variable 'and check whether the DataTable contains this column (column Name = Object attribute Name) If (dt. columns. contains (tempName) Then 'compares this attribute with the column name in the DataTable to check whether the DataTable Contains this attribute' to determine whether the property has a Setter If (Pr. canWrite = False) then' determines whether this attribute can be written. If it cannot be written, the loop "Continue For End If Dim value As Object = dr (tempName)" appears) 'define an object-type variable to save the column value If (value. toString <> DBNull. value. toString () then' if it is not null, the property Pr is assigned to the object. setValue (myT, value, Nothing) 'dynamically accesses the attribute End If Next myList of an object through reflection during runtime. add (myT) 'add to the set Next Return mylist' returns the object set End FunctionEnd Class </span>

Then the interface layer IDAL:

<span style="font-size:18px;">Public Interface IUser    Function SelectUser(ByVal user As entity.UserInfo) As List(Of UserInfo)  End Interface</span>
Layer D:

<Span style = "font-size: 18px;"> Imports IDALImports entityImports System. data. sqlClientPublic Class UserDAL: Implements IDAL. IUser Public Function SelectUser (user As UserInfo) As List (Of UserInfo) Implements IUser. selectUser Dim strUserID As String = user. userID Dim helper As New Helper. sqlhelper Dim dt As New DataTable Dim mylist As List (Of UserInfo) dim strSql As String = "select * from OperatorInfo where OperatorID = @ UserID and state = 'Use'" Dim sqlParams As SqlParameter () = {New SqlParameter ("@ UserID ", strUserID)} 'declare and instantiate the dt = helper parameter. executeNonQuery (strSql, CommandType. text, sqlParams) 'calls the ExecSelect () method in the SqlHelper class to execute the query and obtains the returned value mylist = EntityHelper. convertTolist (Of UserInfo) (dt) 'converts dt to a generic set Return mylist End Function End FunctionEnd Class </span>

Add record

<Span style = "font-size: 18px;"> Imports IDALImports entityImports System. data. sqlClientPublic Class UserWorklogDAL: Implements IDAL. IUserWorklog Public Function AddUserworklog (worklog As OpeLineRecordEntity) As Boolean Implements IUserWorklog. adduserworklog' declares a SqlHelper-type helper Dim helper As New Helper. sqlhelper Dim dt As New Integer declares and instantiates the SQL statement Dim strSql As String = "Insert into OpeLineRecord (OperatorID, landDate, landTime, ComputerID, Status) values (@ OperatorID, @ landDate, @ landTime, @ ComputerID, @ Status) "'declares the Dim sqlParams As SqlParameter () = {New SqlParameter (" @ OperatorID ", worklog. operatorid), New SqlParameter ("@ LandDate", worklog. landdate), New SqlParameter ("@ LandTime", worklog. landtime), New SqlParameter ("@ ComputerID", worklog. computerid), New SqlParameter ("@ Status", worklog. status)} 'Call the ExecAddDeleteUser () method in the SqlHelper class to add information, obtain the returned value, and Return dt = helper. execAddDelUpdate (strSql, CommandType. text, sqlParams) Return dt If dt> 0 Then Return True Else Return False End If End Function </span>

Appearance:

<Span style = "font-size: 18px;"> Public Class LoginFAC Public Function Login (ByVal user As entity. userInfo) As String Dim userlogin As New BLL. userManager userlogin. isUserExit (user) If Not userlogin. isUserExit (user) Then Throw New Exception ("Incorrect password") End If userlogin. checkPWDIsRight (user) If userlogin. checkPWDIsRight (user) = False Then Throw New Exception ("Incorrect password") End If 'checks whether the user has logged on to the system Dim userwork As New entity. opeLineRecordEntity Dim workbll As New BLL. userWorklogManager userwork. operatorid = user. userID userwork. landdate = DateTime. now. toString ("yyyy/MM/dd") userwork. landtime = DateTime. now. toString ("HH: mm") userwork. computerid = System. net. dns. getHostName () userwork. status = "on duty" 'add logon information workbll. addWorklog (userwork) End FunctionEnd Class </span>

SQLhelper layer:

<Span style = "font-size: 18px;"> 'obtain the connection string Imports System from the configuration file. dataImports System. data. sqlClient 'imports System. configuration 'add reference to the Configuration file Public Class Sqlhelper 'Call the Configuration file Private ReadOnly strConnection As String = Configuration. configurationManager. receivettings ("ConnStr "). toString 'query tables with parameters 'Return able ': Public Function ExecuteNonQuery (ByVal plain text As String, ByVal primitive type As CommandType, ByVal sqlParams As SqlParameter ()) as DataTable Using conn As New SqlConnection (strConnection) 'Using the connection pool, you can automatically close the connection after use Dim cmd As SqlCommand = conn. createCommand () 'dim adp As SqlDataAdapter Dim ds As New DataSet cmd. commandText = plain text 'the SQL statement to be executed cmd. commandType = primitive type 'specifies the type of the SQL statement cmd. parameters. addRange (sqlParams) 'parameter array. The number of parameters depends on the actual situation. adp = New SqlDataAdapter (cmd) Try conn. open () adp. fill (ds) 'Return cmd. executeNonQuery () Return ds. tables (0) Catch ex As Exception Return Nothing Throw ex End Try End Using End Function 'add, delete, and modify parameters Public Function ExecAddDelUpdate (ByVal plain text As String, ByVal primitive type As CommandType, byVal sqlParams As SqlParameter () As Integer Using conn As New SqlConnection (strConnection) Dim cmd As SqlCommand = conn. createCommand () Dim adp As SqlDataAdapter Dim ds As New DataSet cmd. commandText = plain text cmd. commandType = commantype cmd. parameters. addRange (sqlParams) adp = New SqlDataAdapter (cmd) Try conn. open () adp. fill (ds) Return ds. tables. count Catch ex As Exception Return Nothing Throw ex End Try End Using End FunctionEnd Class </span>

Factory layer:

<Span style = "font-size: 18px;"> Imports System. reflectionImports [IDAL] Public Class BDFactory 'private Shared ReadOnly db As String = Configuration. configurationManager. deleetask( "DBString") 'Private Shared ReadOnly assemblyName = db + "DAL" Private Shared ReadOnly assemblyName = "SQLDAL" Public Function CreateUserDAO () As IDAL. IUser 'dim className As String = assemblyName + ". "+ db +" UserDAL "Dim className As String = assemblyName + ". "+" UserDAL "Return CType (Assembly. load (assemblyName ). createInstance (className), IUser 'returns the IUser End Function </span>

<Span style = "font-size: 18px;"> Public Function CreateUserworklogDAO () As IDAL. IUserWorklog Dim strInstance As String = assemblyName + ". "+" UserWorklogDAL "'refers to the object to be instantiated (with the same Assembly name as the namespace) Return CType (Assembly. load (assemblyName ). createInstance (strInstance), IUserWorklog 'Return IUserWorklog End Function </span>

Layer B:

Add host records

<Span style = "font-size: 18px;"> Imports FactoryImports entityPublic Class UserWorklogManager Private ReadOnly factory As Factory. BDFactory = New Factory. BDFactory Dim iu As IDAL. iuserworklog' declares and instantiates the variable iuser: Call factory. the iuser' returned by the CreateUserDAO () method adds the Public Function AddWorklog (ByVal worklog As entity. opeLineRecordEntity) As Boolean iu = factory. createUserworklogDAO () Dim dt As Integer dt = iu. addUserworklog (worklog) If dt> 0 Then Return True Else Return False End If End Function </span>
Check whether the user's password is correct

<Span style = "font-size: 18px;"> Imports entityPublic Class UserManager Private ReadOnly factory As Factory. BDFactory = New Factory. BDFactory () Dim iuser As IDAL. IUser Public Function IsUserExit (ByVal user As entity. userInfo) As Boolean Dim iu As IDAL. IUser Dim mylist As List (Of UserInfo) iu = factory. createUserDAO () mylist = iu. selectUser (user) If mylist. count = 0 Then Return False Else Return True End If End Function Public Function CheckPWDIsRight (ByVal user As entity. userInfo) As Boolean Dim iu As IDAL. IUser Dim mylist As List (Of UserInfo) iu = factory. createUserDAO () mylist = iu. selectUser (user) If Trim (mylist (0 ). passWord) <> user. passWord Then Throw New Exception ("Incorrect PassWord") Return False Else Return True End If End Function </span>
Finally, the U Layer

<Span style = "font-size: 18px;"> Public Class frmlogin Private Sub BtnOK_Click (sender As Object, e As EventArgs) Handles BtnOK. click Dim user As New entity. userInfo 'instantiate User user. userID = txtUserName. text user. passWord = txtPassword. text If txtUserName. text. trim (). length> 20 Then MessageBox. show ("no more than 20 digits", "prompt") Exit Sub End If txtUserName. text. trim () = "" Then MessageBox. show ("Enter the user name", "prompt", MessageBoxButtons. OK, MessageBoxIcon. exclamation) Exit Sub ElseIf txtPassword. text. trim () = "" Then MessageBox. show ("Enter Password", "prompt", MessageBoxButtons. OK, MessageBoxIcon. exclamation) Exit Sub End If Try 'Call the appearance of Dim loginfac As Facade. loginFAC = New Facade. loginFAC () loginfac. login (user) Me. hide () ForMain. show () Catch ex As Exception MsgBox (ex. message, CType (vbOKOnly + MsgBoxStyle. information, MsgBoxStyle), "prompt") End Try End Sub End Class </span>
When the OK system is over, there may be slight problems, which can be solved with care (because during the pasting process, you may not be careful, you know ......), For the first time, the code and logic are not simple and difficult. Why do we continue?

I wonder if the reader has such questions. In fact, it is very simple. Although the code and logic are troublesome, the system performance is better and the logic is clearer. At the same time, the burden on the U layer is reduced and the work is embodied. Similarly, the compatibility and scalability are better, and the robustness is also improved. It is more suitable for cooperative development. This is a small system. If it is a large system, is it more advantageous!


Network Examination System Security optimization, such as in the same data room, how can folder sharing be disabled between computers? How can I change the Registry through the net program?

You can shut down the computer GUEST user (GUEST. Right-click my computer and choose manage. Local User Name and Group. User Name Guest. Right-click the attributes and check the account. No one else can see it.

When the Aspnet website is published on a VM, the following error occurs: the "targetFramework" attribute cannot be recognized. Note that the attribute name is distinguished by size.

If Space Provider space is supported. net version switch, you can directly log on to the space management panel to switch. net version, but the general space is only supported. net2.0 ,. net3.0 ,. net3.5, if the background of your space cannot be switched. for the net4.0 version, you need to ask if your space provider does not support version 4.0 and you cannot switch the version by yourself. You can only make adjustments for your Space Provider. If you cannot adjust the version, you cannot use this space.
Reference: www.net.cn-69dns is your best choice!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.