reading csv   Leave a comment


using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Text;

using System.Data;

using System.IO;

using System.Data.OleDb;

using System.Globalization;

using System.Text.RegularExpressions;

using System.Collections;

 

namespace hashtable

{

class Program1

{

static void Main(string[] args)

{

clsValidateEmails objclsValidateEmails = new clsValidateEmails();

objclsValidateEmails.ValidateEmails();

}

}

 

public class clsValidateEmails

{

Hashtable htEmail = new Hashtable();

public void ValidateEmails()

{

DataTable dtCSV = GetDataTableFromCsv(@”G:\live\xls\hashtable\hashtable\foo.txt”, true);

int nCount = Load_DictionaryTable_LINQ(dtCSV);

if (nCount > 0)

{

//Loop through hash table to check working email ids

}

}

 

 

private int Load_DictionaryTable_LINQ(DataTable dt)

{

Regex regex = new Regex(@”^[A-Za-z](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$”);

var count = (from dr in dt.AsEnumerable()

.Where(dr => regex.IsMatch(dr[“EmailId”].ToString()) == true)

select new

{

a = updateHashTable(dr.Field<Int32>(“SlNo”), dr.Field<String>(“EmailId”))

}).Count();

return (int)count;

}

 

public int updateHashTable(int key, string value)

{

if (htEmail.ContainsKey(key))

{

htEmail[key] = value;

}

else

{

htEmail.Add(key, value);

}

return 1;

}

 

 

private DataTable GetDataTableFromCsv(string path, bool isFirstRowHeader)

{

string header = isFirstRowHeader ? “Yes” : “No”;

 

string pathOnly = Path.GetDirectoryName(path);

string fileName = Path.GetFileName(path);

 

string sql = @”SELECT * FROM [” + fileName + “]”;

 

using (OleDbConnection connection = new OleDbConnection(

@”Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + pathOnly +

“;Extended Properties=\”Text;HDR=” + header + “\””))

using (OleDbCommand command = new OleDbCommand(sql, connection))

using (OleDbDataAdapter adapter = new OleDbDataAdapter(command))

{

DataTable dataTable = new DataTable();

dataTable.Locale = CultureInfo.CurrentCulture;

adapter.Fill(dataTable);

return dataTable;

}

}

}

}

 

 

Advertisements

Posted April 17, 2014 by PrashanthChindam in Uncategorized

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: