Solved

Class to database

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
how to add field in my script 2 24
Encryption Decryption in Oracle 12 65
VB.Net - CSV to Oracle table 4 32
Round up to 100% in .NET 10 46
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…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

9 Experts available now in Live!

Get 1:1 Help Now