-4

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();
                //}
            }

        }
Member 1
  • 23
  • 8

1 Answers1

0

As you probably know, this is not the proper way to use csv to store data. You will have to write a code that will go over each line of the csv and parse it. Start by reading the entire file into a string array by using System.IO.File.ReadAllLines(FilePath), and then analyze the strings. I would suggest using a DataTable (assuming your column names are fixed) note that you will have to check for each string in the array if it contains the = character, if it does, then what is before this char is the column name and whatever is after it as data. if it doesn't, use the last column and append the data of the entire row to it's current data.

Once completed, you should have a DataTable that contains all of the data from the 'csv' file. Then all you have to do is send it to a stored procedure that will add or update the data in the database.

Here is what I would probably do:

    private DataTable ReadFiles(string sourceDir)
    {
        var IcsvFile = Directory.EnumerateFiles(sourceDir, "*.csv");
        DataTable dt = new DataTable();
        dt.Columns.Add("Id", System.Type.GetType("System.Int32"));
        dt.Columns.Add("Name", System.Type.GetType("System.String"));
        dt.Columns.Add("Age", System.Type.GetType("System.Int32"));
        dt.Columns.Add("Sex", System.Type.GetType("System.String")); // Gender in proper english 
        dt.Columns.Add("Doctor", System.Type.GetType("System.String"));
        dt.Columns.Add("Mobile", System.Type.GetType("System.String"));
        dt.Columns.Add("Alignment", System.Type.GetType("System.String"));
        dt.Columns.Add("No of medicins", System.Type.GetType("System.String"));
        dt.Columns.Add("Temperature ", Type.GetType("System.Int32"));
        dt.Columns.Add("Blood Pressure", System.Type.GetType("System.String"));
        dt.Columns.Add("Pulse Rate", System.Type.GetType("System.String"));
        dt.Columns.Add("Respiratory Rate", System.Type.GetType("System.Int32"));
        foreach (string currentFile in IcsvFile)
        {
            ImportSingleFile(currentFile, ref dt);
        }
        return dt;
    }

    private void ImportSingleFile(string FilePath, ref DataTable dt)
    {
        string[] Lines = File.ReadAllLines(FilePath);
        string ColumnName, ColumnData;
        int EqualSignIndex, intColumnData;
        DataRow Row = null;
        ColumnName = string.Empty;
        foreach (string Line in Lines)
        {
            EqualSignIndex = Line.IndexOf("=");
            if (EqualSignIndex > -1)
            {
                ColumnName = Line.Substring(0, EqualSignIndex);
                // after = there is always , that we don't want in the data, 
                // and the line sometimes ends with a , that we also don't want
                ColumnData = Line.Substring(EqualSignIndex + 2).TrimEnd(','); 
            }
            else
            {
                ColumnData = Line;
            }

            if (ColumnName == "ID")
            {
                Row = dt.NewRow();
            }

            if (Row != null)
            {
                if(dt.Columns[ColumnName].DataType == Type.GetType("System.Int32")) {
                    if(int.TryParse(ColumnData, out intColumnData)) 
                    {
                        Row[ColumnName] = intColumnData;
                    } else {
                        throw new InvalidDataException(string.Format("For column {0} an integer value is expected", ColumnName));
                    }
                } else {
                    Row[ColumnName] = ColumnData;
                }                   
            }
        }
    }

After that you should have the data table full, and it should be very easy to insert/update the database from it.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • sir how to assign this dt to my datatable – Member 1 Apr 13 '15 at 09:24
  • that depends on your database. if you are working with sql server you can write a stored procedure that accept the dataTable as is from the c# and insert / update the relevant table in the database. – Zohar Peled Apr 13 '15 at 10:00
  • yes i use sql database so how this datatable accepted in stored procedure i did nt found perticular way – Member 1 Apr 13 '15 at 10:05
  • http://www.c-sharpcorner.com/uploadfile/pchandraker/passing-table-valued-parameter-to-stored-procedu-part-1/ – Zohar Peled Apr 13 '15 at 10:09
  • Its bit silly question i know bt cn u tell me where i write code for creating user defined type in sql server 2008 . because in this types section of database there is no option for creating new type or else. – Member 1 Apr 13 '15 at 10:47
  • in SSMS (that's the Sql Server Management Studio), there is a button called 'New Query' (see image in link) http://www.essentialsql.com/wp-content/uploads/2014/08/SSMS-New-Query.png – Zohar Peled Apr 13 '15 at 10:51
  • But sir i am using sql server 2008 and in that new query is like simple query to see quick output – Member 1 Apr 13 '15 at 10:58
  • You can use the query window to write whatever sql you want, including creating types, stored procedures, views and tables. – Zohar Peled Apr 13 '15 at 10:59
  • sir i have to write code for export this file in same format is there some modifications to write export code or else.Thanks for extreme help . plz give me hint to export code . – Member 1 Apr 13 '15 at 11:36
  • sir i found user defined table type it is a new item in sql server 2008 project – Member 1 Apr 13 '15 at 11:41
  • Did you complete the import process? – Zohar Peled Apr 13 '15 at 11:41
  • not now i am trying this but if i got some link for export too then i will try at home – Member 1 Apr 13 '15 at 11:53
  • when im adding that user defined table type vs stopped working . – Member 1 Apr 13 '15 at 11:53
  • sir im unable to do his error poped said about installation changes – Member 1 Apr 13 '15 at 12:11
  • I'm not sure I can help you more, It's very hard to do it without being able to see exactly what you do. I think you should maybe have someone look at your application and database. – Zohar Peled Apr 13 '15 at 12:21