Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim condi As Integer
Dim con As New SqlConnection("Password = 12345 ; User ID = sa ; Initial Catalog=log_In ; Data Source= DELL-PC")
'ADD
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
con.Open()
If MsgBox("Are you sure you want to save changes?", vbYesNo) = MsgBoxResult.Yes Then
If condi = 1 Then
Dim cmd As New SqlCommand("Insert Into emp (empName,empUserName,empPass) Values (@empName,@empUser,@empPass)", con)
cmd.Parameters.AddWithValue("empName", txtName.Text)
cmd.Parameters.AddWithValue("empUser", txtUser.Text)
cmd.Parameters.AddWithValue("empPass", txtPass.Text)
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Record successfully added.")
ElseIf MsgBox("Are you sure you want to save changes?", vbYesNo) = MsgBoxResult.No Then
con.Close()
End If
End If
End Sub
'EDIT
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
con.Open()
If MsgBox("Are you sure you want to save changes?", vbYesNo) = MsgBoxResult.Yes Then
Dim cmd As New SqlCommand("update emp set empName = @empName, empUserName = @empUser ,empPass = @empPass where empName = @empName ", con)
cmd.Parameters.AddWithValue("empName", txtName.Text)
cmd.Parameters.AddWithValue("empUser", txtUser.Text)
cmd.Parameters.AddWithValue("empPass", txtPass.Text)
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Record successfully updated.")
ElseIf MsgBox("Are you sure you want to save changes?", vbYesNo) = MsgBoxResult.No Then
con.Close()
End If
End Sub
'DELETE
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
con.Open()
If MsgBox("Are you sure you want to delete this record?", vbYesNo) = MsgBoxResult.Yes Then
Dim cmd As New SqlCommand("delete from emp where empName = @empName", con)
cmd.Parameters.AddWithValue("empName", txtName.Text)
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Record successfully deleted!.")
ElseIf MsgBox("Are you sure you want to delete this record?", vbYesNo) = MsgBoxResult.No Then
con.Close()
End If
End Sub
End Class
嗨我还是学生,这段代码只是我同学代码的来源。我们正在做一个系统,我选择了一个监控和分析系统,这对程序员来说非常容易。可悲的是,我没有把编程。我甚至无法从头做起。但我还是想学习。所以请告诉我这段代码出了什么问题。 如果我违反了某些论坛的礼仪/规则,请告诉我。谢谢! :)
答案 0 :(得分:0)
这很好......你参考我们的输入,这是最重要的
只有一件事要改变...... 将你的con.close陈述移出你的其他if语句,如果不是你没有关闭你的连接!
喜欢这样
Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
If MsgBox("Are you sure you want to save changes?", vbYesNo) = MsgBoxResult.Yes Then
con.Open()
Dim cmd As New SqlCommand("update emp set empName = @empName, empUserName = @empUser ,empPass = @empPass where empName = @empName ", con)
cmd.Parameters.AddWithValue("empName", txtName.Text)
cmd.Parameters.AddWithValue("empUser", txtUser.Text)
cmd.Parameters.AddWithValue("empPass", txtPass.Text)
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Record successfully updated.")
con.Close()
End If
End Sub
make sure to change all the code...
you may also want to make a single sub and call it... i will write it up in a min...
again what you did is fine but i would use something like
Public Sub saveChanges(howto As String, txtName As String, txtUser As String, txtPass As String, condi As Integer)
Dim con As New SqlConnection("Password = 12345 ; User ID = sa ; Initial Catalog=log_In ; Data Source= DELL-PC")
Dim cmd As New SqlCommand
cmd.Connection = con
Select Case howto
Case "add"
cmd.CommandText = "Insert Into emp (empName,empUserName,empPass) Values (@empName,@empUser,@empPass)"
Case "edit"
condi = 1
cmd.CommandText = "update emp set empName = @empName, empUserName = @empUser ,empPass = @empPass where empName = @empName "
Case "delete"
condi = 1
cmd.CommandText = "delete from emp where empName = @empName"
End Select
'here we write the info
con.Open()
If MsgBox("Are you sure you want to save changes?", vbYesNo) = MsgBoxResult.Yes Then
If condi = 1 Then
cmd.Parameters.AddWithValue("empName", txtName)
cmd.Parameters.AddWithValue("empUser", txtUser)
cmd.Parameters.AddWithValue("empPass", txtPass)
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Record successfully added.")
End If
End If
con.Close()
End Sub
并称之为
call saveChanges ("add",txtName.text,txtUser.txt,txtPass.text,condi)
你不必担心在添加之外的condi var,因为我在select statment中将它设置为1
希望这有帮助!如果您还有其他需要,请告诉我