Read / Write Access Tables
So I have a database in Microsoft Access (its path is c:\database.mdb). There are two tables. Table1 and Table2. I would like to read each row of Table1 to an array, and write a row to Table2.
I have looked all over google, but I couldn't find anything that worked.
Re: Read / Write Access Tables
So what have you tried so far? Do you expect us to do it for you?
Re: Read / Write Access Tables
I have tried:
http://www.startvbdotnet.com/ado/msaccess.aspx
http://www.java2s.com/Code/VB/Databa...sdatatable.htm
http://www.daniweb.com/forums/thread93667.html#
Dim theOleDbCommand As New OleDbCommand("SELECT Department FROM Table1", New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\database.mdb;"))
theOleDbCommand.Connection.Open()
Dim theReader As OleDbDataReader = theOleDbCommand.ExecuteReader(CommandBehavior.CloseConnection)
While theReader.Read() = True
Console.WriteLine(theReader(0).ToString())
End While
None of those solutions seemed to work for me. The code wouldn't be valid or there would be other errors.
Re: Read / Write Access Tables
So we don't know what errors you got, what you actually typed into the program.....
OK
Have you done the tutorials here (I mean on this forum)?
http://www.vbforums.com/showthread.php?t=337051#Net
Re: Read / Write Access Tables
Quote:
Originally Posted by
GaryMazzone
basically I copied and pasted the code that was on the sites and changed the database location / other minor things to make it fit my scenario.
I will take a look at the tutorials now.
Re: Read / Write Access Tables
That is never a good idea. You read and look over the code and try to duplcate it. That way you learn what it is actually doing.
Re: Read / Write Access Tables
Ok, I figured out the reading part. now using the below code I am trying to write to the database.
vb Code:
Using connection As New OleDbConnection(myConnectionString)
Using adapter As New OleDbDataAdapter("SELECT NameFirst, NameLast FROM Employees", _
connection)
Dim insert As New OleDbCommand("INSERT INTO Employees (NameFirst, NameLast) VALUES (@NameFirst, @NameLast)", _
connection)
adapter.InsertCommand = insert
adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey
Dim table As New DataTable
adapter.FillSchema(table, SchemaType.Source)
Dim row As DataRow = table.NewRow()
row("NameFirst") = aName(0)
row("NameLast") = aName(1)
table.Rows.Add(row)
adapter.Update(table)
End Using
End Using
I am getting an error with this line:
adapter.Update(table)
error:
No value given for one or more required parameters.
EDIT: I should note that the first column is auto numbered.
EDIT2: I tried a different table without the auto numbered column and it didn't change anything
Re: Read / Write Access Tables