Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 24th, 2007, 08:26 AM
Authorized User
Points: 386, Level: 6
Points: 386, Level: 6 Points: 386, Level: 6 Points: 386, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2007
Location: windsor, toronto, Canada.
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default loop through tables inside .mdb file using VB.NET

i'm using VB.NET and Microsoft Access.

and in one of my Access table is called Emp.mdb. and in that Emp.mdb i have lots of tables like MD1, MD2,......MD58 and MB1,MB2,......MB67 and some other tables too.

so when a button is pressed i need to check all the tables MD# and MB# and retrive some data to create an XML. and other tables were also there, but i don't need to check that tables. and the Number of MD tables and MB tables varies. so i need to check all the tables with MB# and MB#.


for checking Single table i used this code. how can i loop through all the tables MB# and MD# in Emp.mdb.


Code:
TextBox1.Text = "C:\Program\Emp.mdb"
If File.Exists(TextBox1.Text) Then
            Dim strSQL As String = "Select ItemID,pl11,pl12 from MG10 where ItemID <> 0"
            Dim myConnection As New OleDbConnection(strConn)
            myConnection.Open()
            Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection)
            Dim myReader As OleDbDataReader = myCommand.ExecuteReader

            Dim myXWriter As XmlTextWriter
            Dim myWriter As StreamWriter
            Dim myStream As MemoryStream
            myStream = New MemoryStream

            myXWriter = New XmlTextWriter(myStream, Encoding.UTF8)
            myXWriter.Formatting = Formatting.Indented
            myXWriter.Indentation = 2

            myXWriter.WriteStartDocument()
            myXWriter.WriteStartElement("Employee")

           While myReader.Read
                myXWriter.WriteStartElement("Item")
                myXWriter.WriteAttributeString("CV", myReader(0))
                myXWriter.WriteAttributeString("PL1", myReader(1))
               myXWriter.WriteAttributeString("PL2", myReader(2))
                myXWriter.WriteEndElement()
            End While

            myXWriter.WriteFullEndElement()
            myXWriter.WriteFullEndElement()
            myXWriter.WriteEndDocument()
            myXWriter.Flush()

           myStream.Seek(0, SeekOrigin.Begin)

            Dim strConfig2 As String = New StreamReader(myStream).ReadToEnd()
            myWriter = File.CreateText("C:\Example.xml")
            myWriter.WriteLine(strConfig2)
            myWriter.Close()
        End If



if you have any idea please let me know. and if you can provide an example then it will be great helpfull for me.

thanks in advanace.
Reply With Quote
  #2 (permalink)  
Old September 24th, 2007, 09:09 AM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

If you were in MS Sql Server you could query the schema tables to get a list of all the tables that start with "MB" or "MD". I'm not sure how you can query the schema of an Access database however. If you can find that out then it would be a good approach. In the mean time you could simply loop and try hitting a table with the name created by the loop (i.e. MB1, MB2, ...). Once you hit a table that doesn't exist the query will throw an exception. So you put all that code inside a try..catch and handle it gracefully. The only catch with this solution is that you assume that there will always be a sequential set of tables with no breaks in the number sequence.

-Peter
Reply With Quote
  #3 (permalink)  
Old September 24th, 2007, 09:50 AM
Authorized User
Points: 386, Level: 6
Points: 386, Level: 6 Points: 386, Level: 6 Points: 386, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2007
Location: windsor, toronto, Canada.
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

so by using MS Access whether i can query the schema tables to get a list of all the tables satrting with MD and MB. i will try to search that.... and thanks for your help....

the MD# and MB# tables were non Sequential set of tables. right now in that table i have MD1,MD2,.....MD60,MD667,MD888 etc and MB1,MB2,....,MB60,MB888 etc... so after MD60 its Md667 and after that MD888. so i can't loop through as sequential.

if you have any more idea please help me.

thanks for your help....
Reply With Quote
  #4 (permalink)  
Old September 24th, 2007, 12:45 PM
Authorized User
Points: 386, Level: 6
Points: 386, Level: 6 Points: 386, Level: 6 Points: 386, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2007
Location: windsor, toronto, Canada.
Posts: 62
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Planoie for the helpit really helped me lots... and its working too.... Really thanks a lot for this help...

here is the code that works.... and once again thanks a lot for your help....
Code:
Dim myConnection As New OleDbConnection(strConn)      myConnection.Open()

 Dim schemaTable As DataTable
 schemaTable = myConnection.GetOleDbSchemaTable(OleDb.OleDbSchemaGuid.Tables, New Object() {Nothing, Nothing, Nothing, "TABLE"})

 Dim myXWriter As XmlTextWriter
 Dim myWriter As StreamWriter
 Dim myStream As MemoryStream

 myStream = New MemoryStream
 myXWriter = New XmlTextWriter(myStream, Encoding.UTF8)

 myXWriter.Formatting = Formatting.Indented
 myXWriter.Indentation = 2

 Dim i As Integer
 For i = 0 To schemaTable.Rows.Count - 1
 Dim pTableName As String = schemaTable.Rows(i)!TABLE_NAME.ToString
 If Microsoft.VisualBasic.Left(pTableName, 2) = "MD" Then
     Dim strSQL As String = "Select ItemID,pl1 from " & pTableName & " where ItemID <> 0"
     Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection)
     Dim myReader As OleDbDataReader = myCommand.ExecuteReader
     While myReader.Read
          myXWriter.WriteStartElement("MenuItem")
          myXWriter.WriteAttributeString("CV", myReader(0))
          myXWriter.WriteAttributeString("PL1", myReader(1))
          myXWriter.WriteEndElement()
     End While
     myReader.Close()
 ElseIf Microsoft.VisualBasic.Left(pTableName, 2) = "MB" Then
     Dim strSQL As String = "Select ItemID,pl1 from " & pTableName & " where ItemID <> 0"
     Dim myCommand As OleDbCommand = New OleDbCommand(strSQL, myConnection)
     Dim myReader As OleDbDataReader = myCommand.ExecuteReader
     While myReader.Read
         myXWriter.WriteStartElement("MenuItem")
         myXWriter.WriteAttributeString("CV", myReader(0))
         myXWriter.WriteAttributeString("PL1", myReader(1))
         myXWriter.WriteEndElement()
     End While
     myReader.Close()
  End If
  Next

 myXWriter.WriteEndDocument()
 myXWriter.Flush()

 myStream.Seek(0, SeekOrigin.Begin)
 Dim strConfig2 As String = New StreamReader(myStream).ReadToEnd()
 myWriter = File.CreateText("C:\Program\Example.xml")
 myWriter.WriteLine(strConfig2)
 myWriter.Close()
.
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FTP from inside VB.NET Program fig000 Pro VB.NET 2002/2003 33 June 11th, 2011 03:56 PM
re-link tables in a .mde when .mdb does not exist poyserr Access 1 February 22nd, 2007 12:49 PM
Pass variables from array inside a loop dustygn PHP How-To 2 May 3rd, 2006 05:35 PM
Query to extract data from 2 tables of .mdb file Subuana Beginning VB 6 6 March 16th, 2006 04:37 PM
Unique Control IDs inside a loop using a varible Joshua Sebastian Classic ASP Basics 2 February 3rd, 2005 10:44 AM



All times are GMT -4. The time now is 12:02 PM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.