且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在SQl数据库中插入datagridview

更新时间:2023-02-08 18:01:15

如果您没有对此感到惊讶,那么datagridview的底部会有一个新行部分.



Hello ,

With this code shows data from a database in a datagridview.

And update it with the button.

But how can i insert new data that i put in the datagridview?

Imports System.Data.SqlServerCe
Public Class Form1
    Dim myDA As SqlCeDataAdapter
    Dim myDataSet As DataSet

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As SqlCeConnection = New SqlCeConnection("data source=C:\Users\BAS\Documents\Visual Studio 2010\Projects\WindowsApplication11\WindowsApplication11\bin\Debug\Spullen.sdf")
        Dim cmd As SqlCeCommand = New SqlCeCommand("SELECT * FROM Inkoop", con)
    End Sub


    

    Private Sub Update_Click(sender As System.Object, e As System.EventArgs) Handles Update.Click
        Me.Validate()
        Me.myDA.Update(Me.myDataSet.Tables("Inkoop"))
        Me.myDataSet.AcceptChanges()
    End Sub

    Private Sub Laden_Click(sender As System.Object, e As System.EventArgs) Handles Laden.Click
        Dim con As SqlCeConnection = New SqlCeConnection("data source=C:\Users\BAS\Documents\Visual Studio 2010\Projects\WindowsApplication11\WindowsApplication11\bin\Debug\Spullen.sdf")
        Dim cmd As SqlCeCommand = New SqlCeCommand("SELECT * FROM Inkoop", con)
        con.Open()
        myDA = New SqlCeDataAdapter(cmd)
        'One CommandBuilder object is required, which automatically generates DeleteCommand,UpdateCommand and InsertCommand for DataAdapter object   
        Dim builder As SqlCeCommandBuilder = New SqlCeCommandBuilder(myDA)
        myDataSet = New DataSet()
        myDA.Fill(myDataSet, "Inkoop")
        DataGridView1.DataSource = myDataSet.Tables("Inkoop").DefaultView
        con.Close()
        con = Nothing
    End Sub
End Class

If you have not surpressed that, then the datagridview has in the bottom a new line part.