473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

importing csv to datagrid, adding column data

JMO
I can import a csv file with no problem. I can also add columns to the
datagrid upon import. I want to be able to start importing at the 3rd
row. This will pick up the headers necessary for the datagrid. Once I
can get to that point I need some way to be able to add new data only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use DSN
less connection. The DBQ attribute sets the path of directory which
contains CSV files

string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}

Jan 18 '07 #1
12 6172
JMO,

Isn't easier for you to import everything and then remove the 1st three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
>I can import a csv file with no problem. I can also add columns to the
datagrid upon import. I want to be able to start importing at the 3rd
row. This will pick up the headers necessary for the datagrid. Once I
can get to that point I need some way to be able to add new data only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use DSN
less connection. The DBQ attribute sets the path of directory which
contains CSV files

string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}

Jan 18 '07 #2
JMO
Robson,

How would I do that?

JMO

Robson Siqueira wrote:
JMO,

Isn't easier for you to import everything and then remove the 1st three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
I can import a csv file with no problem. I can also add columns to the
datagrid upon import. I want to be able to start importing at the 3rd
row. This will pick up the headers necessary for the datagrid. Once I
can get to that point I need some way to be able to add new data only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use DSN
less connection. The DBQ attribute sets the path of directory which
contains CSV files

string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}
Jan 18 '07 #3
JMO
Robson,

How would I do that?

JMO

Robson Siqueira wrote:
JMO,

Isn't easier for you to import everything and then remove the 1st three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
I can import a csv file with no problem. I can also add columns to the
datagrid upon import. I want to be able to start importing at the 3rd
row. This will pick up the headers necessary for the datagrid. Once I
can get to that point I need some way to be able to add new data only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use DSN
less connection. The DBQ attribute sets the path of directory which
contains CSV files

string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}
Jan 18 '07 #4
You could use this after fill the dataset:
ds.Tables["Stocks"].Rows[0].Delete();

ds.Tables["Stocks"].Rows[1].Delete();

ds.Tables["Stocks"].Rows[2].Delete();
--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
Robson,

How would I do that?

JMO

Robson Siqueira wrote:
>JMO,

Isn't easier for you to import everything and then remove the 1st three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegr oups.com...
>I can import a csv file with no problem. I can also add columns to the
datagrid upon import. I want to be able to start importing at the 3rd
row. This will pick up the headers necessary for the datagrid. Once I
can get to that point I need some way to be able to add new data only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use DSN
less connection. The DBQ attribute sets the path of directory which
contains CSV files

string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}

Jan 18 '07 #5
JMO
Its removes the rows but its still using the first row as the header,
how can i get it to use the 3rd row as the header?

JMO
Robson Siqueira wrote:
You could use this after fill the dataset:
ds.Tables["Stocks"].Rows[0].Delete();

ds.Tables["Stocks"].Rows[1].Delete();

ds.Tables["Stocks"].Rows[2].Delete();
--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
Robson,

How would I do that?

JMO

Robson Siqueira wrote:
JMO,

Isn't easier for you to import everything and then remove the 1st three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
I can import a csv file with no problem. I can also add columns to the
datagrid upon import. I want to be able to start importing at the 3rd
row. This will pick up the headers necessary for the datagrid. Once I
can get to that point I need some way to be able to add new data only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use DSN
less connection. The DBQ attribute sets the path of directory which
contains CSV files

string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}
Jan 18 '07 #6
Ahhhhh, I think I understood it now.

