Click here to Skip to main content
16,004,782 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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 toumer,
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

12:01,Measurement,put
Temperature =,222
 Blood Pressure =, 555/555
Pulse Rate =, 555
Respiratory Rate =, 999
Posted
Updated 12-Apr-15 0:18am
v2
Comments
Maciej Los 12-Apr-15 6:19am    
What have you tried till now? Where are you stuck?
Sascha Lefèvre 12-Apr-15 6:29am    
That looks like 1 record. How would it look like if there are two records?
Richard MacCutchan 12-Apr-15 6:48am    
That is not CSV, so you will need to write your own parser to process it.
Sinisa Hajnal 12-Apr-15 9:45am    
Looks like custom format, you'll need to make your own algorithm...depending on how it would look with more then one ID entry, you could maybe remove all new lines and try it that way...I would go with while loop and readline filling custom object Patient until I found next ID tag.

That's not a CSV file. A CSV has every field in a record all on one line, with a separator between fields, usually a comma, hence Comma Separated Values.

What you have a some off-the-wall custom format that you're going to have to write a parser for.
 
Share this answer
 
Linq solution might look like:

C#
string filename = @"FullFileNameWithPath.csv";
//get all lines from csv file
string[] lines = File.ReadAllLines(filename);
//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
		};


Result:
ID    Name
10001 Hemant Desai


Note, that patients returns Enumerable(ID, Name). Now you can loop through that data by using:
C#
foreach(var p in patients)
{
    Console.WriteLine("{0}\t{1}", p.ID, p.Name);
}

and save it to the database.
 
Share this answer
 
v2
Comments
Member 11543226 13-Apr-15 2:15am    
Thanks @Maciej Los , may i know the saving procedure of result data in database table datatable name is "Medicin". i tried some code but did not worked.
Maciej Los 13-Apr-15 2:19am    
I don't know what you have tried and what kind of database you use. Please, be more specific and provide more details if you want our help. We can't read in your mind or direct from your screen...
Maciej Los 14-Apr-15 2:38am    
See solution 3
If you wnat to get all data, use:

C#
string sFileName = @"fullfilename.csv";
	string[] lines = File.ReadAllLines(sFileName);
	
	int index = 0;
        //get vertical data 
	var verticaldata = lines
			.Where(a=>a.Trim()!=string.Empty && a.Contains("="))
			.Select(a=>Tuple.Create(a.Contains("ID=") ? index+=1 : index, a.Split(',')[0].Replace("=","").Trim(), a.Split(',')[1].Trim()));
	//transform to horizontal ;)
	var horizontaldata = verticaldata
			.GroupBy(r=>r.Item1)
			.Select(
				grp=>new 
				{
					//grp
					//RowNo = grp.Key,
					ID = grp.Where(r=>r.Item2.Contains("ID")).Select(a=>a.Item3).First(),
					Name = grp.Where(r=>r.Item2.Contains("Name")).Select(a=>a.Item3).First(),
					Age = grp.Where(r=>r.Item2.Contains("Age")).Select(a=>a.Item3).First(),
					Sex = grp.Where(r=>r.Item2.Contains("Sex")).Select(a=>a.Item3).First(),
					Doctor = grp.Where(r=>r.Item2.Contains("Doctor")).Select(a=>a.Item3).First(),
					Mobile = grp.Where(r=>r.Item2.Contains("Mobile")).Select(a=>a.Item3).First(),
					Alignment = grp.Where(r=>r.Item2.Contains("Alignment")).Select(a=>a.Item3).First(),
					No_of_Medicin =  grp.Where(r=>r.Item2.Contains("No of medicins")).Select(a=>a.Item3).First() ,
					Temperature = grp.Where(r=>r.Item2.Contains("Temperature")).Select(a=>a.Item3).First()
				});


Result:
ID    Name         Age Sex  Doctor Mobile     Alignment    No_of_Medicin Temperature
10001 Hemant Desai 60  male Pathak 9021412202 brain toumer 3             222 
 
Share this answer
 
v2
Comments
Member 11543226 14-Apr-15 3:32am    
thanks alot !! sir in No_of_medicin i want all data till temperature and is this code for Link to csv and how to save this data to table
Maciej Los 14-Apr-15 6:19am    
I do understand that you want "this and that", but i can't read in your mind or direct from your screen. If you want my help, you need to be more specific and provide more details. At least you need to do some effort before you post another question or comment. Feel free to ask, but provide more details about your issue.
Member 11543226 15-Apr-15 5:36am    
is this code running in simple winform , i tried alot but no result found
Maciej Los 15-Apr-15 5:56am    
Yes, it needs refernece to System.Linq;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900