-2

I have searched around and it seem's the only answer I can get is to do with LINQ, which I don't have available on my visual stuido 2005.

I am building a program that reads files and imports them into a database, the way it's set up at the moment, it reads the very latest date it finds.

I want to read the earliest file first.

Is there any way around this?

Here is my code

     private string mDirectory; // this will hold the directory path you are working on
                    private string[] mFiles; // this will hold all files in the selected directory

    private void ReadData()
                    {
                    this.toolStripStatusLabel1.Text = "Preparing To Read Data";
                    this.Refresh();
                    string connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};;persist security info=false;Extended Properties=dBase IV", mDirectory);
                    this.richTextBox1.Text = connectionString;
                    try
                    {
                    foreach (string file in mFiles)
                    {

 mDirectory = @"C:\USERS\DESKTOP\Test Pressure\"; 
                mFiles = System.IO.Directory.GetFiles(mDirectory, "*(WIDE).DBF");

                        DateTime dt = File.GetLastWriteTime(file);
                        string newdate = dt.ToString("yyyy-MM-dd HH:mm:ss");
                        DateTime DBTIME = new DateTime(2014, 01, 01, 00, 00, 00);
                        string date = String.Format("{0:yyyy-MM-dd HH:mm:ss}", DBTIME);
                        //this.richTextBox1.Text = date;


                        if (dt > DBTIME)
                        {
                            StringBuilder sb = new StringBuilder(300);
                            int n = GetShortPathName(file, sb, 300);
                            if (n == 0) // check for errors
                                MessageBox.Show(Marshal.GetLastWin32Error().ToString());
                            else
                            { }
                            string filenameWithoutPath1 = System.IO.Path.GetFileName(sb.ToString());
                            string queryString = string.Format("SELECT * FROM [" + "{0}]", filenameWithoutPath1);
                            this.richTextBox1.Text = queryString;


                        string where = " WHERE BAR > 20.0";
                        string myquery = queryString + where;
                       // this.richTextBox1.Text = myquery;

                        using (OleDbConnection connection = new OleDbConnection(connectionString))
                        {
                            OleDbCommand command = new OleDbCommand(myquery, connection);
                            connection.Open();
                            OleDbDataReader reader = command.ExecuteReader();

                            while (reader.Read())
                            {
                                if (reader.IsDBNull(1))
                                {
                                    this.richTextBox1.Text = "Null";
                                }
                                else
                                {



                            string Query = "REPLACE INTO hp42mis.hydrodata (FILEMOD, DOEYMD,TIMEHMS,DATETIMEM,MARKER,CONTRACT_CODE,STS_00" +
                                           ",PIPE,PIPE_NO,STS_01,MAX_PRESB,STS_02,MIN_PRESSB,STS_03,TESTP_BAR,STS_04,five,STS_05,six,STS_06,seven,STS_07," +
                                           "eight,STS_08,nine,STS_09) values ('" + newdate.ToString() + "',";


                                    Query += "'" + Convert.ToDateTime(reader["Date"]).ToString("yyyy-MM-dd") + "'";
                                    Query += ",'" + reader.GetValue(1).ToString() + "'";
                                    Query += ",'" + Convert.ToDateTime(reader["Date"]).ToString("yyyy-MM-dd") + " " + reader.GetValue(1).ToString() + "'";
                                    Query += ",'" + reader.GetValue(2).ToString() + "'";
                                    Query += ",'" + reader.GetValue(3).ToString() + "'";
                                    Query += ",'" + reader.GetValue(4).ToString() + "'";
                                    Query += ",'" + reader.GetValue(5).ToString() + "'";
                                    Query += ",'" + reader.GetValue(3).ToString() + "" + reader.GetValue(5).ToString() + "'";
                                    Query += ",'" + reader.GetValue(6).ToString() + "'";
                                    Query += ",'" + reader.GetValue(7).ToString() + "'";                             
                                    Query += ",'" + reader.GetValue(8).ToString() + "'";
                                    Query += ",'" + reader.GetValue(9).ToString() + "'";
                                    Query += ",'" + reader.GetValue(10).ToString() + "'";
                                    Query += ",'" + reader.GetValue(11).ToString() + "'";
                                    Query += ",'" + reader.GetValue(12).ToString() + "'";
                                    Query += ",'" + reader.GetValue(13).ToString() + "'";
                                    Query += ",'" + reader.GetValue(14).ToString() + "'";
                                    Query += ",'" + reader.GetValue(15).ToString() + "'";
                                    Query += ",'" + reader.GetValue(16).ToString() + "'";
                                    Query += ",'" + reader.GetValue(17).ToString() + "'";
                                    Query += ",'" + reader.GetValue(18).ToString() + "'";
                                    Query += ",'" + reader.GetValue(19).ToString() + "'";
                                    Query += ",'" + reader.GetValue(20).ToString() + "'";
                                    Query += ",'" + reader.GetValue(21).ToString() + "'";
                                    Query += ",'" + reader.GetValue(22).ToString() + "'";
                                    Query += ")";



                                    MySqlCommand cmd = new MySqlCommand(Query, conne);
                                    this.richTextBox1.Text = Query;
                                    this.Refresh();
                                    int res = 0;
                                    try
                                    {
                                        res = cmd.ExecuteNonQuery();
                                    }
                                    catch (MySqlException Myex)
                                    {
                                        MessageBox.Show(Myex.Message);
                                    }
                                    rowcounter++;
                                    this.rowcount.Text = rowcounter.ToString();
                                    this.Refresh();
                                    conne.Dispose();
                                    conne.Close();

                                }                      
                            }                      
                            reader.Close();
                            connection.Close();
                        }
                    }
                    }



                    }

                    catch (Exception ex)
                    {
                    MessageBox.Show(ex.Message);
                    }
                    }
