【发布时间】:2015-06-25 19:40:44
【问题描述】:
对不起新手,我正在制作一个可以插入、删除和更新的库存。但问题是我想要更新的所有数据都没有真正更新。我总是去“记录未更新”然后我再次尝试更新下一个问题是“变量名'@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 导入 System.Data.SqlClient
公开课表3
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
结束类
我的第一个问题出现同样的问题,
【问题讨论】:
-
第二个问题发生是因为你使用了全局变量,第一个(如果你没有任何异常)可能是因为没有记录要更新匹配用于查找 serv_Id 的整数
-
谢谢史蒂夫。我现在试试这个。我会告诉你是否会出现任何问题
-
@Steve 第二个问题已经解决,但第一个问题相同,它没有更新我在数据库中的记录中的数据,我编辑了我以前的问题并添加了我的整个代码。请帮我。并感谢您的回复
标签: vb.net visual-studio-2013 localdb