简体   繁体   中英

Visual Basic 2010 connecting to my database

Private Sub OK_Click(sender As System.Object, e As System.EventArgs) Handles OK.Click

    If TextBox1.Text = "1234" Then



        ' This is the connection. You have to have this exact string, except "E:\Documents\notekeeper.mdb" will be the path to your thing instead

        Dim conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=N:\Visual Studio 2010\Projects\Maths System Aid\Maths System Aid\Database7.mdb;User=;Password=;")
        Try
            conn.Open()
        Catch ex As Exception
            MsgBox("Cannot open database")
        End Try



        ' The SQL statement / command

        Dim cmd = New OleDbCommand("Insert INTO Student ([First Name], [Surname], [Username], [Password]) VALUES "("" & TextBox5.Text & "," & TextBox4.Text & "," & TextBox3.Text & "," & TextBox2.Text & "" & ")"), conn)







        cmd.ExecuteNonQuery() ' Use ExecuteReader() to execute SELECT statements, but ExecuteNonQuery() for others






        ' Basically, the reader is like an array of all of the records that have been returned by the database


        Me.Close()
        StudentLogin.Show()

    Else
        MsgBox("Enter The Correct Confirmation code")

    End If
End Sub

my problem is that it will not find my database file. I have followed the path and it is correct. Any ideas of what is the problem?

I haven't done this in quite some time but at the top of your form you should be running some imports firstly

                                        imports system.data 

Then you should define rather like this:

                                dim conn as new oledb.oledbconnection

Another issue I noticed in your code is that you're using the Jet provider which if memory serves correctly only works with the new databases using the .accdb extension. Try to change it to an accdb in access.. I'll go over it quick and see what I can conjure up. Hopefully that helps at least. The alternative provider if you run into issues :

                                ("Provider=Microsoft.ACE.OLEDB.12.0.......

Is this path a local disk or a network disk? is it on another computer or a mapped network drive?

The way I connect is

Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String

dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbSource = "Data Source = C:\data\database.mdb;Jet OLEDB:Database Password=******;"
con.ConnectionString = dbProvider & dbSource
con.open()
con.close()

This should work perfect but use your path and password if you have one

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM