Solved

Class to database

Posted on 2006-06-12
3
222 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Report Viewer in VS2013 6 32
vb.net code 9 28
VB .net 2010 Byte array 2 20
Javascript function 3 12
This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

739 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