【发布时间】:2015-06-18 09:18:49
【问题描述】:
我想将以下文件数据导入数据库中
Id Name
10001 Hemant Desai
作为常规表格数据,我如何读取这些数据,我对此感到有些困惑。
这里是csv文件数据......
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
并且在 No of medicin 字段中,我必须将所有数据写入温度字段,并在它们之间放置 quamma 并删除所有 = 符号
我尝试了以下代码
公共无效导入() { 尝试 { con.Open(); string sourceDir = @"目录路径"; var IcsvFile = Directory.EnumerateFiles(sourceDir, "*.csv"); 数据表 dt = 新数据表(); //字符串第1行,第8行;
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();
//}
}
}
【问题讨论】:
-
你试过了吗?
-
查看我编辑的答案。
标签: c#