vb.net (2008)met een data conectie naar een acces file

Status
Niet open voor verdere reacties.

trical

Gebruiker
Lid geworden
17 mrt 2007
Berichten
40
Code:
Imports System.Data.OleDb
Imports System.Data.SqlClient


Public Class Form1
    Dim ConnString As String
    Dim SQLStr As String
    Dim SQLConn As New SqlConnection() 'The SQL Connection
    Dim SQLCmd As New SqlCommand() 'The SQL Command
    Private Sub count()
        Dim records, current As Integer
        records = Me.BindingContext( _
        DataSet11, "students").Count
        current = Me.BindingContext( _
        DataSet11, "students").Position + 1
        Label1.Text = "record " & current.ToString & " of " & _
        records.ToString
    End Sub

    Private Sub zoek_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles zoek.Click
        DataSet11.Clear()
        OleDbDataAdapter1.Fill(DataSet11)
        count()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.BindingContext(DataSet11, "students").Position = 0
        count()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Connstring = Server Name, Database Name, Windows Authentication
        ConnString = "wat moet hier komen?;Initial Catalog=students;User ID=Admin;Password="
        SQLConn.ConnectionString = ConnString 'Set the Connection String
        SQLConn.Open() 'Open the connection
        SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
        SQLCmd.CommandText = SQLStr 'Sets the SQL String
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.BindingContext(DataSet11, "students").Position = _
        Me.BindingContext(DataSet11, "students").Count - 1
        count()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Me.BindingContext(DataSet11, "students").Position += 1
        count()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.BindingContext(DataSet11, "students").Position -= 1
        count()
    End Sub

    Private Sub StudentsBindingSource_CurrentChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StudentsBindingSource.CurrentChanged
    End Sub

    Private Sub OleDbDataAdapter1_RowUpdated(ByVal sender As System.Object, ByVal e As System.Data.OleDb.OleDbRowUpdatedEventArgs) Handles OleDbDataAdapter1.RowUpdated

    End Sub

    Private Sub OleDbConnection1_InfoMessage(ByVal sender As System.Object, ByVal e As System.Data.OleDb.OleDbInfoMessageEventArgs) Handles OleDbConnection1.InfoMessage

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Dim naam, voornaam, leeftijd As String
        naam = TextBox6.Text
        voornaam = TextBox5.Text
        leeftijd = TextBox4.Text

        SQLStr = "INSERT into students(id, naam, voornaam, leeftijd, foto) VALUES('10', 'HoSQL?', 'blubber', '4','sdf' )"
        SQLCmd.CommandText = SQLStr 'Sets the SQL String
        SQLCmd.ExecuteNonQuery() 'Executes SQL Commands Non-Querys only

    End Sub
End Class
het probleem zit hem in de "wat moet er hier komen"(of toch in die lijn:)) , de rest zou moeten werken want ik krijg een foutmelding dat de connection niet kan geopend worden

dit stuk dus:
Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Connstring = Server Name, Database Name, Windows Authentication
        ConnString = "wat moet hier komen?;Initial Catalog=students;User ID=Admin;Password="
        SQLConn.ConnectionString = ConnString 'Set the Connection String
        SQLConn.Open() 'Open the connection
        SQLCmd.Connection = SQLConn 'Sets the Connection to use with the SQL Command
        SQLCmd.CommandText = SQLStr 'Sets the SQL String
    End Sub
 
Laatst bewerkt:
Je provider...,

Voor Microsoft Access
Provider=Microsoft.Jet.OLEDB.4.0;

heb je een database zonder passwoord, dan heb je UserID en Pass niet nodig.

Code:
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test\demo.mdb;"

Mocht je met een bestandsnaam in een variable werken let dan goed op de aanhalingstekens.

Code:
ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & mijn bestandnaam.txt & ";"
 
Laatst bewerkt:
nog een fout -->zie bijlage
 

Bijlagen

  • connstring.jpg
    connstring.jpg
    110,6 KB · Weergaven: 29
Ik kan zo niet ontdekken wat SQLConn is voor een object. Maar ik denk dat je een connectie wilt openen zonder een SQL-query.

probeer eens dit , (mijntabelinaccess invullen naar je eigen tabel):

Code:
                Dim SQLquery As String = "SELECT * FROM mijntabelinaccess;"
        Dim ReadConn As New OleDb.OleDbCommand(SQLquery, New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\test\test.mdb;"))

        ReadConn.Connection.Open()

        Dim dr As System.Data.OleDb.OleDbDataReader = ReadConn.ExecuteReader

        dr.Read()

        Debug.Print(dr.ToString)

        ReadConn.Connection.Close()
        ReadConn.Connection.Dispose()

het eerst item in de tabel zou nu moeten worden uitgespuugt via de 'Immediate Window'
 
Dim SQLConn As New SqlConnection() 'The SQL Connection

en ik zou mijn database(acces office 07) moeten bewerken met sql query's
 
Dim SQLConn As New SqlConnection , je hebt geen SQLserver dus vandaar dat je provider niet wordt herkent in je connectiestring.

Voor een losse database gebruik je meestal een OLEdb of indien lokaal gekoppeld in ODBC (odbcad32.exe) een ODBC connection.
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan