0

プログラムがcmd.ExecuteNonQuery()連続してクラッシュする

Public Class Form1
    Dim conn As New OleDb.OleDbConnection
    Private Sub RefreshData()
        If Not conn.State = ConnectionState.Open Then
            'open connection
            conn.Open()
        End If

        Dim da As New OleDb.OleDbDataAdapter("SELECT Oznaka as [Oznaka], " & _
                                             "ReN as [ReN], RmN " & _
                                             " FROM Tabela ORDER BY Oznaka", conn)
        Dim dt As New DataTable
        'fill data to datatable
        da.Fill(dt)

        'offer data in data table into datagridview
        Me.DataGridView1.DataSource = dt

        'close connection
        conn.Close()
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cmd As New OleDb.OleDbCommand
        If Not conn.State = ConnectionState.Open Then
            'open connection if it is not yet open
            conn.Open()
        End If

        cmd.Connection = conn
        'check whether add new or update
        If Me.txtOznaka.Tag & "" = "" Then
            'add new
            'add data to table
            cmd.CommandText = "INSERT INTO Tabela(Oznaka, ReN, RmN) " & _
                                " VALUES('" & Me.txtOznaka.Text & "','" & Me.txtReN.Text & "','" & _
                                Me.txtRmN.Text & "'')"

            cmd.ExecuteNonQuery()
        Else
            'update data in table
            cmd.CommandText = "UPDATE Tabela " & _
                        " SET Oznaka=" & Me.txtOznaka.Text & _
                        ", ReN='" & Me.txtReN.Text & "'" & _
                        ", RmN='" & Me.txtRmN.Text & "'" & _
                                   " WHERE Oznaka=" & Me.txtOznaka.Tag
            cmd.ExecuteNonQuery()

        End If
        'refresh data in list
        RefreshData()
        'clear form
        'Me.btnClear.PerformClick()

        'close connection
        conn.Close()

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        conn = New OleDb.OleDbConnection
        conn.ConnectionString = "Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & Application.StartupPath & "\baza.mdb"
        '
        'get data into list
        Me.RefreshData()
    End Sub



End Class
4

1 に答える 1

0

クラッシュしたときに、INSERTまたはクエリを実行していますか?UPDATE

クエリの場合は INSERT、おそらく SQL 文字列'の末尾に 1 つが多すぎるためです。

... Me.txtRmN.Text & "'')"

                       ^
                       here!

UPDATEクエリの場合:Oznaka文字列列の場合は、値を引用符で囲みます:

" SET Oznaka='" & Me.txtOznaka.Text & "'" & _

それ以外の

" SET Oznaka=" & Me.txtOznaka.Text & _

これらの問題の両方に対するさらに優れた解決策は、文字列を連結する代わりにクエリで SQL パラメータを使用することです。

開始するためのいくつかの例を次に示します。

于 2013-01-16T06:06:37.757 に答える