Solved

Class to database

Posted on 2006-06-12
3
221 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

820 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