i want to import the following file data in database as
Id Name
10001 Hemant Desai
as regular table data so how can i read this data i got some confusion in this.
here is csv file data....
ID=,10001,
Name=,Hemant Desai,
Age=,60,
Sex=,male,
Doctor=,Pathak,
Mobile=,9021412202,
Alignment=,brain tumour,
No of medicins=,3,
12:02,Stamlo-5,1mg,oral,after meal,*XE0280916*
12:01,Atorfit-CV-10,4mg,oral,after meal,*XE0283337*
12:01,Losar,3mg,oral,after meal,*XE0284350*
12:02,Appointment,X ray of right chest at 11.00 am on Wed 11th Dec
12:01,procedure,Sponge patient with warm water
Temperature =,222
Blood Pressure =, 555/555
Pulse Rate =, 555
Respiratory Rate =, 999
and in No of medicin field i have to write all data till temperature field with placing quamma between them and remove all = signs
i tried with following code
public void import() { try { con.Open(); string sourceDir = @"directory path"; var IcsvFile = Directory.EnumerateFiles(sourceDir, "*.csv"); DataTable dt = new DataTable(); //string line1, line8;
foreach (string currentFile in IcsvFile)
{
//string filename = @"FullFileNameWithPath.csv";
//get all lines from csv file
string[] lines = File.ReadAllLines(currentFile);
//get only id's
var ids = lines
.Where(a => a.Trim() != string.Empty && a.Contains("ID="))
.Select((a, x) => new { ID = Convert.ToInt32(a.Split(',')[1]), index = x });
//get names
var names = lines
.Where(a => a.Trim() != string.Empty && a.Contains("Name="))
.Select((a, x) => new { Name = a.Split(',')[1], index = x });
//get patiens; join ids and names on index
var patients = from id in ids
join name in names on id.index equals name.index
select new
{
ID = id.ID,
Name = name.Name
};
foreach (var p in patients)
{
Console.WriteLine("{0}\t{1}", p.ID, p.Name);
}
//DataTable dt = new DataTable();
//DataRow row;
//using (StreamReader sr = new StreamReader(currentFile))
//{
// line1 = sr.ReadLine();
// line8 = sr.ReadLine();
//}
}
}