Dhanil Dinesan
Dhanil Dinesan

Reputation: 575

How connect the uploaded excel file using OLEDB?

I just want to check whether it's possible to connect to an Excel file I recently uploaded, here I'm using OLE DB. I wanted to check whether can I connect to that file properly or not and return a message. But when I try to connect to OLE DB it uploading the same file again. Can you help me with this problem

I'm using C# MVC and OLE DB I have already uploaded the file before checking its an excel file or not.

the controller for calling business and save file

if (_dataExchangeBusiness.IsExcelFile(fname)==true)
                    {                       
                        file.SaveAs(fname);
                        bool connectioncheck;
                        connectioncheck = _dataExchangeBusiness.CheckConnection(fname);
                        return Json(new { Result = "true", Message = "" });

                    }
                     else
                    {
                        return Json(new { Result = "false", Message = "" });
                    }

this is the business where the checking for excel file

 public bool IsExcelFile(string fname)
        {          
            string extension = Path.GetExtension(fname);
            try
            {
               if(extension== ".xls" || extension== ".xlsx")
                {                                       
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }           
        }

and here is the checking for OLE DB connection checking in business

   public bool CheckConnection(string fname)
        {
            string extension = Path.GetExtension(fname);
            try
            {
                string connstring = string.Empty;
                switch (extension)
                {
                    case ".xls":
                        connstring = string.Format(ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString, fname);
                        break;
                    case ".xlsx":
                        connstring = string.Format(ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString, fname);
                        break;
                }
                OleDbConnection connExcel = new OleDbConnection(connstring);
                OleDbCommand cmdExcel = new OleDbCommand();
                cmdExcel.Connection = connExcel;
                try
                {
                    connExcel.Open();
                    DataTable dtExcelSchema;

                    dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    // cmdExcel.ExecuteNonQuery();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
                finally
                {
                    connExcel.Close();
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
            return true;
        }

Here I'm expecting a result of true or false, that is whether can establish a connection or not

Upvotes: 0

Views: 946

Answers (1)

Dhanil Dinesan
Dhanil Dinesan

Reputation: 575

public bool CheckConnection(string fname)
        {
            string extension = Path.GetExtension(fname);
            try
            {
                string connstring = string.Empty;
                switch (extension)
                {
                    case ".xls":
                        connstring = string.Format(ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString, fname);
                        break;
                    case ".xlsx":
                        connstring = string.Format(ConfigurationManager.ConnectionStrings["Excel07+ConString"].ConnectionString, fname);
                        break;
                }
                OleDbConnection connExcel = new OleDbConnection(connstring);
                OleDbCommand cmdExcel = new OleDbCommand();
                cmdExcel.Connection = connExcel;
                //bool canconnect = false;
                try
                {

                    connExcel.Open();
                    return true;
                    //DataTable dtExcelSchema;

                    //dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    // cmdExcel.ExecuteNonQuery();
                }
                catch 
                {
                    return false;
                }
                finally
                {
                    connExcel.Close();
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }

        }

just return true after connExcel.Open(); will get true if can connect else return false

Upvotes: 1

Related Questions