抱歉新手在这里,我正在制作一个可以插入,删除和更新的清单。但问题是我要更新的所有数据都没有真正更新。我总是去"记录未更新"然后我再次尝试更新下一个出现的问题是"变量名称' @ date'已经宣布。变量名在查询批处理或存储过程中必须是唯一的。"
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim iii As Integer
Try
cn.Open()
cmd.CommandText = "UPDATE service SET date=@date ,tech_assign=@tech_assign,err_one=@err_one,err_two= @err_two,err_tri = @err_tri,err_other = @err_other,err_other_spec = @err_other_spec,act_one = @act_one,act_two = @act_two,act_other = @act_other,act_other_spec = @act_other_spec,request = @request,op_unit = @op_unit WHERE serv_Id= @serv_Id"
cmd.Connection = cn
cmd.Parameters.AddWithValue("@date", DateDateTimePicker.Text)
cmd.Parameters.AddWithValue("@tech_assign", Tech_assignTextBox.Text)
cmd.Parameters.AddWithValue("@err_one", Err_oneCheckBox.CheckState)
cmd.Parameters.AddWithValue("@err_two", Err_twoCheckBox.CheckState)
cmd.Parameters.AddWithValue("@err_tri", Err_triCheckBox.CheckState)
cmd.Parameters.AddWithValue("@err_other", Err_otherCheckBox.CheckState)
cmd.Parameters.AddWithValue("@err_other_spec", Err_other_specTextBox.Text)
cmd.Parameters.AddWithValue("@act_one", Act_oneCheckBox.CheckState)
cmd.Parameters.AddWithValue("@act_two", Act_twoCheckBox.CheckState)
cmd.Parameters.AddWithValue("@act_other", Act_otherCheckBox.CheckState)
cmd.Parameters.AddWithValue("@act_other_spec", Act_other_specTextBox.Text)
cmd.Parameters.AddWithValue("@request", RequestTextBox.Text)
cmd.Parameters.AddWithValue("@op_unit", Op_unitTextBox.Text)
cmd.Parameters.AddWithValue("@serv_Id", i)
iii = cmd.ExecuteNonQuery()
If iii <> 0 Then
MsgBox("Record Updated Succesfully!", MessageBoxIcon.Information)
Else
MsgBox("Record Not Updated", MessageBoxIcon.Warning)
End If
cn.Close()
Me.Close()
Catch ex As Exception
MsgBox(ex.Message)
Finally
If cn.State = ConnectionState.Open Then
cn.Close()
End If
End Try
End Sub
@Steve这是我在表单中的完整代码,我使用来自datagridview的i
来自其他表单
导入System.Data Imports System.Data.SqlClient
Public Class Form3
Dim cn As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\Users\Carlo\Documents\Visual Studio 2013\Projects\WindowsApplication5\WindowsApplication5\db.mdf;Integrated Security=True")
Dim cmd As New SqlCommand
Dim da As New SqlDataAdapter
Dim dt As New DataTable
Dim ds As New DataSet
Dim aa As String
Dim i As Integer
Public Sub getText(ByVal a As String)
aa = a
End Sub
Public Sub getId(ByVal into As Integer)
into = i
End Sub
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If aa = "Save" Then
Button1.Enabled = True
Button1.Visible = True
Button2.Enabled = False
Button2.Visible = False
Me.DateDateTimePicker.Text = Now
Me.Tech_assignTextBox.Text = " "
Me.Err_oneCheckBox.CheckState = CheckState.Unchecked
Me.Err_twoCheckBox.CheckState = CheckState.Unchecked
Me.Err_triCheckBox.CheckState = CheckState.Unchecked
Me.Err_otherCheckBox.CheckState = CheckState.Unchecked
Me.Err_other_specTextBox.Text = " "
Me.Act_oneCheckBox.CheckState = CheckState.Unchecked
Me.Act_twoCheckBox.CheckState = CheckState.Unchecked
Me.Act_otherCheckBox.CheckState = CheckState.Unchecked
Me.Act_other_specTextBox.Text = " "
Me.RequestTextBox.Text = " "
Me.Op_unitTextBox.Text = " "
ElseIf aa = "Update" Then
Button1.Enabled = False
Button1.Visible = False
Button2.Enabled = True
Button2.Visible = True
Me.DateDateTimePicker.Text = Form2.ServiceDataGridView.Item(1, i).Value
Me.Tech_assignTextBox.Text = Form2.ServiceDataGridView.Item(2, i).Value
If Me.Err_oneCheckBox.CheckState = Form2.ServiceDataGridView.Item(3, i).Value = "False" Then
Me.Err_oneCheckBox.CheckState = CheckState.Checked
ElseIf Me.Err_oneCheckBox.CheckState = Form2.ServiceDataGridView.Item(3, i).Value = "True" Then
Me.Err_oneCheckBox.CheckState = CheckState.Unchecked
End If
If Me.Err_twoCheckBox.CheckState = Form2.ServiceDataGridView.Item(4, i).Value = "False" Then
Me.Err_twoCheckBox.CheckState = CheckState.Checked
ElseIf Me.Err_twoCheckBox.CheckState = Form2.ServiceDataGridView.Item(4, i).Value = "True" Then
Me.Err_twoCheckBox.CheckState = CheckState.Unchecked
End If
If Me.Err_triCheckBox.CheckState = Form2.ServiceDataGridView.Item(5, i).Value = "False" Then
Me.Err_triCheckBox.CheckState = CheckState.Checked
ElseIf Me.Err_triCheckBox.CheckState = Form2.ServiceDataGridView.Item(5, i).Value = "True" Then
Me.Err_triCheckBox.CheckState = CheckState.Unchecked
End If
If Me.Err_otherCheckBox.CheckState = Form2.ServiceDataGridView.Item(6, i).Value = "False" Then
Me.Err_otherCheckBox.CheckState = CheckState.Checked
ElseIf Me.Err_otherCheckBox.CheckState = Form2.ServiceDataGridView.Item(6, i).Value = "True" Then
Me.Err_otherCheckBox.CheckState = CheckState.Unchecked
End If
Me.Err_other_specTextBox.Text = Form2.ServiceDataGridView.Item(7, i).Value
If Me.Act_oneCheckBox.CheckState = Form2.ServiceDataGridView.Item(8, i).Value = "False" Then
Me.Act_oneCheckBox.CheckState = CheckState.Checked
ElseIf Me.Act_oneCheckBox.CheckState = Form2.ServiceDataGridView.Item(8, i).Value = "True" Then
Me.Act_oneCheckBox.CheckState = CheckState.Unchecked
End If
If Me.Act_twoCheckBox.CheckState = Form2.ServiceDataGridView.Item(9, i).Value = "False" Then
Me.Act_twoCheckBox.CheckState = CheckState.Checked
ElseIf Me.Act_twoCheckBox.CheckState = Form2.ServiceDataGridView.Item(9, i).Value = "True" Then
Me.Act_twoCheckBox.CheckState = CheckState.Unchecked
End If
If Me.Act_otherCheckBox.CheckState = Form2.ServiceDataGridView.Item(10, i).Value = "False" Then
Me.Act_otherCheckBox.CheckState = CheckState.Checked
ElseIf Me.Act_otherCheckBox.CheckState = Form2.ServiceDataGridView.Item(10, i).Value = "True" Then
Me.Act_otherCheckBox.CheckState = CheckState.Unchecked
End If
Me.Act_other_specTextBox.Text = Form2.ServiceDataGridView.Item(11, i).Value
Me.RequestTextBox.Text = Form2.ServiceDataGridView.Item(12, i).Value
Me.Op_unitTextBox.Text = Form2.ServiceDataGridView.Item(13, i).Value
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
cn.Open()
Using cmd As New SqlClient.SqlCommand("INSERT INTO service (date,tech_assign,err_one,err_two,err_tri,err_other,err_other_spec,act_one,act_two,act_other,act_other_spec,request,op_unit) VALUES(@date,@tech_assign,@err_one,@err_two,@err_tri,@err_other,@err_other_spec,@act_one,@act_two,@act_other,@act_other_spec,@request,@op_unit)", cn)
cmd.Parameters.AddWithValue("@date", DateDateTimePicker.Text)
cmd.Parameters.AddWithValue("@tech_assign", Tech_assignTextBox.Text)
cmd.Parameters.AddWithValue("@err_one", Err_oneCheckBox.CheckState)
cmd.Parameters.AddWithValue("@err_two", Err_twoCheckBox.CheckState)
cmd.Parameters.AddWithValue("@err_tri", Err_triCheckBox.CheckState)
cmd.Parameters.AddWithValue("@err_other", Err_otherCheckBox.CheckState)
cmd.Parameters.AddWithValue("@err_other_spec", Err_other_specTextBox.Text)
cmd.Parameters.AddWithValue("@act_one", Act_oneCheckBox.CheckState)
cmd.Parameters.AddWithValue("@act_two", Act_twoCheckBox.CheckState)
cmd.Parameters.AddWithValue("@act_other", Act_otherCheckBox.CheckState)
cmd.Parameters.AddWithValue("@act_other_spec", Act_other_specTextBox.Text)
cmd.Parameters.AddWithValue("@request", RequestTextBox.Text)
cmd.Parameters.AddWithValue("@op_unit", Op_unitTextBox.Text)
cmd.ExecuteNonQuery()
End Using
MsgBox("New Service Has Been Save", MessageBoxIcon.Information)
cn.Close()
Me.Close()
Catch ex As Exception
MsgBox(ex.Message)
Finally
If cn.State = ConnectionState.Open Then
cn.Close()
End If
End Try
End Sub
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Dim iii As Integer
Dim sqlText = "UPDATE service SET date=@date ,tech_assign=@tech_assign,err_one=@err_one,err_two= @err_two,err_tri = @err_tri,err_other = @err_other,err_other_spec = @err_other_spec,act_one = @act_one,act_two = @act_two,act_other = @act_other,act_other_spec = @act_other_spec,request = @request,op_unit = @op_unit WHERE serv_Id= @serv_Id"
Try
Using localCN = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\Users\Carlo\Documents\Visual Studio 2013\Projects\WindowsApplication5\WindowsApplication5\db.mdf;Integrated Security=True")
Using localCMD = New SqlCommand(sqlText, localCN)
localCN.Open()
localCMD.Parameters.Add("@date", SqlDbType.Date).Value = DateDateTimePicker.Value
localCMD.Parameters.Add("@tech_assign", SqlDbType.NVarChar).Value = Tech_assignTextBox.Text
localCMD.Parameters.Add("@err_one", SqlDbType.Bit).Value = Err_oneCheckBox.CheckState
localCMD.Parameters.Add("@err_two", SqlDbType.Bit).Value = Err_twoCheckBox.CheckState
localCMD.Parameters.Add("@err_tri", SqlDbType.Bit).Value = Err_triCheckBox.CheckState
localCMD.Parameters.Add("@err_other", SqlDbType.Bit).Value = Err_otherCheckBox.CheckState
localCMD.Parameters.Add("@err_other_spec", SqlDbType.NVarChar).Value = Err_other_specTextBox.Text
localCMD.Parameters.Add("@act_one", SqlDbType.Bit).Value = Act_oneCheckBox.CheckState
localCMD.Parameters.Add("@act_two", SqlDbType.Bit).Value = Act_twoCheckBox.CheckState
localCMD.Parameters.Add("@act_other", SqlDbType.Bit).Value = Act_otherCheckBox.CheckState
localCMD.Parameters.Add("@act_other_spec", SqlDbType.NVarChar).Value = Act_other_specTextBox.Text
localCMD.Parameters.Add("@request", SqlDbType.NVarChar).Value = RequestTextBox.Text
localCMD.Parameters.Add("@op_unit", SqlDbType.NVarChar).Value = Op_unitTextBox.Text
localCMD.Parameters.Add("@serv_Id", SqlDbType.Int).Value = i
localCMD.ExecuteNonQuery()
localCN.Close()
End Using
End Using
'If iii <> 0 Then
'MsgBox("The Record Has Been Updated!", MessageBoxIcon.Information)
'Else
'MsgBox("The Record Is Not Updated!", MessageBoxIcon.Error)
'End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub form3_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
Form2.shwService()
End Sub
结束班
我的第一个问题就出现了同样的问题,
答案 0 :(得分:0)
除非确实有必要,否则不要使用全局变量。在您的情况下,不需要保留全局连接对象和全局命令对象。如果在本地声明变量,则不会丢失任何性能点。拥有全局SqlCommand对象意味着第二次输入此代码块时,分配的参数仍然存在。相反,本地对象总是被重新创建为新的,并且没有前一次调用的残余 相反,应尽快处理SqlConnection实例,以释放本地计算机和服务器上的系统资源。
所以你的第二个问题可以通过这种方式解决
Public Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim iii As Integer
Dim sqlText = "UPDATE service SET date=@date, ......"
Try
Using localCN = new SqlConnection (.... your connection string....)
Using localCMD = new SqlCommand(sqlText, localCN)
localCN.Open()
localCMD.Parameters.Add("@date", SqlDbType.DateTime).Value = DateDateTimePicker.Date
.....
End Using
End Using
Catch ex as Exception
MsgBox(ex.Message)
' No need to add a Finally (Using statement close and dispose for you)
End Try
第一个问题很可能是由于在通话时没有Serv_ID
字段与您的变量i
匹配的记录,另一种可能性是你是使用与您计划使用的数据库不同的数据库。特别是当您在连接字符串
|DataDirectory|
替换字符串时,这种情况会发生很多
修改强>
看看你的更新,似乎你有这个倒置
Public Sub getId(ByVal into As Integer)
' into = i
i = into
End Sub