数据更新和删除错误

时间:2014-03-03 15:12:23

标签: vb.net sql-server-2008 datagrid

我有一个DataGrid和许多文本字段,用于将数据添加到数据库中。当我添加新数据时,它会在数据库和数据网格中更新。但是当我删除数据时,它只会从数据库中删除。它没有在数据网格中更新。

当我尝试更新数据时,它一次只更新一条记录。当我尝试另一条记录时,它会给我一个错误"The variable name '@date' has already been declared. Variable names must be unique within a query batch or stored procedure."我必须关闭并重新打开表单以更新新记录。

Imports System.Data.SqlClient


Public Class Form
Dim con As New SqlClient.SqlConnection("Server = DEL-PC; Database=Shj; Trusted_Connection=yes;")
Dim cmd As New SqlCommand
Dim dr As SqlDataReader
Dim ds As DataSet
Dim da As SqlDataAdapter


Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

SqlDataAdapter1.Fill(DataSet11)

End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles newbtn.Click


If RegNoTextBox.Text <> "" Then
cmd.Connection = con
con.Open()
cmd.CommandText = "insert into WaterProofing(Date,RegNo) values ('" & DateDateTimePicker.Value & "','" & RegNoTextBox.Text & "')"
cmd.ExecuteNonQuery()
Call showdata()
con.Close()

Else
MessageBox.Show("Please enter registration number")
End If
End Sub

//updation
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editbtn.Click

cmd.Parameters.AddWithValue("@date", DateDateTimePicker.Value)
cmd.Parameters.AddWithValue("@regno", RegNoTextBox.Text)
cmd.Connection = con
con.Open()
cmd.CommandText = "update WaterProofing  set RegNo= @regno where date=@date"
cmd.ExecuteNonQuery()
Call showdata()
con.Close()
MessageBox.Show("Details Updated!")
End Sub


Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles deletebtn.Click
cmd.Connection = con
con.Open()
cmd.CommandText = "delete from WaterProofing where date='" & DateDateTimePicker.Value & "'"
cmd.ExecuteNonQuery()
Call showdata()
con.Close()
MessageBox.Show("Details Deleted!")
End Sub

Sub showdata()

SqlDataAdapter1.Fill(DataSet11, "WaterProofing")

With DataGridView
.Update()
End With

End Sub
End Class

请帮我删除和更新。

2 个答案:

答案 0 :(得分:1)

您在CommandText

处声明Button2_Click之前添加了参数

以这种方式尝试:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles editbtn.Click

Try

    Dim command As New SqlCommand

    con.Open()
    command.Connection = con

    command.CommandText = "update WaterProofing  set RegNo= @regno where date=@date"
    command.Parameters.AddWithValue("@date", DateDateTimePicker.Value)
    command.Parameters.AddWithValue("@regno", RegNoTextBox.Text)

    command.ExecuteNonQuery()
    con.Close()
    Call showdata()

    MessageBox.Show("Details Updated!")

Catch ex as Exception
   MessageBox.Show(ex.Message)  
End try

End Sub

<强>更新

基本上,当你第一次加载DataGridView时你必须​​做同样的事情,以便在删除或更新记录后用数据库中的当前数据刷新DataGridView,所以你的{{ 1}} Sub可能看起来像这样

ShowData

答案 1 :(得分:0)

Private Sub Button4_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理deletebtn.Click         尝试

        Dim command As New SqlCommand

        con.Open()
        command.Connection = con
        command.CommandText = "delete from WaterProofing where date='" & DateDateTimePicker.Value & "'"
        command.ExecuteNonQuery()
        con.Close()
        Call ShowData_Delete()

        MessageBox.Show("Details Deleted!")

    Catch ex As Exception
        MessageBox.Show(ex.Message)
    Finally

    End Try
End Sub

Private Sub ShowData_Delete()         Dim Connection As SqlConnection

    Try


        Connection = New SqlConnection("Server = DEL-PC; Database=Shj; Trusted_Connection=yes;")
        Connection.Open()
        Dim SQL As String = "SELECT * from WaterProofing"

        Dim DS As New DataSet
        Dim Adapter As New SqlDataAdapter(SQL, Connection)

        Adapter.Fill(DS)

        DataGridView1.DataSource = DS.Tables(0)
        DataGridView1.Refresh()


    Catch ex As Exception

        MessageBox.Show(ex.Message)

    Finally
        Connection.Close()


    End Try

End Sub