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. Web Development
  3. ASP.NET
  4. .txt to SQL (MS SQL Server) [modified]

.txt to SQL (MS SQL Server) [modified]

Scheduled Pinned Locked Moved ASP.NET
databasesql-servercomsysadminhelp
13 Posts 3 Posters 0 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.
  • C Christian Graus

    Maxdd 7 wrote:

    StreamReader sr = File.OpenText("E:\\fich.txt");

    Why would you open the file in a stream that you never use ? I'd suspect that is locking the file. Unless the file contains single quotes ( which you can fix with a string.Replace call ), I'd be more inclined to use File.ReadAllText and just do a normal insert of that text. Are you sure that execute scalar would return a string in this instance ?

    Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

    M Offline
    M Offline
    Maxdd 7
    wrote on last edited by
    #3

    Ups, I forgot to delete that line in this post. I really would like to use Load Data Infile, just have sintax problems, cant you help me?

    C 2 Replies Last reply
    0
    • M Maxdd 7

      Ups, I forgot to delete that line in this post. I really would like to use Load Data Infile, just have sintax problems, cant you help me?

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #4

      Did you try without that line and still got the error ?

      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

      M 1 Reply Last reply
      0
      • M Maxdd 7

        Ups, I forgot to delete that line in this post. I really would like to use Load Data Infile, just have sintax problems, cant you help me?

        C Offline
        C Offline
        Christian Graus
        wrote on last edited by
        #5

        I'm reading the SQL Server books online and INFILE is not in the help. Are you sure it's supported by SQL Server ?

        Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

        1 Reply Last reply
        0
        • C Christian Graus

          Did you try without that line and still got the error ?

          Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

          M Offline
          M Offline
          Maxdd 7
          wrote on last edited by
          #6

          Yes. Still having incorrect syntax near 'e:\fich.txt'

          C 1 Reply Last reply
          0
          • M Maxdd 7

            Yes. Still having incorrect syntax near 'e:\fich.txt'

            C Offline
            C Offline
            Christian Graus
            wrote on last edited by
            #7

            I am reading the books online, where are you getting the documentation that makes you think this will work at all ?

            Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

            M 1 Reply Last reply
            0
            • C Christian Graus

              I am reading the books online, where are you getting the documentation that makes you think this will work at all ?

              Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

              M Offline
              M Offline
              Maxdd 7
              wrote on last edited by
              #8

              There isnt documentation that makes me think this will work. I used LOAD DATA INFILE a lot of times, and it works perfectly. The problem is, I never used with ASP.NET C# coding, but MS SQL is MS SQL I just need to make the syntax right. http://dev.mysql.com/doc/refman/5.1/en/load-data.html[^]

              C 1 Reply Last reply
              0
              • M Maxdd 7

                There isnt documentation that makes me think this will work. I used LOAD DATA INFILE a lot of times, and it works perfectly. The problem is, I never used with ASP.NET C# coding, but MS SQL is MS SQL I just need to make the syntax right. http://dev.mysql.com/doc/refman/5.1/en/load-data.html[^]

                C Offline
                C Offline
                Christian Graus
                wrote on last edited by
                #9

                That's documentation for MySQL. My google search turned that up, too. MS SQL is NOT My SQL. I can find NO docs for INFILE in the docs that come with SQL Server, or on the web.

                Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                M 1 Reply Last reply
                0
                • C Christian Graus

                  That's documentation for MySQL. My google search turned that up, too. MS SQL is NOT My SQL. I can find NO docs for INFILE in the docs that come with SQL Server, or on the web.

                  Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                  M Offline
                  M Offline
                  Maxdd 7
                  wrote on last edited by
                  #10

                  My mistake. Probably you are right. My apologies. I was seeing that like a horse with closed eyes :rolleyes: I already starting working on natural solution but having another problem now: My file is for example: field1,field2,field3 anotherfield1,anotherfield2,anotherfield3 Just works for first line. I'm really amateur on C#, how can I do read all other lines? (having also triplicated data because the use of for instead foreach, but I will solve this problem later)

                      string MystringLine;
                      string\[\] MystringArray;
                      char\[\] charArray = new char\[\] { ',' };
                  
                      FileStream MyFileStrema = new FileStream("E:\\\\fich1.txt", FileMode.Open);
                      StreamReader MyStreamReader = new StreamReader(MyFileStrema);
                  
                      MystringLine = MyStreamReader.ReadLine();
                      MystringArray = MystringLine.Split(charArray);
                  
                      int tamanho;
                      tamanho = MystringArray.Length;
                  
                      //for (int i = 0; i <= MystringArray.GetUpperBound(0); i++)
                      for (int i = 0; i < tamanho; i++)
                      {
                          //            Response.Write(MystringArray\[0\]);
                          string query;
                  
                           query = "INSERT INTO Filmes(Name, Genre, Year)";
                          query += " VALUES ('" + MystringArray\[0\] + "','" + MystringArray\[1\] + "','" + MystringArray\[2\] + "')";
                  
                          SqlCommand com = new SqlCommand(query, Conn);
                          SqlDataReader datareader = com.ExecuteReader();
                          datareader.Close();
                  
                      }
                  
                  M C 2 Replies Last reply
                  0
                  • M Maxdd 7

                    My mistake. Probably you are right. My apologies. I was seeing that like a horse with closed eyes :rolleyes: I already starting working on natural solution but having another problem now: My file is for example: field1,field2,field3 anotherfield1,anotherfield2,anotherfield3 Just works for first line. I'm really amateur on C#, how can I do read all other lines? (having also triplicated data because the use of for instead foreach, but I will solve this problem later)

                        string MystringLine;
                        string\[\] MystringArray;
                        char\[\] charArray = new char\[\] { ',' };
                    
                        FileStream MyFileStrema = new FileStream("E:\\\\fich1.txt", FileMode.Open);
                        StreamReader MyStreamReader = new StreamReader(MyFileStrema);
                    
                        MystringLine = MyStreamReader.ReadLine();
                        MystringArray = MystringLine.Split(charArray);
                    
                        int tamanho;
                        tamanho = MystringArray.Length;
                    
                        //for (int i = 0; i <= MystringArray.GetUpperBound(0); i++)
                        for (int i = 0; i < tamanho; i++)
                        {
                            //            Response.Write(MystringArray\[0\]);
                            string query;
                    
                             query = "INSERT INTO Filmes(Name, Genre, Year)";
                            query += " VALUES ('" + MystringArray\[0\] + "','" + MystringArray\[1\] + "','" + MystringArray\[2\] + "')";
                    
                            SqlCommand com = new SqlCommand(query, Conn);
                            SqlDataReader datareader = com.ExecuteReader();
                            datareader.Close();
                    
                        }
                    
                    M Offline
                    M Offline
                    MidwestLimey
                    wrote on last edited by
                    #11

                    If the file is well structured, perhaps you could try issuing a BULK INSERT statement to MS SQL server? I just hit a problem where I needed to push > 200k records in multiple tables, worked in under a couple of minutes.

                    10110011001111101010101000001000001101001010001010100000100000101000001000111100010110001011001011

                    M 1 Reply Last reply
                    0
                    • M Maxdd 7

                      My mistake. Probably you are right. My apologies. I was seeing that like a horse with closed eyes :rolleyes: I already starting working on natural solution but having another problem now: My file is for example: field1,field2,field3 anotherfield1,anotherfield2,anotherfield3 Just works for first line. I'm really amateur on C#, how can I do read all other lines? (having also triplicated data because the use of for instead foreach, but I will solve this problem later)

                          string MystringLine;
                          string\[\] MystringArray;
                          char\[\] charArray = new char\[\] { ',' };
                      
                          FileStream MyFileStrema = new FileStream("E:\\\\fich1.txt", FileMode.Open);
                          StreamReader MyStreamReader = new StreamReader(MyFileStrema);
                      
                          MystringLine = MyStreamReader.ReadLine();
                          MystringArray = MystringLine.Split(charArray);
                      
                          int tamanho;
                          tamanho = MystringArray.Length;
                      
                          //for (int i = 0; i <= MystringArray.GetUpperBound(0); i++)
                          for (int i = 0; i < tamanho; i++)
                          {
                              //            Response.Write(MystringArray\[0\]);
                              string query;
                      
                               query = "INSERT INTO Filmes(Name, Genre, Year)";
                              query += " VALUES ('" + MystringArray\[0\] + "','" + MystringArray\[1\] + "','" + MystringArray\[2\] + "')";
                      
                              SqlCommand com = new SqlCommand(query, Conn);
                              SqlDataReader datareader = com.ExecuteReader();
                              datareader.Close();
                      
                          }
                      
                      C Offline
                      C Offline
                      Christian Graus
                      wrote on last edited by
                      #12

                      This is too much code.

                      string[] datum = File.ReadAllLines("E:\\fich1.txt");

                      foreach(string data in datum)
                      {
                      string [] MystringArray = data.Split(new char[] {','} );

                      // rest of your SQL code will work from here, although I'd use a paramaterised query, or at a minumum do MyStringArray[0].Replace("'", "''") to double quote the strings so the SQL does not blow up

                      }

                      Christian Graus Driven to the arms of OSX by Vista. Read my blog to find out how I've worked around bugs in Microsoft tools and frameworks.

                      1 Reply Last reply
                      0
                      • M MidwestLimey

                        If the file is well structured, perhaps you could try issuing a BULK INSERT statement to MS SQL server? I just hit a problem where I needed to push > 200k records in multiple tables, worked in under a couple of minutes.

                        10110011001111101010101000001000001101001010001010100000100000101000001000111100010110001011001011

                        M Offline
                        M Offline
                        Maxdd 7
                        wrote on last edited by
                        #13

                        How do you ignore one specific field? Christian Graus, thank you.

                        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