我想更新数据网格视图中的更改到我的数据库中的表

时间:2018-04-13 13:45:29

标签: vb.net visual-studio

我希望保存按钮将我对数据网格视图所做的更新更改保存到数据库。但是,此时保存按钮将保存为具有更新更改的全新行。

 Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click

    adt = New OleDbDataAdapter("insert into Student (FName, SName, Attendance, CA1, CA2, FinalExam) values ( '" & txtFName.Text & "','" & txtSName.Text & "',  '" & txtAttendance.Text & "', '" & txtCA1.Text & "', '" & txtCA2.Text & "', '" & txtFinalExam.Text & "' )", dbconn)


    adt.Fill(ds)
    ds = New DataSet
    showData()
    MsgBox("Saved")

2 个答案:

答案 0 :(得分:0)

您需要发出更新而不是insert sql命令

https://www.w3schools.com/sql/sql_update.asp(为了更好地了解更新)

答案 1 :(得分:0)

下面的通用脚本将为您执行插入操作。只需更改列名称即可开展业务。

Imports System.Data.OleDb

Public Class Form1



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

        ' Requires: Imports System.Data.OleDb

        ' ensures the connection is closed and disposed
        Using connection As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source=""C:\Users\Ryan\Desktop\Coding\DOT.NET\Samples VB\Insert Into MS Access Table from Textbox\WindowsApplication1\bin\InsertInto.mdb"";" & _
            "Persist Security Info=False")
            ' open connection
            connection.Open()

            ' Create command
            Dim insertCommand As New OleDbCommand( _
                "INSERT INTO Table1([inputOne] , [inputTwo] , [inputThree]) " & _
                "VALUES (@inputOne, @inputTwo, @inputThree);", _
                connection)
            ' Add the parameters with value
            insertCommand.Parameters.AddWithValue("@inputOne", TextBox1.Text)
            insertCommand.Parameters.AddWithValue("@inputTwo", TextBox2.Text)
            insertCommand.Parameters.AddWithValue("@inputThree", TextBox3.Text)
            ' you should always use parameterized queries to avoid SQL Injection
            ' execute the command
            insertCommand.ExecuteNonQuery()

            MessageBox.Show("Insert is done!!")

        End Using

    End Sub
End Class

HTH。