simpan data dari datagridview vb ke database sql server

bagaimana cara agar data dari kolom datagridview vb bisa tersimpan ke database sql server?

avatar ditais12
@ditais12

1 Kontribusi 0 Poin

Diperbarui 6 tahun yang lalu

2 Jawaban:

Coba ini gan:

 Dim connection As New Data.SqlClient.SqlConnection
Dim command As New Data.SqlClient.SqlCommand

connection.ConnectionString = "Server= server; Database= DB; integrated security=true"
command.CommandText = "INSERT INTO <table> (Col1, Col2, Col3, Col4) VALUES (@Name, @Property, @Value, @Date)"

command.Parameters.Add("@ServerName", SqlDbType.VarChar)
command.Parameters.Add("@Property", SqlDbType.VarChar)
command.Parameters.Add("@Value", SqlDbType.VarChar)
command.Parameters.Add("@CaptureDate", SqlDbType.DateTime)
connection.Open()
command.Connection = connection

For i As Integer = 0 To DataGridView.Rows.Count - 1
    command.Parameters(0).Value = dgvServerConfig.Rows(i).Cells(0).Value
    command.Parameters(1).Value = dgvServerConfig.Rows(i).Cells(1).Value
    command.Parameters(2).Value = dgvServerConfig.Rows(i).Cells(2).Value
    command.Parameters(3).Value = dgvServerConfig.Rows(i).Cells(3).Value
    command.ExecuteNonQuery()
Next

Sumber: https://stackoverflow.com/questions/16530686/vb-net-insert-datagridview-contents-into-database

atau ini:

 Imports System.Data.SqlClient
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim connectionString As String = "Data Source=.;Initial Catalog=pubs;Integrated Security=True"
        Dim sql As String = "SELECT * FROM Authors"
        Dim connection As New SqlConnection(connectionString)
        Dim dataadapter As New SqlDataAdapter(sql, connection)
        Dim ds As New DataSet()
        connection.Open()
        dataadapter.Fill(ds, "Authors_table")
        connection.Close()
        DataGridView1.DataSource = ds
        DataGridView1.DataMember = "Authors_table"
    End Sub
End Class

Sumber: http://vb.net-informations.com/datagridview/vb.net_datagridview_binding.htm

avatar keccikun
@keccikun

368 Kontribusi 197 Poin

Dipost 6 tahun yang lalu

kok vb di tag vue sih ??

avatar Itsdonnisnoni
@Itsdonnisnoni

55 Kontribusi 31 Poin

Dipost 6 tahun yang lalu

Login untuk ikut Jawaban