Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Back To Pretending I Know How To Program

Back To Pretending I Know How To Program

Scheduled Pinned Locked Moved The Lounge
csharpdatabasecsssql-serversysadmin
23 Posts 16 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M mght

    Dan Sutton wrote:

    I love it! That happens to me with C++ -- not to mention...

    I read that as "That happens to me with C plus plus minus minus". I had a brief near heart attack wondering what horrible new extension the C++ committee was foisting on us this time... :doh:

    // ToDo: // Put Signature Here

    M Offline
    M Offline
    Member_5893260
    wrote on last edited by
    #21

    Brilliant!

    1 Reply Last reply
    0
    • R Roger Wright

      Hehehe... Two subtleties, actually. Number one is that I don't care to use Access anymore, so I'm targeting SQL Server (Express, of course) this time around. The second is not a subtlety per se, just something I neglected to describe well. The csv files contain several lines of fluff in a format that changes from time to time, such as:

      Date Range = Yesterday (09/01/2010)
      Time Zone = MST
      Sign Convention = GEN + LOAD + OTHER +
      Include Zero = Yes
      Execution Time: 09/02/2010 07:00 CST

      "Zone = RMS / AMPS"

      "Date","Meter","HE1","HE2","HE3","HE4","HE5","HE6","HE7","HE8","HE9","HE10","HE11","HE12","HE13","HE14","HE15","HE16","HE17","HE18","HE19","HE20","HE21","HE22","HE23","HE24","On Peak","Off Peak","Total"

      The data looks like: "09/01/2010","BCI891 Out","0.000","0.000","0.000","0.000","0.000","0.000","4.000","4.000","4.000","0.000","4.000","4.000","4.000","4.000","4.000","0.000","4.000","0.000","5.000","0.000","0.000","4.000","0.000","0.000","45.000","0.000","45.000" "","DAD348 Out","0.000","0.000","0.000","0.000","0.000","0.000","0.000","2.975","3.857","4.284","4.550","4.879","5.103","5.390","5.544","5.712","5.824","5.628","5.502","5.523","5.460","5.033","4.634","4.382","75.264","9.016","84.280" "","NON921A Out","-6.200","-5.800","-5.600","-5.300","-5.400","-5.500","-5.300","-5.800","-6.500","-7.300","-8.000","-9.100","-10.100","-10.900","-11.500","-11.700","-11.900","-11.700","-11.100","-10.700","-10.100","-9.000","-8.100","-7.300","-150.700","-49.200","-199.900" "","TOP928 In","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000","0.000" "","TOP928 Out","7.490","6.940","6.740","6.440","6.430","6.570","6.400","7.070","7.870","8.710","9.650","10.850","12.000","13.020","13.730","14.080","14.120","14.010","13.210","12.670","11.850","10.680","9.620","8.750","179.920","58.980","238.900" "09/01/2010","","1.290","1.140","1.140","1.140","1.030","1.070","5.100","8.245","9.227","5.694","10.200","10.629","11.003","11.510","11.774","8.092","12.044","7.938","12.612","7.493","7.210","10.713","6.154","5.832","149.484","18.796","168.280" The fluff and header stuff at the beginning, along with any blank lines, I discard. I also dump the last data line, since it's just a column total and shouldn't be preserved in the database. So I probably could have described it better, but I wasn't asking a question here, of course. My

      P Offline
      P Offline
      PeterZbinden
      wrote on last edited by
      #22

      this Kind of Thing is my daily bread and butter. is the size of the Header(fluff) always the same Count of lines? If so, use a StreamReader, Count the lines you're reading until you reach the data. As I understand it, every line of the data represents a different Meter with its own data, the strucure is always the same with the same count of elements and the same Positions. If so, read the lines of Data into an Array of string. from then on you can do with the seperate fields as you like as Long as you know the Position, for example date is alway on the first Position, in a Array this means it is on Position '0'.

      private void readLines(string inputFile, int headerLineCount)
      {
      List inputList = new List();

              Encoding coding = Encoding.GetEncoding(1252); //Ansi-Encoding, Change to 850 for ASCII, 1200 for Unicode UTF16, 65001 for UTF8 etc.
      
              StreamReader reader = new StreamReader(inputFile, coding);
      
              int lineIndex = 0;
              while (reader.EndOfStream != true)//Read until the file has no more lines to read.
              {
                  string inputLine = reader.ReadLine();//read the data here
      
                  if (lineIndex >= headerLineCount)//This Code is executed only after the set amount of lines (header) has been overread.
                  {
                      string\[\] inputElements = inputLine.Replace("\\"", "").Split(','); //We replace the '"' with an empty string and split the line into seperate Elements. You can use any char as a delemiter, for excample ';' or '!' etc. 
      
                      meterInput newInput = new meterInput();
                      newInput.Date = Convert.ToDateTime(inputElements\[0\]);// Here we use the first element of the 'inputElements' and convert it to a Datetime.
                      newInput.Name = inputElements\[1\];
                      newInput.HE1 = Convert.ToDecimal(inputElements\[2\]);
                      newInput.HE2 = Convert.ToDecimal(inputElements\[3\]);
                      newInput.HE3 = Convert.ToDecimal(inputElements\[4\]);
                      newInput.HE4 = Convert.ToDecimal(inputElements\[5\]);
                      //... Add the rest of the data.
                      newInput.OnPeak = Convert.ToDecimal(inputElements\[26\]);
                      newInput.OffPeak = Convert.ToDecimal(inputElements\[27\]);
                      newInput.Total = Convert.ToDecimal(inputElements\[28\]);
      
                      inputList.Add(newInput); // Add the complete element to
      
      R 1 Reply Last reply
      0
      • P PeterZbinden

        this Kind of Thing is my daily bread and butter. is the size of the Header(fluff) always the same Count of lines? If so, use a StreamReader, Count the lines you're reading until you reach the data. As I understand it, every line of the data represents a different Meter with its own data, the strucure is always the same with the same count of elements and the same Positions. If so, read the lines of Data into an Array of string. from then on you can do with the seperate fields as you like as Long as you know the Position, for example date is alway on the first Position, in a Array this means it is on Position '0'.

        private void readLines(string inputFile, int headerLineCount)
        {
        List inputList = new List();

                Encoding coding = Encoding.GetEncoding(1252); //Ansi-Encoding, Change to 850 for ASCII, 1200 for Unicode UTF16, 65001 for UTF8 etc.
        
                StreamReader reader = new StreamReader(inputFile, coding);
        
                int lineIndex = 0;
                while (reader.EndOfStream != true)//Read until the file has no more lines to read.
                {
                    string inputLine = reader.ReadLine();//read the data here
        
                    if (lineIndex >= headerLineCount)//This Code is executed only after the set amount of lines (header) has been overread.
                    {
                        string\[\] inputElements = inputLine.Replace("\\"", "").Split(','); //We replace the '"' with an empty string and split the line into seperate Elements. You can use any char as a delemiter, for excample ';' or '!' etc. 
        
                        meterInput newInput = new meterInput();
                        newInput.Date = Convert.ToDateTime(inputElements\[0\]);// Here we use the first element of the 'inputElements' and convert it to a Datetime.
                        newInput.Name = inputElements\[1\];
                        newInput.HE1 = Convert.ToDecimal(inputElements\[2\]);
                        newInput.HE2 = Convert.ToDecimal(inputElements\[3\]);
                        newInput.HE3 = Convert.ToDecimal(inputElements\[4\]);
                        newInput.HE4 = Convert.ToDecimal(inputElements\[5\]);
                        //... Add the rest of the data.
                        newInput.OnPeak = Convert.ToDecimal(inputElements\[26\]);
                        newInput.OffPeak = Convert.ToDecimal(inputElements\[27\]);
                        newInput.Total = Convert.ToDecimal(inputElements\[28\]);
        
                        inputList.Add(newInput); // Add the complete element to
        
        R Offline
        R Offline
        Roger Wright
        wrote on last edited by
        #23

        Thanks, Peter! This is about where I'd got to last time, but your way is much cleaner. I can learn from this, which is why I enjoy doing it. :-D The # of lines of header varies, though, and only the first and last lines of meter data includes the date, so there's a little modification to be done, but that's easy stuff.

        Will Rogers never met me.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups