从vb.net中的数据网格视图更新数据库

时间:2018-07-16 10:26:48

标签: vb.net datagridview datatable sql-update

我有一个数据网格视图,由数据库中的数据填充,现在我需要更改数据并更新数据库。

 Private daVD As New OracleDataAdapter
    Private cmdJV As New OracleCommandBuilder
    Private dtVD As New DataTable()



 Dim cmd As New OracleCommand("Select JV_CODE,JV_ACC_NAME,DEBIT,CREDIT From VOUCHER_DETAIL where VOUCHERNO =:Vno", sgcnn)
            cmd.Parameters.Add("@Vno", OracleDbType.NVarchar2).Value = txtJVNo.Text.ToString.Trim


            dtVD.Clear()
            daVD = New OracleDataAdapter(cmd)
            daVD.Fill(dtVD)
            dgvAccDetail.AutoGenerateColumns = False
            dgvAccDetail.DataSource = dtVD



            If dtVD.Rows.Count > 0 Then
                For i As Integer = 0 To dtVD.Rows.Count - 1
                    ''Dim DataType() As String = myTableData.Rows(i).Item(1)
                    dgvAccDetail.Rows(i).Cells(0).Value = dtVD.Rows(i).Item("JV_CODE").ToString.Trim
                    dgvAccDetail.Rows(i).Cells(1).Value = dtVD.Rows(i).Item("JV_ACC_NAME").ToString.Trim
                    dgvAccDetail.Rows(i).Cells(2).Value = dtVD.Rows(i).Item("DEBIT").ToString.Trim
                    dgvAccDetail.Rows(i).Cells(3).Value = dtVD.Rows(i).Item("CREDIT").ToString.Trim
                Next
            End If

我尝试了多种更新数据库表的方法,但是不起作用

      Try
        cmdJV = New OracleCommandBuilder(da)
       daJV.Update(ds, "VJ_Details")
        ds.AcceptChanges()
        MessageBox.Show(" The record has been updated.")
    Catch ex As Exception

        MsgBox(ex.Message)

    End Try 

1 个答案:

答案 0 :(得分:0)

我从未使用过Oracle,但这是您可以使用SQL Server进行的操作(实际上有几种方法可以将数据从DatGridView推送到SQL Server中的表中。)

这是您尝试的一种选择。

Imports System.Data.SqlClient
Public Class Form1
    Dim connetionString As String
    Dim connection As SqlConnection
    Dim adapter As SqlDataAdapter
    Dim cmdBuilder As SqlCommandBuilder
    Dim ds As New DataSet
    Dim changes As DataSet
    Dim sql As String
    Dim i As Int32

    Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        connetionString = "Data Source=Server_Name\SQLEXPRESS;Initial Catalog=Test;Trusted_Connection=True;"
        connection = New SqlConnection(connetionString)
        sql = "Select * from Orders"
        Try
            connection.Open()
            adapter = New SqlDataAdapter(Sql, connection)
            adapter.Fill(ds)
            DataGridView1.DataSource = ds.Tables(0)
            connection.Close()
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'NOTE:  for this code to work, there must be a PK on the Table
        Try
            cmdBuilder = New SqlCommandBuilder(adapter)
            changes = ds.GetChanges()
            If changes IsNot Nothing Then
                adapter.Update(changes)
            End If
            MsgBox("Changes Done")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles DataGridView1.Click
        DataGridView1.DefaultCellStyle.SelectionBackColor = Color.Orange
    End Sub
End Class

这是您考虑的另一种选择。

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

tblReadCSV.Columns.Add("FName")
tblReadCSV.Columns.Add("LName")
tblReadCSV.Columns.Add("Department")

Dim csvParser As New TextFieldParser("C:\Users\Excel\Desktop\Employee.txt")

csvParser.Delimiters = New String() {","}
csvParser.TrimWhiteSpace = True
csvParser.ReadLine()

While Not (csvParser.EndOfData = True)
    tblReadCSV.Rows.Add(csvParser.ReadFields())
End While

Dim con As New SqlConnection("Server=Server_Name\SQLEXPRESS;Database=Database_Name;Trusted_Connection=True;")
Dim strSql As String = "Insert into Employee(FName,LName,Department) values(@Fname,@Lname,@Department)"
'Dim con As New SqlConnection(strCon)
Dim cmd As New SqlCommand()
cmd.CommandType = CommandType.Text
cmd.CommandText = strSql
cmd.Connection = con
cmd.Parameters.Add("@Fname", SqlDbType.VarChar, 50, "FName")
cmd.Parameters.Add("@Lname", SqlDbType.VarChar, 50, "LName")
cmd.Parameters.Add("@Department", SqlDbType.VarChar, 50, "Department")

Dim dAdapter As New SqlDataAdapter()
dAdapter.InsertCommand = cmd
Dim result As Integer = dAdapter.Update(tblReadCSV)

End Sub