Home > Software design >  Creating XML file from legay application
Creating XML file from legay application

Time:11-29

I have an old application that I wrote in Access VBA, the time has come to upgrade the code and the company decided to go with C# since we use it the most. My question is following, I have this code in VBA that works great,

Set RS2 = Db.OpenRecordset("Select * FROM TTable WHERE ID="&Forms![test]![SifraFirme]&")
su = RS2.RecordCount
RS2.MoveFirst
Do While Not RS2.EOF
//lines of code
RS3.MoveNext
Loop
RS3.Close

Now my question is, is there a C# command similar to Do While Not RS.EOF, any literature or examples would be highly appreciated. Just a nudge in the right direction because it has become frustrating. The main point of code above is to go through the table and filter the data and write it to XML (predefined structure) based on ID once he is done with first, move on to the second, and ...

Thank you,

CodePudding user response:

C# has the XMLWriter class and you can use the SQL classes for querying and reading the information.

CodePudding user response:

The while loop in C# would be something like this:

while (!RS2.EOF)
{
    //lines of code
    RS2.MoveNext();
}

The ! is the Logical negation operator.

CodePudding user response:

ADO.NET has the DataSet class which works with data in a way that is similar to a RecordSet in VBA.

See Microsoft's documentation on DataSet

CodePudding user response:

Answering to:

The main point of code above is to go through the table and filter the data and write it to XML

You can read database table to some DataSet, using OleDbDataAdapter from System.Data namespace. Then easily work with filled DataSet or instantly get its XML representation by GetXml method:

static void Main(string[] args)
{
    // Note about set Prefer 32-bit app version of your C# app to use Jet.OLEDB provider
    var connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=YourDBPath";
    var query = "Select * FROM TTable";

    // Introducing our DataSet
    var dataSet = new System.Data.DataSet();

    using (var connection = new System.Data.OleDb.OleDbConnection(connectionString))
    {
        var command = new System.Data.OleDb.OleDbCommand(query, connection);

        try
        {
            connection.Open();
 
            using (var dataAdapter = new System.Data.OleDb.OleDbDataAdapter(command))
            {
                // Fill DataSet
                dataAdapter.Fill(dataSet);
            }

            // Get XML representation of DataSet and save to XML file
            System.IO.File.WriteAllText(@"TTable.xml", dataSet.GetXml());

            // Or if need to filter data before save - read through DataSet
            var TTable = dataSet.Tables["TTable"];
            foreach (var row in TTable.Rows.Cast<System.Data.DataRow>().ToArray()) // using System.Linq needed
            {
                    
            }
        }
        catch (System.Exception ex)
        {
            // Handle exception in some way
            System.Console.WriteLine(ex.Message);
        }  
    }
      
    System.Console.ReadKey();
}
  • Related