In my website, I have many CSV files that I need to parse and insert their data into my MySQL database.
How can I parse the CSV in my website programmatically?
Got the Answer:
I have successfully done the parsing of my CSV file using the below code:
_nNrRowsProccessed = 0;
string connectionString = @"Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq="+ConfigurationManager.AppSettings["csvFolder"]+";";
OdbcConnection conn = new OdbcConnection(connectionString);
try
{
conn.Open();
string strFileName = ConfigurationManager.AppSettings["csvFileName"];
string strSQL = "Select * from " + strFileName;
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = conn;
cmd.CommandText = strSQL;
cmd.CommandType = CommandType.Text;
OdbcDataReader reader = cmd.ExecuteReader();
string strLine = null;
MasterCalendar_DB.OpenMySQLConnection();
while (reader.Read())
{
// insert data into mastercalendar
strLine = reader[0].ToString();
string[] arLine = strLine.Split(';');
string strAgencyPropertyID = arLine[0];
DateTime dt = DateTime.Parse(arLine[1]);
Int64 nDate = (Int64)Util.ConvertToUnixTimestamp(dt);
String strAvailability = (arLine[2]);
_nNrRowsProccessed++;
MasterCalendar_DB.Insert(strAgencyPropertyID, nDate, strAvailability);
}
}
finally
{
conn.Close();
MasterCalendar_DB.CloseMySQLConnection();
}
Also you need to add the following code under Configurations tag into your web.config file:
<appSettings>
<add key="csvFileName" value="<file_name>.csv" />
<add key="csvFolder" value="<filePath>"/>
</appSettings>
Hope this helps someone :)
I recommend looking at the TextFieldParserClass in .Net. You need to include
Imports Microsoft.VisualBasic.FileIO.TextFieldParser
Here's a quick sample:
Dim afile As FileIO.TextFieldParser = New FileIO.TextFieldParser(FileName)
Dim CurrentRecord As String() ' this array will hold each line of data
afile.TextFieldType = FileIO.FieldType.Delimited
afile.Delimiters = New String() {","}
afile.HasFieldsEnclosedInQuotes = True
' parse the actual file
Do While Not afile.EndOfData
Try
CurrentRecord = afile.ReadFields
Catch ex As FileIO.MalformedLineException
Stop
End Try
Loop
Here is a pretty good CSV parser. You'll just have to loop through the dataset to put it back into the DB.
http://www.codeproject.com/KB/database/CsvReader.aspx