One of the previous articles is that the DataGridView control displays data, andDataGridView often appears and is useful when dealing with databases. It is more convenient to make changes and queries to the database through DataGridView.
Here we need to use the DataGridView data and delete the data from the database by selecting rows.
The principle is that the primary key of the selected record is extracted, and then passed to the entity, through the entity to the D-level transfer value to achieve the database changes.
The following are the layers of code. For everyone to participate in the test.
The interface layer code is the parent class method of the D layer. Here is just the D-layer code:
<span style= "Background-color:rgb (255, 255, 255); ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > ' override Delete User interface method public Function deluser (user as entity.loginentity) as Integer Implements Iadddel.deluser Dim strSQL as String = "Delete from user_info where [email protected]" 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</span></span>
Abstract Factory Code:
<span style= "Background-color:rgb (255, 255, 255); ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > Private Shared ReadOnly assemblyname As String = "DAL" ' declared assembly name Private shared ReadOnly db As String = Config Urationmanager.appsettings ("DB") ' read config 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 function</span></span>
B-Layer code:
<span style= "Background-color:rgb (255, 255, 255); ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > ' inferred whether the successful public Function Isdeluser (ByVal user as entity.loginentity) as a Boolean Dim int = Iadddel was removed. Deluser (user) If int = 1 then return True Else return False end If End function</span> </span>
U-Layer Code
<span style= "Background-color:rgb (255, 255, 255); ><span style= "FONT-FAMILY:KAITI_GB2312;FONT-SIZE:18PX;" > Dim k As Integer = GvwUser.SelectedRows.Count Dim thisuser As New Entity.loginentity Dim ub As New BLL. ADDDELBLL ' Infer if there is a choice to record if K > 0 Then If MessageBox.Show ("Delete user will not recover!)Do you want to continue deleting? "," hint ", Messageboxbuttons.yesno, _ messageboxicon.question) = Windows.Forms.DialogResult.Ye S then ' delete from bottom up, avoid hourglass effect for I as Integer = k to 1 Step-1 ' get username Thisuser.user_name = Gvwuser.selectedrows (i-1). Cells ("UserName"). Value.tostring ' Infer whether the user is logged on as user if Thisuser.user_name = UserName Then MsgBox ("The current user cannot be deleted.") Please choose again! ", vbOKOnly + vbexclamation," System Prompt ") Exit Sub Else If UB. Isdeluser (thisuser) = True then MsgBox ("Delete succeeded! ", vbOKOnly + vbinformation," System Prompt ") Else MessageBox.Show (" Delete failed! "," System Prompt ", MessageBoxButtons.OK, Messageboxicon.error) End If ' will remove information from the database from D Delete GvwUser.Rows.RemoveAt in Atagridview1 (gvwuser.selectedrows(i-1). Index) End If Next End If Else MsgBox ("Please select rows to delete") Exit Sub End If End sub</span></span>
Effects such as the following:
before deletion: after deletion:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmdkynjk2na==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmdkynjk2na==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvdtaxmdkynjk2na==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/southeast ">
We operate the data more intuitively through DataGridView. But the data is priceless, the operation of the database must be cautious. So as not to bring us unnecessary trouble. You should give a hint before deleting it if necessary. Whether to confirm the deletion, or improve the operation permissions, convenient operation is to ensure the security of the data.
Delete DataGridView Select row and update database