Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 24th, 2007, 08:26 AM
Authorized User
 
Join Date: Sep 2007
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.
 
Old September 24th, 2007, 09:09 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
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
 
Old September 24th, 2007, 09:50 AM
Authorized User
 
Join Date: Sep 2007
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....
 
Old September 24th, 2007, 12:45 PM
Authorized User
 
Join Date: Sep 2007
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()
.





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





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