Solved

multiple input file store

Posted on 2007-11-20
13
253 Views
Last Modified: 2013-11-07
Halo2All, i have these files in .dbf .sql .csv and i need to read from them and store each in the appropriate row and column ... they have to be stored exactly the way they are read and i have to use the data for plotting a graph. i need help on big for as i am stock for a very long time and my deadline is coming up soon and i have to have this ready very very soon ... please help out if you can .. thanks a lot

regards,

emerson
0
Comment
Question by:da_emerson
  • 7
  • 6
13 Comments
 
LVL 7

Expert Comment

by:bungHoc
ID: 20319465
Reading DBF:

OleDbConnection dbc = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path.GetDirectoryName(fileName) + ";Extended Properties=DBASE IV;");

OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM " + Path.GetFileNameWithoutExtension(fileName), dbc);

dbc.Open();
adapter.Fill(dSet);

foreach (DataTable dTab in dSet.Tables)
{
  foreach (DataRow dRow in dTab.Rows)
  {
     // Store value somewhere..
  }
}

Read CSV:
ArrayList lineList = new ArrayList();
using (StreamReader sr = new StreamReader(filename))
{
  string line;
  while ((line = sr.ReadLine()) != null)
  {
    lineList.Add(line);
  }
  sr.Close();
}

foreach (string line in m_lineList)
{
  char [] charSeparators = { ';' };
  string[] columnValues = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
  // Store value somewhere..
}
0
 

Author Comment

by:da_emerson
ID: 20321347
so i have this button for which when i click i have to put the read csv and read dbf files in there ... i want to put the data in the gridview so that i can see that its reading the data correct ... i have to store them in a table so that i can use the table rows and columns to access the data ... can u help me please with the code ?
i get the reading part from just reading the code that you have written but not the storing part .. help me to do this part please
0
 
LVL 7

Expert Comment

by:bungHoc
ID: 20326194
For DBF file, just use the DataSet with its tables. I believe that is already sufficient.

For CSV file:
- Get column names of the table (assume that column names are in first row)
- Loop through each line, read values for each column
- Add values to new row, add the row to table.

ArrayList lineList = new ArrayList();

DataTable table = new DataTable();
 

using (StreamReader sr = new StreamReader(filename))

{

  string line;

  while ((line = sr.ReadLine()) != null)

  {

    lineList.Add(line);

  }

  sr.Close();

}
 

char [] charSeparators = { ';' };

string[] columnNames = lineList[0].ToString().Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
 

for (int i = 0; i < columnNames.Length; i++)

{

  table.Columns.Add(columnNames[i]);

}
 

foreach (string line in lineList)

{  

  string[] columnValues = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
 

  // Store value somewhere..

  DataRow row = table.NewRow();

  for (int i = 0; i < columnValues.Length; i++)

  {

    row[columnName[i]] = columnValues[i];

  }

  table.Rows.Add(row);

}

Open in new window

0
 

Author Comment

by:da_emerson
ID: 20334505
thnx for the help ... i have replace my code (reading from the stream and using byte array ) with your code. and i have these errors that i canot get by:

ArrayList is a type ? , i mean directive is List or array ? i have no idea ...
cannot apply indexing with [ ] to arraylist
foreach statement could not operate due to arrayList does not contain GetEnumerator
newarray, temp and columnName does not exits in cotext

