Hashtable ht = new Hashtable();
using (TextFieldParser parser = new TextFieldParser(csv_file_path))
{
parser.TextFieldType = FieldType.Delimited;
parser.SetDelimiters(",");
while (!parser.EndOfData)
{
try
{
string[] fields = parser.ReadFields();
ht.Add(fields[0],fields[1]); //Here i need help how to insert specific columns value into a hashtable.
}
catch (Exception ex)
{
// ...
}
}
}
foreach (string key in ht.Keys)
{
Console.WriteLine(String.Format("{0} : {1}", key, ht[key]));
}
Hi,
I want to read specific columns from my .csv file and store the values into a hashtable. The csv file contains total of 9 columns of which i want to read 4 columns and there values(Columns names are like PeopleId,HashValue,SecurityToken and Date).
Can you please suggest me the code to read the file data, as i am new to this.
According to your description, I think you want to read the specific columns from .csv file. I think you could use the
StreamReader reading the .csv file, and use the
String.Split() Method to divide the result and get the specific columns. The following is a sample, you could refer to it.
protected void GetCSVValue()
{
StreamReader sr = new StreamReader(@"D:\CSVData.csv");
StringBuilder sb = new StringBuilder();
DataTable dt = CreateTable();
DataRow dr;
string s;
while (!sr.EndOfStream)
{
s = sr.ReadLine();
string[] str = s.Split(',');
dr = dt.NewRow();
//because the first line is header
string str1 = str[0].ToString();
if (!str1.Equals("Id"))
{
dr["Id"] = str[0].ToString();
dr["Name"] = str[1].ToString();
dt.Rows.Add(dr);
}
} // I test it using a GridView
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected DataTable CreateTable()
{
// Create a new DataTable.
DataTable table = new DataTable("TestTable");
// Declare variables for DataColumn and DataRow objects.
DataColumn column;
DataRow row;
// Create new DataColumn, set DataType,
// ColumnName and add to DataTable.
column = new DataColumn();
column.DataType = System.Type.GetType("System.Int32");
column.ColumnName = "Id";
column.ReadOnly = true;
column.Unique = true;
// Add the Column to the DataColumnCollection.
table.Columns.Add(column);
// Create second column.
column = new DataColumn();
column.DataType = System.Type.GetType("System.String");
column.ColumnName = "Name";
column.AutoIncrement = false;
column.Caption = "Name";
column.ReadOnly = false;
column.Unique = false;
// Add the column to the table.
table.Columns.Add(column);
// Make the ID column the primary key column.
DataColumn[] PrimaryKeyColumns = new DataColumn[1];
PrimaryKeyColumns[0] = table.Columns["Id"];
table.PrimaryKey = PrimaryKeyColumns;
return table;
}
Hope it can help you.
Best Regards,
Dillion
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
and use the
String.Split() Method to divide the result and get the specific columns
You can not reliably read a CSV file by simply splitting on the separator (e.g. ','). The fields themselves can contain the separator in which case the split will return an incorrect result.
Only Robinson Crusoe had everything done by Friday.
Member
9 Points
29 Posts
Read specific columns and there values from .CSV file and store it in a hashtable using C#.net
Aug 01, 2014 12:07 AM|anand.sonawane|LINK
Hi,
I want to read specific columns from my .csv file and store the values into a hashtable. The csv file contains total of 9 columns of which i want to read 4 columns and there values(Columns names are like PeopleId,HashValue,SecurityToken and Date).
Can you please suggest me the code to read the file data, as i am new to this.
Thanks,
Anand.
Anand
Member
624 Points
274 Posts
Re: Read specific columns and there values from .CSV file and store it in a hashtable using C#.ne...
Aug 01, 2014 01:37 AM|akfkmupiwu|LINK
I would suggest to use filehelpers multirecordengine
You can define your record selector to read the required field.
if (helpful){
switch (ShouldIMarkAsAnwer) {
// Process
...
}
}
Contributor
3054 Points
1018 Posts
Re: Read specific columns and there values from .CSV file and store it in a hashtable using C#.ne...
Aug 02, 2014 08:39 AM|wim sturkenboom|LINK
A hashtable contains key / value pairs. So unless you combine columns, you can't. Also note that the key must be unique.
An alternative is to make the value a new hashtable
I'm not sure why you want to use a hashtable for this, a datatable might be a better option.
PS: none of the code is tested!
All-Star
45489 Points
7008 Posts
Microsoft
Re: Read specific columns and there values from .CSV file and store it in a hashtable using C#.ne...
Aug 05, 2014 05:59 AM|Zhi Lv - MSFT|LINK
Hi anand,
According to your description, I think you want to read the specific columns from .csv file. I think you could use the StreamReader reading the .csv file, and use the String.Split() Method to divide the result and get the specific columns. The following is a sample, you could refer to it.
Hope it can help you.
Best Regards,
Dillion
Contributor
3054 Points
1018 Posts
Re: Read specific columns and there values from .CSV file and store it in a hashtable using C#.ne...
Aug 05, 2014 07:43 AM|wim sturkenboom|LINK
You can not reliably read a CSV file by simply splitting on the separator (e.g. ','). The fields themselves can contain the separator in which case the split will return an incorrect result.