我的更新方法不使用 localdb 在 vb.net 中更新我的数据库中的任何数据
My Update Method don't update any data in my database in vb.net using localdb
对不起这里的新手,我正在做一个可以插入、删除和更新的库存。但问题是我想更新的所有数据都没有真正更新。我总是去 "Record Not Updated" 然后我再次尝试更新下一个问题是 "The variable name '@date' has already been declared. Variable names must be unique within a query batch or stored procedure."
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
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
结束Class
我的第一个问题出现了同样的问题,
除非确实需要,否则不要使用全局变量。在您的情况下,无需保留全局连接对象和全局命令对象。如果您在本地声明变量,则不会丢失任何性能点。拥有一个全局 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
对不起这里的新手,我正在做一个可以插入、删除和更新的库存。但问题是我想更新的所有数据都没有真正更新。我总是去 "Record Not Updated" 然后我再次尝试更新下一个问题是 "The variable name '@date' has already been declared. Variable names must be unique within a query batch or stored procedure."
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
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
结束Class
我的第一个问题出现了同样的问题,
除非确实需要,否则不要使用全局变量。在您的情况下,无需保留全局连接对象和全局命令对象。如果您在本地声明变量,则不会丢失任何性能点。拥有一个全局 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