📄 form2.vb
字号:
'
'SqlSelectCommand1
'
Me.SqlSelectCommand1.CommandText = "SELECT name, age, class, xuehao FROM yonghu"
Me.SqlSelectCommand1.Connection = Me.SqlConnection1
'
'SqlInsertCommand1
'
Me.SqlInsertCommand1.CommandText = "INSERT INTO yonghu(name, age, class, xuehao) VALUES (@name, @age, @class, @xuehao" & _
"); SELECT name, age, class, xuehao FROM yonghu WHERE (xuehao = @xuehao)"
Me.SqlInsertCommand1.Connection = Me.SqlConnection1
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 10, "name"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@age", System.Data.SqlDbType.VarChar, 10, "age"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@class", System.Data.SqlDbType.VarChar, 10, "class"))
Me.SqlInsertCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@xuehao", System.Data.SqlDbType.VarChar, 10, "xuehao"))
'
'SqlUpdateCommand1
'
Me.SqlUpdateCommand1.CommandText = "UPDATE yonghu SET name = @name, age = @age, class = @class, xuehao = @xuehao WHER" & _
"E (xuehao = @Original_xuehao) AND (age = @Original_age OR @Original_age IS NULL " & _
"AND age IS NULL) AND (class = @Original_class OR @Original_class IS NULL AND cla" & _
"ss IS NULL) AND (name = @Original_name OR @Original_name IS NULL AND name IS NUL" & _
"L); SELECT name, age, class, xuehao FROM yonghu WHERE (xuehao = @xuehao)"
Me.SqlUpdateCommand1.Connection = Me.SqlConnection1
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@name", System.Data.SqlDbType.VarChar, 10, "name"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@age", System.Data.SqlDbType.VarChar, 10, "age"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@class", System.Data.SqlDbType.VarChar, 10, "class"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@xuehao", System.Data.SqlDbType.VarChar, 10, "xuehao"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_xuehao", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "xuehao", System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_age", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "age", System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_class", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "class", System.Data.DataRowVersion.Original, Nothing))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_name", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "name", System.Data.DataRowVersion.Original, Nothing))
'
'SqlDeleteCommand1
'
Me.SqlDeleteCommand1.CommandText = "DELETE FROM yonghu WHERE (xuehao = @Original_xuehao) AND (age = @Original_age OR " & _
"@Original_age IS NULL AND age IS NULL) AND (class = @Original_class OR @Original" & _
"_class IS NULL AND class IS NULL) AND (name = @Original_name OR @Original_name I" & _
"S NULL AND name IS NULL)"
Me.SqlDeleteCommand1.Connection = Me.SqlConnection1
Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_xuehao", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "xuehao", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_age", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "age", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_class", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "class", System.Data.DataRowVersion.Original, Nothing))
Me.SqlDeleteCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_name", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "name", System.Data.DataRowVersion.Original, Nothing))
'
'SqlConnection1
'
Me.SqlConnection1.ConnectionString = "workstation id=LX241;packet size=4096;integrated security=SSPI;initial catalog=12" & _
"3;persist security info=False"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(248, 248)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(120, 56)
Me.Button2.TabIndex = 9
Me.Button2.Text = "删除"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(6, 14)
Me.ClientSize = New System.Drawing.Size(448, 349)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.TextBox4)
Me.Controls.Add(Me.TextBox3)
Me.Controls.Add(Me.TextBox2)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.TextBox1)
Me.Controls.Add(Me.Label1)
Me.Name = "Form2"
Me.Text = "插入删除"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'检查姓名。。。。。等是否为空
If TextBox1.Text = "" Then
MessageBox.Show("姓名不能为空")
End If
If TextBox1.Text = "" Then
MessageBox.Show("年龄不能为空")
End If
If TextBox1.Text = "" Then
MessageBox.Show("班级不能为空")
End If
If TextBox1.Text = "" Then
MessageBox.Show("学号不能为空")
End If
'执行插入操作
If MessageBox.Show("你确定你要插入吗?", "数据插入", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = DialogResult.OK Then
Try
SqlConnection1.Open()
Dim cmd As New SqlCommand
cmd = SqlConnection1.CreateCommand
cmd.CommandText = "insert into yonghu values('" + Trim(TextBox1.Text) + "','" + Trim(TextBox2.Text) + "','" + Trim(TextBox3.Text) + "','" + Trim(TextBox4.Text) + "')"
cmd.ExecuteNonQuery()
MessageBox.Show("数据插入成功")
SqlConnection1.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Else
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'查找你要删除的数据是否存在
Try
SqlConnection1.Open()
Dim cmd As New SqlCommand
cmd = SqlConnection1.CreateCommand
cmd.CommandText = "select xuehao from yonghu where xuehao='" + Trim(TextBox1.Text) + "'"
Dim dr As SqlDataReader
dr = cmd.ExecuteReader
If dr.Read <> -1 Then
MessageBox.Show("你删除的数据不存在")
dr.Close()
SqlConnection1.Close()
Exit Sub
Else
dr.Close()
SqlConnection1.Close()
End If
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
'你删除的数据存在,执行删除操作
If MessageBox.Show("你确定你要删除吗?", "删除数据", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) = DialogResult.OK Then
Try
SqlConnection1.Open()
Dim cmd As New SqlCommand
cmd = SqlConnection1.CreateCommand
cmd.CommandText = "delete from yonghu where xuehao='" + Trim(TextBox4.Text) + "'"
cmd.ExecuteNonQuery()
MessageBox.Show("数据删除成功")
SqlConnection1.Close()
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
Else
TextBox4.Text = ""
End If
End Sub
End Class
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -