创建新表

时间:2014-03-21 16:35:44

标签: database vb.net

如何使用vb.net代码在访问数据库中添加新表。 我应该编写一个代码,当我要添加一个新文件时,它将在我的数据库中拥有自己的表。 这是我的代码:

Public Class Form3
    Dim con As New OleDb.OleDbConnection
    Dim dbprovider As String
    Dim dbsource As String
    Dim ds As New DataSet
    Dim da As New OleDb.OleDbDataAdapter
    Dim sql As String
    Dim inc As Integer
    Dim intDB_ID_Selected As Integer
    Dim ocmd As OleDb.OleDbCommand
    Dim odatareader As OleDb.OleDbDataReader
    Dim state As Integer
    Dim rownumber As Integer
    Dim intCurIndex As Integer


    Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'SofEngdbDataSet.DentalRecords' table. You can move, or remove it, as needed.

        dbprovider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
        dbsource = "Data Source = C:\Users\AllyzaJane\Desktop\Finals\SofEngdb.mdb"
        con.ConnectionString = dbprovider & dbsource

        con.Open()
        sql = "SELECT * FROM DentalRecords"
        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "SofEngdb")

        tb_fname.ReadOnly = True
        tb_minitial.ReadOnly = True
        tb_lname.ReadOnly = True
        tb_add.ReadOnly = True
        tb_num.ReadOnly = True
        rtb_remarks.ReadOnly = True
        tb_search.ReadOnly = False

        b_add.Enabled = True
        b_cancel.Enabled = True
        b_save.Enabled = False
        b_search.Enabled = True
        b_delete.Enabled = False
        b_update.Enabled = False

    End Sub

    Private Sub b_cancel_Click(sender As System.Object, e As System.EventArgs) Handles b_cancel.Click


        b_save.Enabled = False
        b_add.Enabled = True
        b_update.Enabled = False
        b_delete.Enabled = False
        b_search.Enabled = True

        MsgBox("Cancelled")
        tb_search.ReadOnly = False
        cleartextvalue()
    End Sub

    Private Function DentalRecordsTableAdapter() As Object
        Throw New NotImplementedException
    End Function

    Private Function SampledDataSet() As Object
        Throw New NotImplementedException
    End Function

    Private Sub b_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_update.Click
        state = 0
        tb_fname.ReadOnly = False
        tb_fname.ReadOnly = False
        tb_minitial.ReadOnly = False
        tb_lname.ReadOnly = False
        tb_num.ReadOnly = False
        tb_add.ReadOnly = False
        rtb_remarks.ReadOnly = False
        tb_search.ReadOnly = True

        b_add.Enabled = False
        b_save.Enabled = True
        b_cancel.Enabled = True
        b_delete.Enabled = False
        b_search.Enabled = False
        b_update.Enabled = False

    End Sub

    Private Sub b_add_Click(sender As System.Object, e As System.EventArgs) Handles b_add.Click
        state = 1

        b_add.Enabled = False
        b_search.Enabled = False
        b_delete.Enabled = False
        b_update.Enabled = False
        b_save.Enabled = True
        b_cancel.Enabled = True
        tb_fname.ReadOnly = False
        tb_minitial.ReadOnly = False
        tb_lname.ReadOnly = False
        tb_num.ReadOnly = False
        tb_add.ReadOnly = False
        rtb_remarks.ReadOnly = False
        cleartextvalue()

    End Sub

    Private Sub b_save_Click(sender As System.Object, e As System.EventArgs) Handles b_save.Click
        b_add.Enabled = True
        b_save.Enabled = False
        b_search.Enabled = True

        ' for ADD
        If state = 1 Then

            If tb_fname.Text = "" Or tb_minitial.Text = "" Or tb_lname.Text = "" Or tb_num.Text = "" Or tb_add.Text = "" Or rtb_remarks.Text = "" Then
                MessageBox.Show("Fillup all the information needed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else
                Try
                    con.Close()
                    con.Open()
                    sql = "INSERT INTO DentalRecords (fname,minitial,lname,cnumber,address,remarks) values ('" & tb_fname.Text & "','" & tb_minitial.Text & "','" & tb_lname.Text & "','" & tb_num.Text & "','" & tb_add.Text & "','" & rtb_remarks.Text & "')"
                    ocmd = New OleDb.OleDbCommand(sql, con)
                    odatareader = ocmd.ExecuteReader()



                Catch ex As Exception

                    MessageBox.Show(ex.Message)
                End Try
                MessageBox.Show("Succesfully added.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                cleartextvalue()
                tb_search.ReadOnly = False
            End If

        Else

            If tb_fname.Text = "" Or tb_minitial.Text = "" Or tb_lname.Text = "" Or tb_num.Text = "" Or tb_add.Text = "" Or rtb_remarks.Text = "" Then
                MessageBox.Show("Fillup all the information needed.", "Personal Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Else

                sql = "UPDATE DentalRecords set lname = '" & tb_lname.Text & "', minitial = '" & tb_minitial.Text & "', fname = '" & tb_fname.Text & "', address = '" & tb_add.Text & "', remarks = '" & rtb_remarks.Text & "' WHERE cnumber = '" & tb_num.Text & "'"
                ocmd = New OleDb.OleDbCommand(sql, con)
                odatareader = ocmd.ExecuteReader()

                MessageBox.Show("Successfully updated.", "Personal Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
                cleartextvalue()

                b_update.Enabled = False
                b_save.Enabled = False
                b_delete.Enabled = False
                b_add.Enabled = True
                b_search.Enabled = True

            End If
            con.Close()
        End If
        tb_search.ReadOnly = False
    End Sub

    Private Sub b_delete_Click(sender As System.Object, e As System.EventArgs) Handles b_delete.Click
        Dim del As Integer
        If tb_add.Text = "" Or tb_fname.Text = "" Or tb_minitial.Text = "" Or tb_lname.Text = "" Or tb_num.Text = "" Or tb_add.Text = "" Or rtb_remarks.Text = "" Then
            MsgBox("No Info")
        Else
            Try
                con.Close()
                con.Open()
                del = MessageBox.Show("Are you sure you want to delete this file?", "Information System", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
                If del = 6 Then

                    sql = "DELETE FROM DentalRecords WHERE fname = '" & tb_fname.Text & "' AND lname = '" & tb_lname.Text & "'AND minitial = '" & tb_minitial.Text & "'AND address = '" & tb_add.Text & "'AND cnumber = '" & tb_num.Text & "' AND remarks = '" & rtb_remarks.Text & "'"
                    ocmd = New OleDb.OleDbCommand(sql, con)
                    odatareader = ocmd.ExecuteReader()
                    MessageBox.Show("Data has been deleted", "Information System", MessageBoxButtons.OK, MessageBoxIcon.Information)

                    cleartextvalue()
                End If
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        End If
    End Sub
    Sub cleartextvalue()        ' for clearing the datas after doin an operation
        tb_fname.Text = ""
        tb_minitial.Text = ""
        tb_lname.Text = ""
        tb_add.Text = ""
        tb_num.Text = ""
        rtb_remarks.Text = ""
        tb_search.Text = ""

    End Sub

    Private Sub b_search_Click(sender As System.Object, e As System.EventArgs) Handles b_search.Click
        If tb_search.Text = "" Then
            MsgBox("Not Found")
        Else
            con.Close()
            con.Open()
            sql = "SELECT * FROM DentalRecords WHERE fname LIKE '" & tb_search.Text & "'"

            ocmd = New OleDb.OleDbCommand(sql, con)
            odatareader = ocmd.ExecuteReader()
            If odatareader.HasRows Then
                Do While odatareader.Read

                    tb_fname.Text = odatareader.Item(1)
                    tb_minitial.Text = odatareader.Item(2)
                    tb_lname.Text = odatareader.Item(3)
                    tb_num.Text = odatareader.Item(5)
                    tb_add.Text = odatareader.Item(4)
                    rtb_remarks.Text = odatareader.Item(6)

                Loop

                tb_fname.ReadOnly = True
                tb_minitial.ReadOnly = True
                tb_lname.ReadOnly = True
                tb_num.ReadOnly = True
                tb_add.ReadOnly = True
                tb_search.ReadOnly = False
                rtb_remarks.ReadOnly = True
            Else
                MsgBox("Not Found")
            End If

            b_add.Enabled = True
            b_delete.Enabled = True
            b_cancel.Enabled = True
            b_update.Enabled = True

        End If
    End Sub
End Class

我想以这样的方式修改它,即在每个添加的新文件中创建一个表

0 个答案:

没有答案