This screencast shows how to sort the contents of CSV file using Microsoft Text Driver and ADODB. (Note:- you can watch the same screencast here http://video.google.com/googleplayer.swf?docid=2082004182954594811&hl=en&fs=true in a better resolution)
Code Snippet
-------------
private void button1_Click(object sender, EventArgs e)
{
Connection connection = null;
try
{
connection = new ConnectionClass();
string connectionString =
"Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=" + Path;//eg:- Path= "c:\Data"
//Void Open(string ConnectionString,string UserID,string Password,int Options)
connection.Open(connectionString, "", "", (int)ConnectModeEnum.adModeUnknown);
string strSql = string.Format("Select * FROM {0} ORDER BY Name,{1} desc", FileName, FieldToSort); // filename="SampleData.csv" , FieldToSort = "ID"
object obj;
//Recordset Execute(string CommandText,out object RecordsAffected,int Options)
Recordset rs = connection.Execute(strSql, out obj, (int)CommandTypeEnum.adCmdText);
DisplayData(rs);
rs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
{
if(connection != null && connection.State != (int)ConnectionState.Closed)
connection.Close();
}
}
private void DisplayData(Recordset rs)
{
string strToDisplay = string.Empty;
//show field names
for(int i=0 ; i<rs.Fields.Count ; i++)
{
strToDisplay += rs.Fields[i].Name + ",";
}
strToDisplay = strToDisplay.Trim(',') + Environment.NewLine;
//recordsetobject.GetString (StringFormat, NumRows, ColumnDelimiter, RowDelimiter, NullExpr)
strToDisplay += rs.GetString(StringFormatEnum.adClipString,-1,",",Environment.NewLine,string.Empty);
textBox1.Text = strToDisplay;
}
No comments:
Post a Comment