通过VB.net中的DataGridView更新SQL表

时间:2014-07-27 20:56:10

标签: vb.net datagridview database-connection

好的,可能是这里曾经问到的最愚蠢的问题之一,但我已经在这个问题上苦苦挣扎了几个小时。虽然在我的辩护中,我只是一个vb.net程序员大约3个小时所以.......

我想更新一个SQL表。我已设法将SQL查询的内容检索到DataGridView中。我还设法得到一个单独的查询来填充DGV中的组合框。

然而,我正在努力更新表...它看起来似乎没有数据连接......查询在大约30秒后超时并且有一些非常长的消息但我总结为“服务器不是发现或无法访问。“

这是包含DGV的表单类:

代码:

Imports System.Data.SqlClient


Public Class Form1
    Public query As New Query
    Private con As New SqlConnection("Data Source=SQL-SERVER;Initial Catalog=Expenses;Integrated Security=True")

在表单加载上跟着这个(这里的SQL工作正常):

代码:

  Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'CREATE CHECKBOX
        Dim chk As New DataGridViewCheckBoxColumn() With {.HeaderText = "SELECT", .Name = "chk", _
                                                          .AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCells, _
                                                          .FlatStyle = FlatStyle.Standard, .ThreeState = False}
        'CREATE COMBOBOX
        Dim CMD As New SqlCommand
        Dim cbb As New DataGridViewComboBoxColumn() With {.HeaderText = "CATEGORY"}
        CMD.Connection = con
        CMD.CommandText = "SELECT Category FROM InformationCategory"
        con.Open()


        Dim Reader As SqlDataReader
        Reader = CMD.ExecuteReader


        While Reader.Read
            cbb.Items.Add(Reader.Item("Category"))
        End While


        con.Close()


        'ADD BUTTON
        Dim btnDelete As New DataGridViewButtonColumn() With {.HeaderText = "DELETE", .Text = "DELETE"}


        'DataGridView1.Columns.Insert(0, chk)
        DataGridView1.Columns.Add("", "ID")
        DataGridView1.Columns.Add("", "DATE")
        DataGridView1.Columns.Add("", "CARD NO.")
        DataGridView1.Columns.Add("", "DESCRIPTION")
        DataGridView1.Columns.Add("", "VALUE")
        'DataGridView1.Columns.Add("", "VALUE")
        DataGridView1.Columns.Insert(4, cbb)
        'DataGridView1.Columns.Insert(6, btnDelete)
        'DataGridView1.Columns.Add("", "ID")
        DataGridView1.Columns.Add("", "COMMENTS")


        DataGridView1.Columns(0).Width = 30
        DataGridView1.Columns(1).Width = 70
        DataGridView1.Columns(2).Width = 100
        DataGridView1.Columns(3).Width = 500
        DataGridView1.Columns(4).Width = 205
        DataGridView1.Columns(5).Width = 70
        DataGridView1.Columns(6).AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill


        RemoveHandler DataGridView1.CellValueChanged, AddressOf DataGridView1_CellValueChanged
        FillGrid()
        AddHandler DataGridView1.CellValueChanged, AddressOf DataGridView1_CellValueChanged


    End Sub

虽然这不起作用......:

代码:

Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged


        Dim sql As String


        sql = "UPDATE Database.dbo.Table SET COMMENTS='" + DataGridView1.CurrentRow.Cells(6).Value + "'"


        MessageBox.Show("row index: " & e.RowIndex & Environment.NewLine & "old value: " & cellValue)


        MessageBox.Show(sql)


        con.Open()


        query.ExcuteQuery(sql)


        con.Close()
    End Sub

提前为这个愚蠢的问题道歉。

0 个答案:

没有答案