Insert all data of a datagridview to database at once

前端 未结 7 902
南方客
南方客 2020-12-08 23:55

I have a datagridview which is created by various action and user\'s manipulation of data. I want to insert all the data of the gridview to the database at once, I know I co

7条回答
  •  醉酒成梦
    2020-12-09 00:22

    Please see if below can help you

    Class Post_Sales

    Public Shared Sub Post_sales()
    
        Dim ITM_ID As Integer
    
        Dim SLS_QTY As Integer
    
        Dim SLS_PRC As Double
    
        Dim SLS_AMT As Double
    
        Dim DSPL_RCT As String
    
        Dim TAX_CODE As Integer
    
    
        'Format the current  date and send it to a textbox
        Form1.TextBox6.Text = System.DateTime.Now.ToString((" yyyy-MM-dd"))
    
        'Open Connection
    
        Dim con As New SqlConnection("Initial Catalog=Your Database here;Data source=.;Network Library=DBMSSOCN;User ID=sa;Password=")
    
        con.Open()
    
        'Insert Records into the database
    
    
        For Each rw As DataGridViewRow In Form1.DataGridView1.Rows
    
            ITM_ID = rw.Cells("Column1").Value
            DSPL_RCT = rw.Cells("Column2").Value
            SLS_QTY = rw.Cells("Column3").Value
            SLS_PRC = rw.Cells("Column4").Value
            SLS_AMT = rw.Cells("Column5").Value
            TAX_CODE = rw.Cells("Column6").Value
    
            Dim cmd As New SqlCommand("INSERT INTO DAY_PLUSALES (DT,ITM_ID,DSPL_RCT,SLS_QTY,SLS_PRC,SLS_AMT,TAX_CODE) values ('" & Form1.TextBox6.Text & "','" & ITM_ID & "','" & DSPL_RCT & "','" & SLS_QTY & "','" & SLS_PRC & "','" & SLS_AMT & "','" & TAX_CODE & "')", con)
    
            cmd.ExecuteNonQuery()
    
        Next
    
        con.Close()
    
        MessageBox.Show("Records Added to the SQL Database successfully!", "Records Updated ")
    
    End Sub
    

    End Class

提交回复
热议问题