Solved

multiple input file store

Posted on 2007-11-20
13
292 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

 

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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
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 synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

734 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