如何读取.dat文件,从每一行拆分记录并将其保存到数据库的各个列中
问题描述:
A.O.A
我必须上传包含以下代码的文件,然后阅读每条记录
逐行(并以下面提到的格式分割每一行),
然后将其保存到数据库..
A.O.A
i have to upload a file containing following codes and then read each record
line by line(and split each line in the format i mentioned below),
then save it to database..
31201011281853000100000000710003
分割的格式是
The format of splitting is
31
(2010-11-28,18 53) yyyy-mm-dd,hr-min
0001
0000000071
0003
请答复我该怎么做.
please reply how i can do this
答
使用
String.substring
拆分字符串,然后使用插入命令将其保存到数据库中.
to split the string and save it into your database using Insert command.
string sql = "",code = "",Inout = "",EmployeeCode = "",TerminalNo = "";
int year = 0,month = 0,day = 0,hour = 0,minute = 0;
string text = string.Empty;
UploadId = Convert.ToInt32(munshi.getcolnum("select MAX(isnull(uploadid,0)) + 1 from attendance"));
if (UploadId == -1)
{
UploadId = UploadId + 1;
}
do
{
text = reader.ReadLine();
if (text == null)
break;
else if (text == string.Empty)
{
text = "1";
continue;
}
#region S p l i t t i n g D a t e
year = Convert.ToInt32(text.Substring(2, 4));
month = Convert.ToInt32(text.Substring(6, 2));
day = Convert.ToInt32(text.Substring(8, 2));
hour = Convert.ToInt32(text.Substring(10, 2));
minute = Convert.ToInt32(text.Substring(12, 2));
#endregion
code = text.Substring(0, 2);
Inout = text.Substring(14, 4);
EmployeeCode = text.Substring(18, 10);
TerminalNo = text.Substring(28, 4);
DateTime date = new DateTime(year,month,day,hour, minute, 0);
}
while (!string.IsNullOrEmpty(text));