Click here to Skip to main content
15,887,267 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi guys, this is my first project, I am a beginner programmer so my C# skills are few for now.

What I have to do is create a console application that extracts a single table, from an access database, using a SQL query in app.config, so that it is possible to change the SQL without changing the code in the application.
The extracted information has to be in a text file format. The hard part is that I have and excel file with the mapping of the that table and i only need to extract that information, and add some other.

For example I will need to create a new column that is not originally in the datatable and give it a default value. And that information is on the excel file.

Basically I already have the connections done and I extracted a table that I need from the access database, for a temporary datatable, I already have a datatable from the excel file with only the columns I will need to use in a temporary datatable.
I need to create a foreach loop to check if the A column in excel is in use, if yes then I need to check another column that contains the name of the that column in the access datatable, if there is a value I need to to extract that column to the text file, if there is no value I need to check other column that contains a default value and I need to add that value to a column that doesn't exist in the database. If there are no values in both those columns than I need to just add a blank space with the width value in the text file.

All this columns extracted to the text file need to have the width-string.width has a blank space between them and there is no need to extract the column names.

This what i have so far. It may be really wrong, so any help would be appreciated


C#
static void createfile(DataTable accessTable, DataTable excelTable)

        {
            string strFileData = "";

            foreach(DataRow accessRow in accessTable.Rows)
            {
                foreach (DataRow excelrow in excelTable.Rows)
                {
                    string fieldname = "";
                    fieldname = excelrow["FieldName"] //if it's not empty
                    strFileData.insert accessRow[fieldname];
                    string test = accessRow[fieldname.ToString()];
                    if(position == 0)
                    strFileData =          strFileData.Insert(0,ColumnName.ToString().PadLeft(width, '0'));
                    
                }

                
                //insert in the text file
Posted
Comments
PIEBALDconsult 16-Sep-15 23:15pm    
I couldn't follow all that, but you will likely want to access the excelTable.Columns collection to determine how many columns there are and what their names are.
Sinisa Hajnal 17-Sep-15 3:26am    
There is a way to use access as datasource for excel. You don't need the app for that. Assuming your queries are varied, you could use it like you describe, but then you don't get the data "in text format" , you get DataTable object which you can get into the excel easily. Read about Office.Interop namespace, there are plenty of examples on getting data table data to excel.

1 solution

Thank you for your answers, i already solved the problem, I just needed to create a foreach inside a foreach and specify the operations inside each foreach loop with some if operators.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900