:-(
0
 
LVL 7

Expert Comment

by:bungHoc
ID: 20334523
Post your code here..
0
 

Author Comment

by:da_emerson
ID: 20334539
i have a form with a menu ... you will go to file --> open and inside the open button i have the following code ... i did not know where else to put it ...
 private void openToolStripMenuItem_Click(object sender, EventArgs e)

        {

            Stream myStream = null;

            OpenFileDialog openFileDialog1 = new OpenFileDialog();
 

            openFileDialog1.InitialDirectory = "c:\\";

            openFileDialog1.Filter = "database files (*.dbf)|*.dbf|comma-separated value files (*.csv)|*.csv|sql files (*.sql)|*.sql";

            openFileDialog1.FilterIndex = 20;

            openFileDialog1.RestoreDirectory = true;
 

            if (openFileDialog1.ShowDialog() == DialogResult.OK)

            {

                try

                {

                    if ((myStream = openFileDialog1.OpenFile()) != null)

                    {

                        using (myStream)

                        {
 

                            ArrayList lineList = new ArrayList();

                            DataTable table = new DataTable();
 

                            using (StreamReader sr = new StreamReader(filename))

                            {

                                string line;

                                while ((line = sr.ReadLine()) != null)

                                {

                                    lineList.Add(line);

                                }

                                sr.Close();

                            }
 

                            char[] charSeparators = { ';' };

                            string[] columnNames = lineList[0].ToString().Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
 

                            for (int i = 0; i < columnNames.Length; i++)

                            {

                                table.Columns.Add(columnNames[i]);

                            }
 

                            foreach (string line in lineList)

                            {

                                string[] columnValues = line.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
 

                                // Store value somewhere..

                                DataRow row = table.NewRow();

                                for (int i = 0; i < columnValues.Length; i++)

                                {

                                    row[columnName[i]] = columnValues[i];

                                }

                                table.Rows.Add(row);

                            }
 

                            while (myStream.Read(newArray, 0, newArray.Length) > 0)

                            {

                                textBox1.Text = temp.GetString(newArray); //You can show the file contents into a control like a textbox , TextBox1 must be multiline= true
 

                            }

                        }

                    }

                }

                catch (Exception ex)

                {

                    MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);

                }

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 7

Expert Comment

by:bungHoc
ID: 20334604
Okay..
>>ArrayList is a type ? , i mean directive is List or array ? i have no idea ...

Add this to your code:
using System.Collections;

>> newarray, temp and columnName does not exits in cotext
Change: row[columnName[i]] = columnValues[i];
To: row[columnNames[i]] = columnValues[i]; // missing an 's'

Declare your newArray and temp..
0
 

Author Comment

by:da_emerson
ID: 20335223
i have changed the code ... i have added collection to directives ... i have added "s" to the columnNames array ... but i still get the filename and newArray and temp error for not declaring them ...
i have to delcare them and i know how to declare but i have no idea what type they are and where i should declare them ... so i have added
ArrayList lineList = new ArrayList();
DataTable table = new DataTable();
to the begining of the code ... it worked out .. but i do not know why ? ... but i could not get the other ones to work ... since i can not get it to declare ... this is very stupid ... or i am just thinking to hard to see something so simple ... can u tell what i have to do here please .. thnx ;-)
0
 
LVL 7

Expert Comment

by:bungHoc
ID: 20337770
fileName is .. the name of your file, for example: "someFile.csv"

newArray.. declare it..
0
 
LVL 7

Expert Comment

by:bungHoc
ID: 20337871
Just out of my curiosity, are you totally new to ... coding?
0
 

Author Comment

by:da_emerson
ID: 20339012
yes .. i am new to visual studio coding in c# and development in visual studio .net frame  ... i just started last month ... but i have to say that i love it .. and this is the language and the environment that i will be working in  the feature.

my filename is some filename.csv ... it will be change every time when u read  a file ... so i can not hard code the filename into code for exmaple, thisfilename.csv ... i have to have it open so that whatever the filename is it will take and read ...

thnx for the help so far ...
0
 
LVL 7

Accepted Solution

by:
bungHoc earned 500 total points
ID: 20357316
Sorry, been busy lately. File name is not important.. I've never said you have to hard code it.

You can have a "Browse" button and let user select the file and get file name..

private void m_btBrowse_Click(object sender, EventArgs e)

{

  string path = "c:\\";

  string extension = ".csv";
 

  openFileDialog.InitialDirectory = @path;

  openFileDialog.Filter = "All files (*.*)|*.*|"+ extension +" files (*"+ extension + ")|*" + extension; 

  openFileDialog.FilterIndex = 2;

  openFileDialog.RestoreDirectory = true;

  if (openFileDialog.ShowDialog() == DialogResult.OK)

  {

    fileName = openFileDialog.FileName;

  }

} 

Open in new window

0
 

Author Closing Comment

by:da_emerson
ID: 31410108
thnx it was very helpful and i love it .. lol

;-)
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now