Friday, June 27, 2014

DEMO#1 excel files into brand new access mdb file

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using System.Data;
using System.Data.OleDb;
using ADODB;
using ADOX;

namespace ClassCodeShim
{
    class ExcelFileCheck
    {
        public int sheetcount;
        public string[] sheetnames;
        public int[] rowcountpersheet;
        public int[] columncountpersheet;
        public List<string>[] columnnamespersheet;
        public object[] mycells_one_based;
    }//class ExcelFileCheck

    class Program
    {
        static void ScanDirectory(List<string> keeplist, string searchpath)
        {
            try
            {
                Console.WriteLine(searchpath);
                DirectoryInfo mydirinfo = new DirectoryInfo(searchpath);
                FileInfo[] folderfiles = mydirinfo.GetFiles();
                if (folderfiles == null)
                {
                    Console.WriteLine("folderfiles is null");
                }
                else
                {
                    for (int ii = 0; ii < folderfiles.Length; ii++)
                    {
                        string allupperstr = folderfiles[ii].FullName.ToUpper();
                        if (allupperstr.EndsWith(".SENT"))
                            keeplist.Add(allupperstr);
                    }
                }

                DirectoryInfo[] folderdirs = mydirinfo.GetDirectories();
                if (folderdirs == null)
                {
                    Console.WriteLine("folderdirs is null");
                }
                else
                {
                    for (int ii = 0; ii < folderdirs.Length; ii++)
                    {
                        ScanDirectory(keeplist, folderdirs[ii].FullName);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: inside ScanDirectory an exception was caught!");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                System.Environment.FailFast("PURPOSEFULLY TERMINATING THIS PROCESS BECAUSE OF CAUGHT EXCEPTION");
            }
        }//ScanDirectory

        static void GetSentFilesInfo(List<string> sentfilepaths, string[] createtime, string[] modtime)
        {
            try
            {
                for (int ii = 0; ii < sentfilepaths.Count; ii++ )
                {
                    string excelfilepath = sentfilepaths[ii];
                    FileInfo myfileinfo = new FileInfo(excelfilepath);
                    createtime[ii] = myfileinfo.CreationTime.ToLongDateString() + " " + myfileinfo.CreationTime.ToLongTimeString();
                    modtime[ii] = myfileinfo.LastWriteTime.ToLongDateString() + " " + myfileinfo.LastWriteTime.ToLongTimeString();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: inside GetSentFilesInfo an exception was caught!");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                System.Environment.FailFast("PURPOSEFULLY TERMINATING THIS PROCESS BECAUSE OF CAUGHT EXCEPTION");
            }
        }//GetSentFilesInfo

        static void All_ExcelFileCheck(List<string> sentpath, ExcelFileCheck[] checkarray)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                for (int ii = 0; ii < sentpath.Count; ii++)
                {
                    Console.WriteLine("READING {0}/{1}: {2}", ii, sentpath.Count, sentpath[ii]);
                    Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(sentpath[ii]);

                    ExcelFileCheck excelcheck = new ExcelFileCheck();
                    excelcheck.sheetcount = wb.Sheets.Count;
                    excelcheck.sheetnames = new string[wb.Sheets.Count];
                    excelcheck.rowcountpersheet = new int[wb.Sheets.Count];
                    excelcheck.columncountpersheet = new int[wb.Sheets.Count];
                    excelcheck.columnnamespersheet = new List<string>[wb.Sheets.Count];
                    excelcheck.mycells_one_based = new object[wb.Sheets.Count];

                    int sheet_idx = 0;
                    foreach (Worksheet sheet in wb.Sheets)
                    {
                        excelcheck.sheetnames[sheet_idx] = sheet.Name;
                        Range excelrange = sheet.UsedRange;
                        string addresscheck = excelrange.Address.ToString();
                        //Console.WriteLine(addresscheck);
                        //if (addresscheck.Equals("$A$1"))
                        //    Console.WriteLine("true on $A$1 check");
                        //else
                        //    Console.WriteLine("false on $A$1 check");
                        if (addresscheck.Equals("$A$1"))
                        {
                            object myobj = excelrange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
                            if (myobj == null)
                            {
                                //Console.WriteLine("myobj is null on A1 check");
                                excelcheck.columncountpersheet[sheet_idx] = 0;
                                excelcheck.rowcountpersheet[sheet_idx] = 0;
                                excelcheck.columnnamespersheet[sheet_idx] = null;
                                excelcheck.mycells_one_based[sheet_idx] = null;
                            }
                            else
                            {
                                //Console.WriteLine("myobj is ok on A1 check");
                                //Console.WriteLine(myobj.ToString());
                                excelcheck.columncountpersheet[sheet_idx] = 1;
                                excelcheck.rowcountpersheet[sheet_idx] = 1;
                                excelcheck.columnnamespersheet[sheet_idx] = null;
                                excelcheck.mycells_one_based[sheet_idx] = myobj;
                            }
                        }
                        else
                        {
                            object[,] mycells = (object[,])excelrange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
                            //Console.WriteLine(excelfilepath);
                            //Console.WriteLine(string.Format("SHEET{0}: {1}", sheet_idx, sheet.Name));
                            //Console.WriteLine(string.Format("ROWS={0} COLS={1}", excelrange.Rows.Count, excelrange.Columns.Count));
                            if (mycells == null)
                            {
                                //Console.WriteLine("mycells is null");
                                excelcheck.columncountpersheet[sheet_idx] = 0;
                                excelcheck.rowcountpersheet[sheet_idx] = 0;
                                excelcheck.columnnamespersheet[sheet_idx] = null;
                                excelcheck.mycells_one_based[sheet_idx] = null;
                            }
                            else
                            {
                                //Console.WriteLine("mycells is not null");
                                excelcheck.rowcountpersheet[sheet_idx] = excelrange.Rows.Count;
                                excelcheck.columncountpersheet[sheet_idx] = excelrange.Columns.Count;
                                excelcheck.columnnamespersheet[sheet_idx] = new List<string>();
                                excelcheck.mycells_one_based[sheet_idx] = mycells;

                                for (int col = 1; col <= excelrange.Columns.Count; col++)
                                {
                                    if (mycells[1, col] == null)
                                        excelcheck.columnnamespersheet[sheet_idx].Add("NULL");
                                    else
                                    {
                                        string temp = mycells[1, col].ToString();
                                        string tempupper = temp.ToUpper();
                                        excelcheck.columnnamespersheet[sheet_idx].Add(tempupper);
                                    }
                                }
                            }
                        }
                        sheet_idx++;
                    }//for sheet
                    wb.Close();
                    checkarray[ii] = excelcheck;
                }//for sentpath
                app.Quit();
            }//try
            catch (Exception e)
            {
                Console.WriteLine("ERROR: inside GetExcelFileCheckInfo an exception was caught!");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                System.Environment.FailFast("PURPOSEFULLY TERMINATING THIS PROCESS BECAUSE OF CAUGHT EXCEPTION");
            }
        }//All_ExcelFileCheck

        static void One_ExcelFileCheck(string excelfilepath, ExcelFileCheck excelcheck)
        {
            try
            {
                Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
                Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Open(excelfilepath);

                excelcheck.sheetcount = wb.Sheets.Count;
                excelcheck.sheetnames = new string[wb.Sheets.Count];
                excelcheck.rowcountpersheet = new int[wb.Sheets.Count];
                excelcheck.columncountpersheet = new int[wb.Sheets.Count];
                excelcheck.columnnamespersheet = new List<string>[wb.Sheets.Count];
                excelcheck.mycells_one_based = new object[wb.Sheets.Count];

                int sheet_idx = 0;
                foreach (Worksheet sheet in wb.Sheets)
                {
                    //Console.WriteLine(string.Format("SHEET{0}: {1}", sheet_idx, sheet.Name));

                    excelcheck.sheetnames[sheet_idx] = sheet.Name;
                    Range excelrange = sheet.UsedRange;

                    string addresscheck = sheet.UsedRange.Address.ToString();
                    //Console.WriteLine(addresscheck);
                    //if (addresscheck.Equals("$A$1"))
                    //    Console.WriteLine("true on $A$1 check");
                    //else
                    //    Console.WriteLine("false on $A$1 check");
                    if (addresscheck.Equals("$A$1"))
                    {
                        object myobj = excelrange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
                        if (myobj == null)
                        {
                            //Console.WriteLine("myobj is null on A1 check");
                            excelcheck.columncountpersheet[sheet_idx] = 0;
                            excelcheck.rowcountpersheet[sheet_idx] = 0;
                            excelcheck.columnnamespersheet[sheet_idx] = null;
                            excelcheck.mycells_one_based[sheet_idx] = null;
                        }
                        else
                        {
                            //Console.WriteLine("myobj is ok on A1 check");
                            //Console.WriteLine(myobj.ToString());
                            excelcheck.columncountpersheet[sheet_idx] = 1;
                            excelcheck.rowcountpersheet[sheet_idx] = 1;
                            excelcheck.columnnamespersheet[sheet_idx] = null;
                            excelcheck.mycells_one_based[sheet_idx] = myobj;
                        }
                    }
                    else
                    {
                        object[,] mycells = (object[,])excelrange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
                        //Console.WriteLine(excelfilepath);
                        //Console.WriteLine(string.Format("SHEET{0}: {1}", sheet_idx, sheet.Name));
                        //Console.WriteLine(string.Format("ROWS={0} COLS={1}", excelrange.Rows.Count, excelrange.Columns.Count));
                        if (mycells == null)
                        {
                            //Console.WriteLine("mycells is null");
                            excelcheck.columncountpersheet[sheet_idx] = 0;
                            excelcheck.rowcountpersheet[sheet_idx] = 0;
                            excelcheck.columnnamespersheet[sheet_idx] = null;
                            excelcheck.mycells_one_based[sheet_idx] = null;
                        }
                        else
                        {
                            //Console.WriteLine("mycells is not null");
                            excelcheck.rowcountpersheet[sheet_idx] = excelrange.Rows.Count;
                            excelcheck.columncountpersheet[sheet_idx] = excelrange.Columns.Count;
                            excelcheck.columnnamespersheet[sheet_idx] = new List<string>();
                            excelcheck.mycells_one_based[sheet_idx] = mycells;

                            for (int col = 1; col <= excelrange.Columns.Count; col++)
                            {
                                if (mycells[1, col] == null)
                                    excelcheck.columnnamespersheet[sheet_idx].Add("NULL");
                                else
                                {
                                    string temp = mycells[1, col].ToString();
                                    string tempupper = temp.ToUpper();
                                    excelcheck.columnnamespersheet[sheet_idx].Add(tempupper);
                                }
                            }
                        }
                    }

                    sheet_idx++;
                }
                wb.Close();
                app.Quit();
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: inside One_ExcelFileCheck an exception was caught!");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                System.Environment.FailFast("PURPOSEFULLY TERMINATING THIS PROCESS BECAUSE OF CAUGHT EXCEPTION");
            }
        }//One_ExcelFileCheck

        static void Debug_One_ExcelFileCheck(string excelfilepath, ExcelFileCheck excelcheck)
        {
            try
            {
                //Console.WriteLine("SENT = {0} SHEETCOUNT = {1}", excelfilepath, excelcheck.sheetcount);
                for (int ss = 0; ss < excelcheck.sheetcount; ss++)
                {
                    //Console.WriteLine("SHEET{0} = {1}", ss, excelcheck.sheetnames[ss]);
                    //Console.WriteLine("ROWS = {0} COLS = {1}", excelcheck.rowcountpersheet[ss], excelcheck.columncountpersheet[ss]);

                    if (excelcheck.rowcountpersheet[ss] == 1 && excelcheck.columncountpersheet[ss] == 1)
                    {
                        object myobj = excelcheck.mycells_one_based[ss];
                        //if (myobj == null)
                        //    Console.WriteLine("NULL ");
                        //else
                        //    Console.WriteLine(myobj.ToString() + " ");
                    }
                    else //normal mycells_one_based
                    {
                        for (int rr = 1; rr <= excelcheck.rowcountpersheet[ss]; rr++)
                        {
                            for (int cc = 1; cc <= excelcheck.columncountpersheet[ss]; cc++)
                            {
                                object[,] mycells = (object[,])excelcheck.mycells_one_based[ss];
                                //if (mycells[rr, cc] == null)
                                //    Console.Write("NULL ");
                                //else
                                //    Console.Write(mycells[rr, cc].ToString() + " ");
                            }
                            //Console.WriteLine();
                        }
                    }
                }
                //Console.WriteLine();
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: inside DebugPrintExcelFileCheck an exception was caught!");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                System.Environment.FailFast("PURPOSEFULLY TERMINATING THIS PROCESS BECAUSE OF CAUGHT EXCEPTION");
            }
        }//Debug_One_ExcelFileCheck

        static void Debug_All_ExcelFileCheck(List<string> sentpath, ExcelFileCheck[] checkarray)
        {
            try
            {
                for (int ff = 0; ff < sentpath.Count; ff++)
                {
                    string excelfilepath = sentpath[ff];
                    ExcelFileCheck excelcheck = checkarray[ff];

                    //Console.WriteLine("SENT = {0} SHEETCOUNT = {1}", excelfilepath, excelcheck.sheetcount);
                    for (int ss = 0; ss < excelcheck.sheetcount; ss++)
                    {
                        //Console.WriteLine("SHEET{0} = {1}", ss, excelcheck.sheetnames[ss]);
                        //Console.WriteLine("ROWS = {0} COLS = {1}", excelcheck.rowcountpersheet[ss], excelcheck.columncountpersheet[ss]);
                        if (excelcheck.rowcountpersheet[ss] == 1 && excelcheck.columncountpersheet[ss] == 1)
                        {
                            object myobj = excelcheck.mycells_one_based[ss];
                            //if (myobj == null)
                            //    Console.WriteLine("NULL ");
                            //else
                            //    Console.WriteLine(myobj.ToString() + " ");
                        }
                        else
                        {
                            for (int rr = 1; rr <= excelcheck.rowcountpersheet[ss]; rr++)
                            {
                                for (int cc = 1; cc <= excelcheck.columncountpersheet[ss]; cc++)
                                {
                                    object[,] mycells = (object[,])excelcheck.mycells_one_based[ss];
                                    //if (mycells[rr, cc] == null)
                                    //    Console.Write("NULL ");
                                    //else
                                    //    Console.Write(mycells[rr, cc].ToString() + " ");
                                }
                                //Console.WriteLine();
                            }
                        }
                    }
                    //Console.WriteLine();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: inside DebugPrintExcelFileCheck an exception was caught!");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                System.Environment.FailFast("PURPOSEFULLY TERMINATING THIS PROCESS BECAUSE OF CAUGHT EXCEPTION");
            }
        }//Debug_All_ExcelFileCheck

        static int FindMaxColumns(List<string> excelfilepaths, ExcelFileCheck[] checkarray)
        {
            int maxfile = 0;
            int maxsheet = 0;
            int maxval = 0;
            for (int ii=0; ii < checkarray.Length; ii++)
            {
                for (int ss=0; ss < checkarray[ii].sheetcount; ss++ )
                {
                    if (checkarray[ii].columncountpersheet[ss] > maxval)
                    {
                        maxval = checkarray[ii].columncountpersheet[ss];
                        maxsheet = ss;
                        maxfile = ii;
                    }
                }
            }

            Console.WriteLine("maxval = {0}", maxval);
            if (maxval > 0)
            {
                Console.WriteLine("maxfile = {0}", excelfilepaths[maxfile]);
                Console.WriteLine("maxsheet = {0}", maxsheet);
            }

            return maxval;
        }//FindMaxColumns

        static void MakeAccessMDB(int maxcolumns, List<string> sentpath, string[] sentcreate, string[] sentmod, ExcelFileCheck[] checkarray, string outpath, string outfilename)
        {
            try
            {
                ADOX.Catalog cat = new Catalog();
                ADOX.Table table = new Table();

                table.Name = "mytable";

                ADOX.Column importcol = new Column();
                importcol.Name = "IMPORT_DATE_TIME";
                importcol.Type = ADOX.DataTypeEnum.adVarWChar;
                importcol.Attributes = ColumnAttributesEnum.adColNullable;
                table.Columns.Append(importcol);

                ADOX.Column foldercol = new Column();
                foldercol.Name = "FOLDER";
                foldercol.Type = ADOX.DataTypeEnum.adVarWChar;
                foldercol.Attributes = ColumnAttributesEnum.adColNullable;
                table.Columns.Append(foldercol);

                ADOX.Column filenamecol = new Column();
                filenamecol.Name = "FILE_NAME";
                filenamecol.Type = ADOX.DataTypeEnum.adVarWChar;
                filenamecol.Attributes = ColumnAttributesEnum.adColNullable;
                table.Columns.Append(filenamecol);

                ADOX.Column sheetcol = new Column();
                sheetcol.Name = "SHEET_NAME";
                sheetcol.Type = ADOX.DataTypeEnum.adVarWChar;
                sheetcol.Attributes = ColumnAttributesEnum.adColNullable;
                table.Columns.Append(sheetcol);

                ADOX.Column createcol = new Column();
                createcol.Name = "FILE_CREATED";
                createcol.Type = ADOX.DataTypeEnum.adVarWChar;
                createcol.Attributes = ColumnAttributesEnum.adColNullable;
                table.Columns.Append(createcol);

                ADOX.Column modcol = new Column();
                modcol.Name = "FILE_MODIFIED";
                modcol.Type = ADOX.DataTypeEnum.adVarWChar;
                modcol.Attributes = ColumnAttributesEnum.adColNullable;
                table.Columns.Append(modcol);

                for (int ii = 0; ii < maxcolumns; ii++)
                {
                    string colname;
                    if (ii <= 25) //A = 'A' + 0  but  Z = 'A' + 25 because 26 letters in alphabet
                        colname = string.Format("{0}", (char)('A' + ii));
                    else
                        colname = string.Format("{0}", ii);

                    ADOX.Column mycol = new Column();
                    mycol.Name = colname;
                    mycol.Type = ADOX.DataTypeEnum.adVarWChar;
                    mycol.Attributes = ColumnAttributesEnum.adColNullable;
                    table.Columns.Append(mycol);
                }

                string filename = outpath + "\\" + outfilename;
                if (File.Exists(filename))
                    File.Delete(filename);

                cat.Create("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + filename + "; Jet OLEDB:Engine Type=5");
                cat.Tables.Append(table);

                //Now Close the database
                ADODB.Connection con = cat.ActiveConnection as ADODB.Connection;
                if (con != null)
                    con.Close();


                OleDbConnection dbconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filename);
                OleDbDataAdapter adapter = new OleDbDataAdapter();
                dbconn.Open();
                for (int ff = 0; ff < sentpath.Count; ff++)
                {
                    Console.WriteLine("WRITING {0}/{1}: {2}", ff, sentpath.Count, sentpath[ff]);
                    for (int ss = 0; ss < checkarray[ff].sheetcount; ss++)
                    {
                        for (int rr = 1; rr <= checkarray[ff].rowcountpersheet[ss]; rr++)
                        {
                            StringBuilder sb = new StringBuilder();
                            sb.Append("INSERT INTO mytable ([IMPORT_DATE_TIME], [FOLDER], [FILE_NAME], [SHEET_NAME], [FILE_CREATED], [FILE_MODIFIED]");
                            for (int xx = 0; xx < checkarray[ff].columncountpersheet[ss]; xx++)
                            {
                                string tempcol;
                                if (xx <= 25)
                                    tempcol = string.Format(", [{0}]", (char)('A' + xx));
                                else
                                    tempcol = string.Format(", [{0}]", xx);
                                sb.Append(tempcol);
                            }
                            int lastidx = sentpath[ff].LastIndexOf("\\");
                            string folder = sentpath[ff].Substring(0, lastidx);
                            string file_name = sentpath[ff].Substring(lastidx + 1);
                            DateTime current_time = DateTime.Now;
                            string import_time = current_time.ToLongDateString() + " " + current_time.ToLongTimeString();

                            string import_NOQUOTE = import_time.Replace("'", "''");
                            string folder_NOQUOTE = folder.Replace("'", "''");
                            string filename_NOQUOTE = file_name.Replace("'", "''");
                            string sheetname_NOQUOTE = checkarray[ff].sheetnames[ss].Replace("'", "''");
                            string create_NOQUOTE = sentcreate[ff].Replace("'", "''");
                            string mod_NOQUOTE = sentmod[ff].Replace("'", "''");

                            string tempstr = string.Format(") VALUES ('{0}', '{1}', '{2}', '{3}', '{4}', '{5}'", import_NOQUOTE, folder_NOQUOTE, filename_NOQUOTE, sheetname_NOQUOTE, create_NOQUOTE, mod_NOQUOTE);
                            sb.Append(tempstr);
                            for (int cc = 1; cc <= checkarray[ff].columncountpersheet[ss]; cc++)
                            {
                                if (checkarray[ff].rowcountpersheet[ss] == 1 && checkarray[ff].columncountpersheet[ss] == 1)
                                {
                                    object mycells = (object)checkarray[ff].mycells_one_based[ss];
                                    string tempvalstr;
                                    if (mycells == null)
                                        throw new Exception("mycells is NULL inside MakeAccessMDB for one cell! This should be impossible, Fix this please Max Lee");
                                    else
                                    {
                                        if (mycells == null)
                                            tempvalstr = ", 'NULL'";
                                        else
                                        {
                                            string cellstr = mycells.ToString();
                                            string cell_NOQUOTE = cellstr.Replace("'", "''");
                                            if (cell_NOQUOTE.Length > 255) //Microsoft Access can only take 255 length strings
                                            {
                                                Console.WriteLine("ONE CELL: FORCING STRING TO 255 LENGTH");
                                                tempvalstr = string.Format(", '{0}'", cell_NOQUOTE.Substring(0, 255));
                                            }
                                            else
                                                tempvalstr = string.Format(", '{0}'", cell_NOQUOTE);
                                        }
                                        sb.Append(tempvalstr);
                                    }
                                }
                                else
                                {
                                    object[,] mycells = (object[,])checkarray[ff].mycells_one_based[ss];
                                    string tempvalstr;
                                    if (mycells == null)
                                        throw new Exception("mycells is NULL inside MakeAccessMDB for many cells! This should be impossible, Fix this please Max Lee");
                                    else
                                    {
                                        if (mycells[rr, cc] == null)
                                            tempvalstr = ", 'NULL'";
                                        else
                                        {
                                            string cellstr = mycells[rr, cc].ToString();
                                            string cell_NOQUOTE = cellstr.Replace("'", "+");
                                            if (cell_NOQUOTE.Length > 255) //Microsoft Access can only take 255 length strings
                                            {
                                                Console.WriteLine("MANY CELLS: FORCING STRING TO 255 LENGTH");
                                                tempvalstr = string.Format(", '{0}'", cell_NOQUOTE.Substring(0, 255));
                                            }
                                            else
                                                tempvalstr = string.Format(", '{0}'", cell_NOQUOTE);
                                        }
                                        sb.Append(tempvalstr);
                                    }
                                }
                            }
                            sb.Append(")");

                            //Console.WriteLine(sb.ToString());
                            //Console.WriteLine(string.Format("STRLEN={0}", sb.ToString().Length));

                            OleDbCommand cmd = new OleDbCommand();
                            cmd.CommandText = sb.ToString();
                            cmd.CommandType = CommandType.Text;
                            cmd.Connection = dbconn;
                            cmd.ExecuteNonQuery();

                            sb.Clear();
                        }//row
                    }//sheet
                }//file
                dbconn.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR: exception caught in MakeAccessMDB");
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                System.Environment.FailFast("PURPOSEFULLY TERMINATING THIS PROCESS BECAUSE OF CAUGHT EXCEPTION");
            }
        }//MakeAccessMDB

        static void Main(string[] args)
        {
            //ExcelFileCheck excelcheck = new ExcelFileCheck();
            //string excelfilepath = "C:\\Users\\leem\\TEST_DATA\\DOE_CLS_CD_RPT_2012MMDD.xls.sent";
            //One_ExcelFileCheck(excelfilepath, excelcheck);
            //Debug_One_ExcelFileCheck(excelfilepath, excelcheck);

            Console.WriteLine("Please enter directory to scan (ex: C:\\Users\\leem\\TEST_DATA):");
            string scandir = Console.ReadLine();
            if (scandir.Length == 0)
                scandir = "C:\\Users\\leem\\TEST_DATA";
            Console.WriteLine(string.Format("ENTERED: {0} LENGTH:{1}", scandir, scandir.Length));

            Console.WriteLine("Please enter directory to output Access mdb file (ex: C:\\Users\\leem\\TEST_DATA):");
            string outpath = Console.ReadLine();
            if (outpath.Length == 0)
                outpath = "C:\\Users\\leem\\TEST_DATA";
            Console.WriteLine(string.Format("ENTERED: {0} LENGTH:{1}", outpath, outpath.Length));

            Console.WriteLine("Please enter filename of output Access mdb file (ex: mytest.mdb):");
            string outfilename = Console.ReadLine();
            if (outfilename.Length == 0)
                outfilename = "mytest.mdb";
            Console.WriteLine(string.Format("ENTERED: {0} LENGTH:{1}", outfilename, outfilename.Length));

            List<string> sentpath = new List<string>(); //contains sentfile paths
            ScanDirectory(sentpath, scandir);

            string[] sentcreate = new string[sentpath.Count];   //contains sentfile creation date and time
            string[] sentmod = new string[sentpath.Count];      //contains sentfile modified date and time
            GetSentFilesInfo(sentpath, sentcreate, sentmod);

            ExcelFileCheck[] checkarray = new ExcelFileCheck[sentpath.Count];
            All_ExcelFileCheck(sentpath, checkarray);
            //Debug_All_ExcelFileCheck(sentpath, checkarray);
            int maxcolumns = FindMaxColumns(sentpath, checkarray);
            MakeAccessMDB(maxcolumns, sentpath, sentcreate, sentmod, checkarray, outpath, outfilename);

            Console.WriteLine("DONE WITH NO EXCEPTIONS");
        }//Main
    }//class Program
}//namespace ClassCodeShim

No comments:

Post a Comment