Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Participant
1211 Points
216 Posts
Jun 30, 2008 08:40 AM|LINK
Try this:
using System; using System.Collections.Generic; using System.Text; using System.Data.OleDb; internal static string GetSingleValueFromFile(string emailAddress) { string returnValue = string.Empty; string sql = string.Empty; sql = "SELECT PASSWORD FROM " + "[" + "yourFileName.csv" + "] WHERE EMAIL = '" + emailAddress + "'"; string textFileConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:\temp" + ";Extended Properties='text;HDR=Yes'"; try { using (System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(textFileConnectionString)) { using (System.Data.OleDb.OleDbCommand Command = new System.Data.OleDb.OleDbCommand(sql.ToString(), Conn)) { Command.CommandType = System.Data.CommandType.Text; Conn.Open(); using (System.Data.OleDb.OleDbDataReader dataReader = Command.ExecuteReader()) { if (dataReader.HasRows == true) { while (dataReader.Read()) { if (!dataReader.IsDBNull(0)) { returnValue = dataReader.GetValue(0).ToString(); } else { returnValue = string.Empty; } } } } } } } catch (Exception ex) { throw; } return returnValue; }
kemical
Participant
1211 Points
216 Posts
Re: How to search for a value in a csv file and return another value.
Jun 30, 2008 08:40 AM|LINK
Try this:
using System; using System.Collections.Generic; using System.Text; using System.Data.OleDb; internal static string GetSingleValueFromFile(string emailAddress) { string returnValue = string.Empty; string sql = string.Empty; sql = "SELECT PASSWORD FROM " + "[" + "yourFileName.csv" + "] WHERE EMAIL = '" + emailAddress + "'"; string textFileConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + "C:\temp" + ";Extended Properties='text;HDR=Yes'"; try { using (System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection(textFileConnectionString)) { using (System.Data.OleDb.OleDbCommand Command = new System.Data.OleDb.OleDbCommand(sql.ToString(), Conn)) { Command.CommandType = System.Data.CommandType.Text; Conn.Open(); using (System.Data.OleDb.OleDbDataReader dataReader = Command.ExecuteReader()) { if (dataReader.HasRows == true) { while (dataReader.Read()) { if (!dataReader.IsDBNull(0)) { returnValue = dataReader.GetValue(0).ToString(); } else { returnValue = string.Empty; } } } } } } } catch (Exception ex) { throw; } return returnValue; }Remember to click “Mark as Answer” on the post if it helped you. Thank you!