Solved

Class to database

Posted on 2006-06-12
3
217 Views
Last Modified: 2012-05-05
I have an array of Class. How can I create a table in an Access database with the content?

My class lets say for simplicity has two string variables as follows:

Public Class Test1
  Public Field1 As String
  Public Field2 As String
End Class

Code:

Dim ArrT(10000) As Test1
Dim objTest1 as new Test1
Dim objTest2 as new Test1

objTest1.field1=a
objTest1.field2=b

objTest2.field1=c
objTest2.field2=d

ArrT(0)=objTest1
ArrT(1)=objTest2

Now that I have a full Array of a class, is it possible to create a table in Microsoft Access and write the contents to it?
Thanks

0
Comment
Question by:Midnight2005
  • 2
3 Comments
 
LVL 19

Expert Comment

by:arif_eqbal
ID: 16891787
OK Here is a sample code

Dim ConStr As String = "Provider=....... Your Connection String"
Dim QStr as String
Dim Cn As New OleDbConnection (ConStr)
Dim Cmd As New OleDbCommand("Create Table DynamicTable (Field1 Text(100), Field2 Number)", Cn)
   'I have taken a text and a Numeric field, First Col would be of type Text of size 100 second is Numeric
Cn.Open
Try
Cmd.ExecuteNonQuery  'Table Created Here
For i As Integer=0 To ArrT.Length - 1
     QStr=String.Format("Insert Into DynamicTable (Field1,Field2) Values ('{0}', {1})", ArrT.field1, ArrT.field2)
     Cmd.ExecuteNonQuery
Next
Cn.Close
Cn.Dispose
Cmd.Dispose

That's it
One more thing, since I have looped for all the elements in the array ArrT make sure all the elements of the array are initialized. your eg above shows array of size 10000, so if you really have 10,000 elements make sure they all have values.
0
 
LVL 34

Accepted Solution

by:
Sancler earned 300 total points
ID: 16892383
There'd be a couple of problems with this code

For i As Integer=0 To ArrT.Length - 1
     QStr=String.Format("Insert Into DynamicTable (Field1,Field2) Values ('{0}', {1})", ArrT.field1, ArrT.field2)
     Cmd.ExecuteNonQuery
Next

First, Cmd is not given the new CommandText.  Second, ArrT is not given any index.  I think it should be

For i As Integer = 0 To ArrT.Length - 1
     QStr = String.Format("Insert Into DynamicTable (Field1,Field2) Values ('{0}', {1})", ArrT(i).Field1, ArrT(i).Field2)
     Cmd.CommandText = QStr
     Cmd.ExecuteNonQuery()
Next

And the Try/Catch block is not completed.

Otherwise, I agree ;-)

Roger
0
 
LVL 19

Assisted Solution

by:arif_eqbal
arif_eqbal earned 200 total points
ID: 16892700
I am extremely sorry
I just typed out the code here and forgot it
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now