Probably your file has blank lines on the beginning or something else you
dont' want to use. The code you're using came from
http://www.codeproject.com/cs/databa...lCSVReader.asp. I've read the
article. There is a way to customize certain aspects when you deal with
these files thru ODBC using the schema.ini file
(http://msdn.microsoft.com/library/de..._ini_file.asp).
Therefore, there is no place where you specify where the header starts. Are
you files too big? If they're not, you could do this:

StreamReader sr = new StreamReader("c:\\file.txt");

StreamWriter sw = new StreamWriter("c:\\mynewfile.txt");

for (int i = 1; i < 3; i++)

sr.ReadLine();

while (!sr.EndOfStream)

{

sw.WriteLine(sr.ReadLine());

}

sw.Flush();

sr.Close();

sw.Close();

And then use the new file created. If your file is bigger, you should try
something else.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Its removes the rows but its still using the first row as the header,
how can i get it to use the 3rd row as the header?

JMO
Robson Siqueira wrote:
>You could use this after fill the dataset:
ds.Tables["Stocks"].Rows[0].Delete();

ds.Tables["Stocks"].Rows[1].Delete();

ds.Tables["Stocks"].Rows[2].Delete();
--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegr oups.com...
Robson,

How would I do that?

JMO

Robson Siqueira wrote:
JMO,

Isn't easier for you to import everything and then remove the 1st
three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegr oups.com...
I can import a csv file with no problem. I can also add columns to
the
datagrid upon import. I want to be able to start importing at the
3rd
row. This will pick up the headers necessary for the datagrid.
Once I
can get to that point I need some way to be able to add new data
only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use DSN
less connection. The DBQ attribute sets the path of directory which
contains CSV files

string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}


Jan 18 '07 #7
JMO
Would I put this at the beginning of the "Function For Importing Data
>From CSV File"?
Robson Siqueira wrote:
Ahhhhh, I think I understood it now.

Probably your file has blank lines on the beginning or something else you
dont' want to use. The code you're using came from
http://www.codeproject.com/cs/databa...lCSVReader.asp. I've read the
article. There is a way to customize certain aspects when you deal with
these files thru ODBC using the schema.ini file
(http://msdn.microsoft.com/library/de..._ini_file.asp).
Therefore, there is no place where you specify where the header starts. Are
you files too big? If they're not, you could do this:

StreamReader sr = new StreamReader("c:\\file.txt");

StreamWriter sw = new StreamWriter("c:\\mynewfile.txt");

for (int i = 1; i < 3; i++)

sr.ReadLine();

while (!sr.EndOfStream)

{

sw.WriteLine(sr.ReadLine());

}

sw.Flush();

sr.Close();

sw.Close();

And then use the new file created. If your file is bigger, you should try
something else.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Its removes the rows but its still using the first row as the header,
how can i get it to use the 3rd row as the header?

JMO
Robson Siqueira wrote:
You could use this after fill the dataset:
ds.Tables["Stocks"].Rows[0].Delete();

ds.Tables["Stocks"].Rows[1].Delete();

ds.Tables["Stocks"].Rows[2].Delete();
--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
Robson,

How would I do that?

JMO

Robson Siqueira wrote:
JMO,

Isn't easier for you to import everything and then remove the 1st
three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
I can import a csv file with no problem. I can also add columns to
the
datagrid upon import. I want to be able to start importing at the
3rd
row. This will pick up the headers necessary for the datagrid.
Once I
can get to that point I need some way to be able to add new data
only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use DSN
less connection. The DBQ attribute sets the path of directory which
contains CSV files

string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}
Jan 18 '07 #8
JMO
What about adding data to specific columns from say another sql
statement based on the 2nd column of data?
JMO wrote:
Would I put this at the beginning of the "Function For Importing Data
From CSV File"?

Robson Siqueira wrote:
Ahhhhh, I think I understood it now.

