The previous article published the DataGridView control to display data. DataGridView often appears when dealing with databases and is also very useful. Using the DataGridView to modify and query databases is convenient. Here we need to use the DataGridView data and delete the data from the database by selecting the row. The principle is to set the master of the selected record
The previous article published the DataGridView control to display data. DataGridView often appears when dealing with databases and is also very useful. Using the DataGridView to modify and query databases is convenient. Here we need to use the DataGridView data and delete the data from the database by selecting the row. The principle is to set the master of the selected record
The previous article published the DataGridView control to display data. DataGridView often appears when dealing with databases and is also very useful. Using the DataGridView to modify and query databases is convenient.
Here we need to use the DataGridView data and delete the data from the database by selecting the row.
The principle is to extract the primary key of the selected record and then pass it to the entity. The object is used to pass the value to the D layer to modify the database.
The following is the code of each layer for your reference.
The interface-Layer Code is the parent class method of layer D. Only layer D code is provided here:
'Override and delete the user interface method Public Function DelUser (user As Entity. loginEntity) As Integer Implements IAddDel. delUser Dim strSQL As String = "delete from User_info where userName = @ username" Dim params () As SqlParameter = {New SqlParameter ("@ username", user. user_name)} Dim helper As New SqlHelper Dim int = helper. executeNoQuery (strSQL, CommandType. text, params) Return int End Function
Abstract Factory Code:
Private Shared ReadOnly AssemblyName As String = "DAL" 'declares the Assembly name Private Shared ReadOnly db As String = ConfigurationManager. appSettings ("DB") 'reads the configuration file Public Function AddDel () As IAddDel Dim className As String = AssemblyName + ". "+ db +" AddDelDAL "Dim iadddel As IAddDel iadddel = CType (Assembly. load (AssemblyName ). createInstance (className), IAddDel) 'reflection Return iadddel End FunctionLayer B code:
'Determine whether the Public Function IsDelUser (ByVal user As Entity. loginEntity) As Boolean Dim int = iadddel. delUser (user) If int = 1 Then Return True Else Return False End If End Function
U-Layer Code
Dim k As Integer = gvwUser. selectedRows. count Dim thisUser As New Entity. loginEntity Dim ub As New BLL. addDelBLL 'determines whether there is an option to record If k> 0 Then If MessageBox. show ("deleted users cannot be restored! Continue to delete? "," Prompt ", MessageBoxButtons. yesNo, _ MessageBoxIcon. question) = Windows. forms. dialogResult. yes then' is deleted from the bottom up To avoid the hourglass effect For I As Integer = k To 1 Step-1 'To get the user name thisUser. user_name = gvwUser. selectedRows (I-1 ). cells ("userName "). value. tostring' to determine whether the selected user is a logon user If thisUser. user_name = UserName Then MsgBox ("the current user cannot be deleted. Please select again! ", VbOKOnly + vbExclamation," system prompt ") Exit Sub Else If ub. IsDelUser (thisUser) = True Then MsgBox (" deleted successfully! ", VbOKOnly + vbInformation," system prompt ") Else MessageBox. Show (" deletion failed! "," System prompt ", MessageBoxButtons. OK, MessageBoxIcon. error) End If 'deletes the information deleted from the database from Datagridview1 gvwUser. rows. removeAt (gvwUser. selectedRows (I-1 ). index) End If Next End If Else MsgBox ("select the row to delete") Exit Sub End If End Sub
The effect is as follows:
Before deletion: after deletion:
We use the DataGridView to operate data more intuitively, but the data is priceless. Be careful when operating the database! To avoid unnecessary troubles. If necessary, you should give a prompt to determine whether to delete the data or improve the operation permissions. To facilitate the operation, ensure data security!