views:

31

answers:

2

What I have is an extremely large text file that needs to go into a specific column at a specific raw. The file is around 100k lines. So what I want to do is read the whole file, and for each line append into that specific SQL column the line. Here's what I have but i really need help on the SQL query

string[] primaryfix = File.ReadAllLines(dinfo+"\\"+filex);
            string filename = filex.ToString();
            string[] spltifilename = filename.Split('.');
            foreach (string primary in primaryfix)
            {
                string sqltable = ("dbo.amu_Textloadingarea");
                string sql = "update " + sqltable + " set [Text] = [Text] + '" + primary + "' where begbates = '" + spltifilename[0] + "'";

                SqlConnection con = new SqlConnection("Data Source= Corvette ;Initial Catalog= GSK_Avandia_SSECASE;Integrated Security= SSPI");
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                SqlDataReader reader = cmd.ExecuteReader();
                con.Close();

            }

everything is fine except for the string sql, it doesn't update the way I would like it to.

Any help is always appreciated.

+1  A: 

The first thing I see is that you are not escaping the input from the text file; any SQL escape characters (like a single quote) will break that command. I'd recommend using parameters so you needn't worry about escapes at all.

Other than that, nothing pops to mind that would suggest why the command isn't working, but I do wonder if it might not cause fewer problems if it's such a large file to read it line-by-line rather than all at once.

Andrew Barber
it is being read in line by line
Mike
Not if the code you've posted above is the actual code being run; File.ReadAllLines reads the whole file and returns it as an in-memory array, at least according to this: http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx
Andrew Barber
foreach (string primary in primaryfix) {
Mike
That code is iterating through the primaryfix array, which is already in-memory due to the call to File.ReadAllLines().
Andrew Barber
A: 

Look likes you're trying to read from the database with that code inside the loop. SqlDataReader provides a way to read rows from the database, but not the other way around.

Replace

SqlDataReader reader = cmd.ExecuteReader();

with

cmd.ExecuteNonQuery();
Duracell
Maybe a dumb question, but what would be the benefit on replacing that?
Mike
Less overhead in creating the reader, which you don't need for an UPDATE statement. That and I don't believe the command text gets executed if you try to do it that way. Don't quote me on that :3
Duracell