Probably your file has blank lines on the beginning or something else you
dont' want to use. The code you're using came from
http://www.codeproject.com/cs/databa...lCSVReader.asp. I've read the
article. There is a way to customize certain aspects when you deal with
these files thru ODBC using the schema.ini file
(http://msdn.microsoft.com/library/de..._ini_file.asp).
Therefore, there is no place where you specify where the header starts. Are
you files too big? If they're not, you could do this:

StreamReader sr = new StreamReader("c:\\file.txt");

StreamWriter sw = new StreamWriter("c:\\mynewfile.txt");

for (int i = 1; i < 3; i++)

sr.ReadLine();

while (!sr.EndOfStream)

{

sw.WriteLine(sr.ReadLine());

}

sw.Flush();

sr.Close();

sw.Close();

And then use the new file created. If your file is bigger, you should try
something else.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Its removes the rows but its still using the first row as the header,
how can i get it to use the 3rd row as the header?
>
JMO
>
>
Robson Siqueira wrote:
>You could use this after fill the dataset:
>ds.Tables["Stocks"].Rows[0].Delete();
>>
>ds.Tables["Stocks"].Rows[1].Delete();
>>
>ds.Tables["Stocks"].Rows[2].Delete();
>>
>>
>--
>Regards,
>Robson Siqueira
>Enterprise Architect
>"JMO" <j.**********@gmail.comwrote in message
>news:11*********************@v45g2000cwv.googlegr oups.com...
Robson,
>
How would I do that?
>
JMO
>
Robson Siqueira wrote:
>JMO,
>>
>Isn't easier for you to import everything and then remove the 1st
>three
>rows?
>>
>--
>Regards,
>Robson Siqueira
>Enterprise Architect
>"JMO" <j.**********@gmail.comwrote in message
>news:11**********************@38g2000cwa.googlegr oups.com...
>I can import a csv file with no problem. I can also add columns to
>the
datagrid upon import. I want to be able to start importing at the
3rd
row. This will pick up the headers necessary for the datagrid.
Once I
can get to that point I need some way to be able to add new data
only
to the new columns that were added.
>
Here is some of my code:
>
//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string
>
// Create a connection string as below, if you want to use DSN
less connection. The DBQ attribute sets the path of directory which
contains CSV files
>
string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;
>
//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());
>
// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");
>
//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";
>
obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");
>
ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");
>
//Set the datagrid properties
>
dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";
>
//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}
>
>
>
Jan 18 '07 #9
Yes, before you open the ODBC connection.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@38g2000cwa.googlegrou ps.com...
Would I put this at the beginning of the "Function For Importing Data
>>From CSV File"?

Robson Siqueira wrote:
>Ahhhhh, I think I understood it now.

Probably your file has blank lines on the beginning or something else you
dont' want to use. The code you're using came from
http://www.codeproject.com/cs/databa...lCSVReader.asp. I've read the
article. There is a way to customize certain aspects when you deal with
these files thru ODBC using the schema.ini file
(http://msdn.microsoft.com/library/de..._ini_file.asp).
Therefore, there is no place where you specify where the header starts.
Are
you files too big? If they're not, you could do this:

StreamReader sr = new StreamReader("c:\\file.txt");

StreamWriter sw = new StreamWriter("c:\\mynewfile.txt");

for (int i = 1; i < 3; i++)

sr.ReadLine();

while (!sr.EndOfStream)

{

sw.WriteLine(sr.ReadLine());

}

sw.Flush();

sr.Close();

sw.Close();

And then use the new file created. If your file is bigger, you should try
something else.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@l53g2000cwa.googleg roups.com...
Its removes the rows but its still using the first row as the header,
how can i get it to use the 3rd row as the header?

JMO
Robson Siqueira wrote:
You could use this after fill the dataset:
ds.Tables["Stocks"].Rows[0].Delete();

ds.Tables["Stocks"].Rows[1].Delete();

ds.Tables["Stocks"].Rows[2].Delete();
--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegr oups.com...
Robson,

How would I do that?

JMO

Robson Siqueira wrote:
JMO,

Isn't easier for you to import everything and then remove the 1st
three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegr oups.com...
I can import a csv file with no problem. I can also add columns
to
the
datagrid upon import. I want to be able to start importing at
the
3rd
row. This will pick up the headers necessary for the datagrid.
Once I
can get to that point I need some way to be able to add new data
only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use
DSN
less connection. The DBQ attribute sets the path of directory
which
contains CSV files

string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}

Jan 18 '07 #10
JMO
Do you knowing about adding Data to specific columns?
Robson Siqueira wrote:
Yes, before you open the ODBC connection.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@38g2000cwa.googlegrou ps.com...
Would I put this at the beginning of the "Function For Importing Data
>From CSV File"?
Robson Siqueira wrote:
Ahhhhh, I think I understood it now.

Probably your file has blank lines on the beginning or something else you
dont' want to use. The code you're using came from
http://www.codeproject.com/cs/databa...lCSVReader.asp. I've read the
article. There is a way to customize certain aspects when you deal with
these files thru ODBC using the schema.ini file
(http://msdn.microsoft.com/library/de..._ini_file.asp).
Therefore, there is no place where you specify where the header starts.
Are
you files too big? If they're not, you could do this:

StreamReader sr = new StreamReader("c:\\file.txt");

StreamWriter sw = new StreamWriter("c:\\mynewfile.txt");

for (int i = 1; i < 3; i++)

sr.ReadLine();

while (!sr.EndOfStream)

{

sw.WriteLine(sr.ReadLine());

}

sw.Flush();

sr.Close();

sw.Close();

And then use the new file created. If your file is bigger, you should try
something else.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Its removes the rows but its still using the first row as the header,
how can i get it to use the 3rd row as the header?

JMO
Robson Siqueira wrote:
You could use this after fill the dataset:
ds.Tables["Stocks"].Rows[0].Delete();

ds.Tables["Stocks"].Rows[1].Delete();

ds.Tables["Stocks"].Rows[2].Delete();
--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
Robson,

How would I do that?

JMO

Robson Siqueira wrote:
JMO,

Isn't easier for you to import everything and then remove the 1st
three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
I can import a csv file with no problem. I can also add columns
to
the
datagrid upon import. I want to be able to start importing at
the
3rd
row. This will pick up the headers necessary for the datagrid.
Once I
can get to that point I need some way to be able to add new data
only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use
DSN
less connection. The DBQ attribute sets the path of directory
which
contains CSV files

string strConnString="Driver={Microsoft Text Driver (*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}

Jan 18 '07 #11
I know. It depends on what you want to do.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
Do you knowing about adding Data to specific columns?
Robson Siqueira wrote:
>Yes, before you open the ODBC connection.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@38g2000cwa.googlegro ups.com...
Would I put this at the beginning of the "Function For Importing Data
From CSV File"?

Robson Siqueira wrote:
Ahhhhh, I think I understood it now.

Probably your file has blank lines on the beginning or something else
you
dont' want to use. The code you're using came from
http://www.codeproject.com/cs/databa...lCSVReader.asp. I've read
the
article. There is a way to customize certain aspects when you deal
with
these files thru ODBC using the schema.ini file
(http://msdn.microsoft.com/library/de..._ini_file.asp).
Therefore, there is no place where you specify where the header
starts.
Are
you files too big? If they're not, you could do this:

StreamReader sr = new StreamReader("c:\\file.txt");

StreamWriter sw = new StreamWriter("c:\\mynewfile.txt");

for (int i = 1; i < 3; i++)

sr.ReadLine();

while (!sr.EndOfStream)

{

sw.WriteLine(sr.ReadLine());

}

sw.Flush();

sr.Close();

sw.Close();

And then use the new file created. If your file is bigger, you should
try
something else.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@l53g2000cwa.googleg roups.com...
Its removes the rows but its still using the first row as the
header,
how can i get it to use the 3rd row as the header?

JMO
Robson Siqueira wrote:
You could use this after fill the dataset:
ds.Tables["Stocks"].Rows[0].Delete();

ds.Tables["Stocks"].Rows[1].Delete();

ds.Tables["Stocks"].Rows[2].Delete();
--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegr oups.com...
Robson,

How would I do that?

JMO

Robson Siqueira wrote:
JMO,

Isn't easier for you to import everything and then remove the
1st
three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegr oups.com...
I can import a csv file with no problem. I can also add
columns
to
the
datagrid upon import. I want to be able to start importing at
the
3rd
row. This will pick up the headers necessary for the
datagrid.
Once I
can get to that point I need some way to be able to add new
data
only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use
DSN
less connection. The DBQ attribute sets the path of directory
which
contains CSV files

string strConnString="Driver={Microsoft Text Driver
(*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new
System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}


Jan 20 '07 #12
JMO
I have a CSV file with 15 columns in it. I know the sample project was
setup for just 2 columns. I am only getting 3 columns of data showing.
If I edit the 14 in this line...for (int j = 1; j <=
da.Tables["Stocks"].Columns.Count - 14; j++) to 13 I gain an additional
column of data but the rows duplicate once. As I decrease the 14 one by
one, each number I decrease by is how many duplicates of the rows I
get. Can someone help me out on this?

// Now we will collect data from data table and insert it into database
one by one
// Initially there will be no data in database so we will insert data
in first two columns
// and after that we will update data in same row for remaining columns
// The logic is simple. 'i' represents rows while 'j' represents
columns

cmd.Connection = con1;
cmd.CommandType = CommandType.Text;
cmd1.Connection = con1;
cmd1.CommandType = CommandType.Text;

con1.Open();
for (int i = 0; i <= da.Tables["Stocks"].Rows.Count - 1; i++)
{

for (int j = 1; j <= da.Tables["Stocks"].Columns.Count - 14; j++)
{

cmd.CommandText = "Insert into Test (srno, " +
da.Tables["Stocks"].Columns[0].ColumnName.Trim() + ") values(" + (i +
1) + ",'" + da.Tables["Stocks"].Rows[i].ItemArray.GetValue(0) + "')";

// For UPDATE statement, in where clause you need some unique row
//identifier. We are using 'srno' in WHERE clause.
cmd1.CommandText = "Update Test set " +
da.Tables["Stocks"].Columns[j].ColumnName.Trim() + " = '" +
da.Tables["Stocks"].Rows[i].ItemArray.GetValue(j) + "' where srno =" +
(i + 1);
cmd.ExecuteNonQuery();
cmd1.ExecuteNonQuery();

}
}

Thanks,
JMO

Robson Siqueira wrote:
I know. It depends on what you want to do.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@m58g2000cwm.googlegr oups.com...
Do you knowing about adding Data to specific columns?
Robson Siqueira wrote:
Yes, before you open the ODBC connection.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@38g2000cwa.googlegrou ps.com...
Would I put this at the beginning of the "Function For Importing Data
From CSV File"?

Robson Siqueira wrote:
Ahhhhh, I think I understood it now.

Probably your file has blank lines on the beginning or something else
you
dont' want to use. The code you're using came from
http://www.codeproject.com/cs/databa...lCSVReader.asp. I've read
the
article. There is a way to customize certain aspects when you deal
with
these files thru ODBC using the schema.ini file
(http://msdn.microsoft.com/library/de..._ini_file.asp).
Therefore, there is no place where you specify where the header
starts.
Are
you files too big? If they're not, you could do this:

StreamReader sr = new StreamReader("c:\\file.txt");

StreamWriter sw = new StreamWriter("c:\\mynewfile.txt");

for (int i = 1; i < 3; i++)

sr.ReadLine();

while (!sr.EndOfStream)

{

sw.WriteLine(sr.ReadLine());

}

sw.Flush();

sr.Close();

sw.Close();

And then use the new file created. If your file is bigger, you should
try
something else.

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@l53g2000cwa.googlegr oups.com...
Its removes the rows but its still using the first row as the
header,
how can i get it to use the 3rd row as the header?

JMO
Robson Siqueira wrote:
You could use this after fill the dataset:
ds.Tables["Stocks"].Rows[0].Delete();

ds.Tables["Stocks"].Rows[1].Delete();

ds.Tables["Stocks"].Rows[2].Delete();
--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11*********************@v45g2000cwv.googlegro ups.com...
Robson,

How would I do that?

JMO

Robson Siqueira wrote:
JMO,

Isn't easier for you to import everything and then remove the
1st
three
rows?

--
Regards,
Robson Siqueira
Enterprise Architect
"JMO" <j.**********@gmail.comwrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
I can import a csv file with no problem. I can also add
columns
to
the
datagrid upon import. I want to be able to start importing at
the
3rd
row. This will pick up the headers necessary for the
datagrid.
Once I
can get to that point I need some way to be able to add new
data
only
to the new columns that were added.

Here is some of my code:

//Function For Importing Data From CSV File
public DataSet ConnectCSV(string filetable)
{
DataSet ds = new DataSet();
try
{
// You can get connected to driver either by using DSN or
connection string

// Create a connection string as below, if you want to use
DSN
less connection. The DBQ attribute sets the path of directory
which
contains CSV files

string strConnString="Driver={Microsoft Text Driver
(*.txt;
*.csv)};Dbq="+txtCSVFolderPath.Text.Trim()+";Exten sions=asc,csv,tab,txt;Persist
Security Info=False";
string sql_select;
System.Data.Odbc.OdbcConnection conn;

//Create connection to CSV file
conn = new
System.Data.Odbc.OdbcConnection(strConnString.Trim ());

// For creating a connection using DSN, use following line
//conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

//Open the connection
conn.Open();
//Fetch records from CSV
sql_select = "select * from ["+ filetable +"]";

obj_oledb_da = new
System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
//Fill dataset with the records from CSV file
obj_oledb_da.Fill(ds,"Stocks");
ds.Tables["Stocks"].Columns.Add("Name");

ds.Tables["Stocks"].Columns.Add("Description");
ds.Tables["Stocks"].Columns.Add("CUSIP");
ds.Tables["Stocks"].Columns.Add("SEDOL1");
ds.Tables["Stocks"].Columns.Add("ISIN");

//Set the datagrid properties

dGridCSVdata.DataSource=ds;
dGridCSVdata.DataMember="Stocks";

//Close Connection to CSV file
conn.Close();
}
catch(Exception e) //Error
{
MessageBox.Show(e.Message);
}
return ds;
}


Jan 22 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Simon | last post by:
Can any one please tell me how to set the width of my datagrid coloums after I have put them into edit mode? At the moment I bind the datagrid to a returning dataset; DataGrid1.DataSource =...
8
by: Ashish Shridharan | last post by:
Hi All I have been trying to add a control to the header cell of a datagrid on my ASP.NET page. These controls are defined in the HTML as ASP.NET web controls. They are being added into the...
2
by: Clayton Hamilton | last post by:
I have a DataGrid on a webform bound to a Datasource and can successfully use <ItemTemplate> to create edit/update/cancel functionality for user maintenance of data. I use separate logic to delete...
3
by: Jim Heavey | last post by:
Trying to figure out the technique which should be used to add rows to a datagrid. I am thinking that I would want an "Add" button on the footer, but I am not quite sure how to do that. Is that...
1
by: Andrew | last post by:
Hey all, I am very new to ASP.Net (and .Net in general), but that isn't stopping the boss from wanting to begin new projects in it. This latest project has me kinda stumped and after a couple...
0
by: optimizeit | last post by:
What I am attempting to do is import an Excel Workbook and display the worksheets in a datagrid dynamically. I am very close to getting this to work. I have to this point successfully imported a...
1
by: sianan | last post by:
I tried to use the following example, to add a checkbox column to a DataGrid in an ASP.NET application: http://www.codeproject.com/aspnet/datagridcheckbox.asp For some reason, I simply CAN'T get...
2
by: Terry Olsen | last post by:
Most of the Apps I write are 'Data Mining' apps. In VB6, I used the MSFlexGrid a lot. Now i'm trying to understand the use of the DataGrid, DataTable, & DataSet. Using examples I've gotten...
4
by: Melson | last post by:
Hi I've a problem. Can anyone help. I would like to use datagrid for data entry. How can I set the number of rows in the datagrid. And use the datagrid to update the ms sql table. regards...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.