multiple input file store

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
da_emersonAsked:
Who is Participating?
 
bungHocConnect With a Mentor Commented:
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
 
bungHocCommented:
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
 
da_emersonAuthor Commented:
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
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
bungHocCommented:
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
 
da_emersonAuthor Commented:
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
 
bungHocCommented:
Post your code here..
0
 
da_emersonAuthor Commented:
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
 
bungHocCommented:
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
 
da_emersonAuthor Commented:
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
 
bungHocCommented:
fileName is .. the name of your file, for example: "someFile.csv"

newArray.. declare it..
0
 
bungHocCommented:
Just out of my curiosity, are you totally new to ... coding?
0
 
da_emersonAuthor Commented:
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
 
da_emersonAuthor Commented:
thnx it was very helpful and i love it .. lol

;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.