SK2017
  • 753
  • 9
  • 38
  • 3
    What is the relevance of your code? What would you like to sort? What does the proposed code using Linq look like, how have you tried to convert it to "plain C#" and why don't you upgrade your ten year old development environment? – CodeCaster Jan 13 '15 at 11:26
  • You could probably cut your codesample down to a few lines of code to better show what you want to sort, and how. In any case, using a custom comparator for the file system time and Array.Sort is probably what you want to do. – J. Steen Jan 13 '15 at 11:27
  • 1
    Please format your code properly – DrKoch Jan 13 '15 at 11:27
  • 1
    Additionally, *please* use parameterized SQL to avoid SQL injection attacks (and to make the code saner). – Jon Skeet Jan 13 '15 at 11:29
  • I appoligise for my inexperience of C# , i'm an absolute beginner, and I will take your quotes into account. – SK2017 Jan 13 '15 at 11:36

4 Answers4

2

I found an easier way how to sort the array of files...

mFiles = System.IO.Directory.GetFiles(mDirectory, "*(WIDE).DBF");

Array.Sort(mFiles);
SK2017
  • 753
  • 9
  • 38
0

If you want to walk over your mFiles array based on each file's last write time, see Sort List using string without Linq:

private IList<string> GetOrderedFiles(string[] files)
{
    var fileList = new List<string>(files);

    Comparison<string> compare = delegate(string file1, string file2)
    {
        DateTime file1Time = File.GetLastWriteTime(file1);
        DateTime file2Time = File.GetLastWriteTime(file2);

        return file1Time.CompareTo(file2Time);
    };

    fileList.Sort(compare);
    return fileList;
}

Usage:

var sortedFiles = GetOrderedFiles(mFiles);

foreach (string file in sortedFiles)
{

}
Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272
0

As others have said, that code needs reformatting and probably isn't even correct (e.g. you're setting mFiles within a loop iterating through mFiles).

However, maybe this will help?

Sorting Directory.GetFiles()

The key is using FileInfo items rather than just GetFiles().

Community
  • 1
  • 1
0

You can do this :

public class ReverseComparer : IComparer<FileSystemInfo>
        {
            public int Compare(FileSystemInfo x, FileSystemInfo y)
            {
             return x.CreationTime.CompareTo(y); 
            }
    }




 DirectoryInfo di = new DirectoryInfo("C:\\...");
 FileSystemInfo[] files = di.GetFileSystemInfos();
 Array.Sort(files, new ReverseComparer());
Royi Namir
  • 144,742
  • 138
  • 468
  • 792