Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
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
Posted
Updated 12-Jan-12 23:00pm
v4
Comments
Om Prakash Pant 13-Jan-12 3:01am    
will the length of data that you want to extract remain same?
zainknoman 13-Jan-12 5:04am    
the length of data in each line remains the same
Sergey Alexandrovich Kryukov 13-Jan-12 3:14am    
Why? why?!
--SA
StM0n 13-Jan-12 3:53am    
You deserve it ;)

use the
String.substring
to split the string and save it into your database using Insert command.
 
Share this answer
 
Comments
zainknoman 13-Jan-12 5:10am    
i have to split this whole code (31201011281853000100000000710003) into 5 parts, is it possible to do that by using substring?
Rajesh Anuhya 13-Jan-12 5:11am    
Yes , why not?
zainknoman 13-Jan-12 5:14am    
ok thanks, can u tell me some c# example code for uploading, and then reading a file
Rajesh Anuhya 13-Jan-12 5:15am    
see this link, you will get an idea
http://www.codeproject.com/KB/aspnet/fileupload.aspx
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));
 
Share this answer
 
v2

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