none
刪除資料庫資料 RRS feed

  • 問題

  • 請問各位大大,我在vb下設計了一個開改mdb的功能,有查詢、新增、刪除的選項,而查詢、新增的功能都能正常運作,可是就是刪除的部份一直出錯,我找了許久就是找不出我那寫錯了,麻煩各位大大高手給我點意見吧,謝謝
    Imports System.Data.OleDb
    Public Class DataAdapter

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim oledbl As New OleDbConnection
            Dim oledataset1 As New DataSet
            oledbl.ConnectionString = "provider = microsoft.jet.oledb.4.0; data source=x:\125.mdb"
            oledbl.Open()
            If ConnectionState.Open Then
                Dim oledataAdapterl As OleDbDataAdapter = New OleDbDataAdapter("select * from 技術業務部", oledbl)
                oledataAdapterl.Fill(oledataset1, "技術業務部")
                DataGridView1.DataSource = oledataset1.Tables("技術業務部")
                oledbl.Close()
            End If
        End Sub

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

        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim oledbl As New OleDbConnection
            oledbl.ConnectionString = "provider = microsoft.jet.oledb.4.0; data source=x:\125.mdb"
            oledbl.Open()
            If ConnectionState.Open Then
                Dim cmdl As New OleDbCommand
                cmdl.Connection = oledbl
                cmdl.CommandText = "Insert into 技術業務部 (識別碼, 人員名稱) values ('" + TextBox1.Text + "'" + ",'" + TextBox2.Text + "')"
                cmdl.ExecuteNonQuery()
                oledbl.Close()
            End If
        End Sub

        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            Dim oledbl As New OleDbConnection
            oledbl.ConnectionString = "provider = microsoft.jet.oledb.4.0; data source=x:\125.mdb"
            oledbl.Open()
            If ConnectionState.Open Then
                Dim cmdl As New OleDbCommand
                cmdl.Connection = oledbl
                cmdl.CommandText = "delete From 技術業務部 Where 識別碼='TextBox1.Text'+人員名稱='TextBox2.Text'"
                cmdl.ExecuteNonQuery()
                oledbl.Close()
            End If
        End Sub
    End Class

    2010年2月25日 上午 08:49

解答


  • 比對每個欄位資料庫的型別,比如說您的 [識別碼] 是數字,則 識別碼=3 而不是 識別碼='3'



    小歐ou是個菜鳥工程師,回答不好請不要標我的回覆為解答,有網友熱心回覆也請發問者儘快回應,謝謝
    歡迎參觀我的Blog.NET菜鳥自救會
    • 已標示為解答 郭大雄 2010年2月25日 上午 09:14
    2010年2月25日 上午 09:10
    版主

所有回覆