Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Class to database

Posted on 2006-06-12
3
Medium Priority
?
225 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 1200 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 800 total points
ID: 16892700
I am extremely sorry
I just typed out the code here and forgot it
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

604 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