SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question geting information from txt file using ADO.net

    Hi all,
    can any one to told me how to fix my code because I am trying to get information for text file and insert it to database Access. right now I am trying to get the form text file using ADO.net I get the information but I want to see it on console windows
    Attached Files Attached Files

  2. #2
    SitePoint Guru Jason__C's Avatar
    Join Date
    Oct 2009
    Location
    Racoon City
    Posts
    660
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hey, I just downloaded your code to see where you are coming from. Thoughts. Have you thought about using the File System Class? That way, you can bypass the Database/ADO.NET altogether, and store everything in a text-file on the server. You would need to delimiate the file for storage, then retrieve it later, and even add to the text-file. Basically a flat-text database. Check out the File System built into .NET to help you out.

    Mainly, StreamWriter and StreamReader. Along with a few other objects.

    Good Luck.

  3. #3
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,633
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Well, if you are importing a text file into access, frankly the best tool could be access. No ADO.NET required.

    If you need to go through ADO.NET for some reason, check out the FileHelpers library -- it is great at munging text files.

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank all right now I use FileHelpers library and I follow the example in how to use it. but I counter one problem the console app can't convert Datatime for the birthday
    and this is error message
    "Error Converting ' BirthDate (mm/dd/yyyy)' to type: 'DateTime'. There are more chars than in the format string: 'dd/MM/yyyy'


    Code Csharp:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Data.OleDb;
    using System.Data;
    using FileHelpers;
     
     
     
    namespace TechnicalTest
    {
        class Program
        {
     
            [DelimitedRecord("|")]
            public class Employee
            {
                public string FirstName;
                public string LastName;
                public string Address1;
                public string City;
                public string PostalCode;
                public string PhoneNumber;
     
                public string SocialInsuranceNumber;
                [FieldConverter(ConverterKind.Date, "m/dd/yyyy")]
                public DateTime BirthDate;
     
     
     
            }
     
     
            static void Main(string[] args)
            {
     
                FileHelperEngine eng = new FileHelperEngine(typeof(Employee));
     
     
                Employee[] emp = eng.ReadFile("Employees.txt") as Employee[];
     
                eng.WriteFile("FileOut.txt", emp);
     
                foreach (Employee empl in emp)
                {
     
                    Console.WriteLine(empl.FirstName + "-" + empl.BirthDate.ToString("M/dd/yy"));
     
                }
            }
     
     
        }
    }
    "
    Attached Files Attached Files

  5. #5
    SitePoint Author silver trophybronze trophy
    wwb_99's Avatar
    Join Date
    May 2003
    Location
    Washington, DC
    Posts
    10,633
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Ran into this one before, they use DateTime.ParseExact() which is very finnicky. Best bet is to use a custom field converter that wraps DateTime.Parse(). Note that isn't very performant if you have alot of records, like a few million.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •