locked
ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified RRS feed

  • Question

  • User1525137291 posted

    Hello All,

    I am trying to estalish a connection and import the CSV files from a network share to a dataset. Here is the code I am using.

    app.config file:

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <appSettings >
        <add key="ImportLocation" value="\\networkshare\xyzfoldername"/>
     
      </appSettings>
     
    </configuration>

     private static void Import()
            {
                string filepath = GetFile();
                DataSet ds = Utility.ConnectCSV("", filepath);
             
            }

     private static string GetFile()
            {
                string filepath = ConfigurationManager.AppSettings["ImportLocation"].ToString();

                string filename = "\\CapitalSpend." + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString("00") + DateTime.Now.Day.ToString("00") + ".csv";

                string fullpath = filepath + filename;
                return fullpath;
            }

     public static DataSet ConnectCSV (string filetable, string filepath)
            {
              DataSet ds = new DataSet ();
              System.Data.Odbc.OdbcDataAdapter obj_oledb_da;
              try
               {        
                   /* You can get connected to driver either by using
                   DSN or connection string. Create a connection string
                   as below, if you want to use DSN less connection.
                   The DBQ attribute sets the path of directory which
                   contains CSV files*/

                   string strConnString =
                         "Driver={Microsoft Text Driver (*.txt;*.csv)}; Dbq=" + filepath.Trim() + ";Extensions=asc,csv,tab,txt;Persist Security Info=False";

                   string sql_select;                                
                   System.Data.Odbc.OdbcConnection conn;        
            
                   //Create connection to CSV file

                  // conn = new System.Data.Odbc.OdbcConnection(strConnString.Trim ());

                   conn = new System.Data.Odbc.OdbcConnection();

                   // For creating a connection using DSN, use following line

                   //conn = new System.Data.Odbc.OdbcConnection(DSN="MyDSN");

        
                   //Open the connection

                   conn.Open ();
                   //Fetch records from CSV

                   sql_select="select * from ["+ filetable +"]";
                    
                   obj_oledb_da=new System.Data.Odbc.OdbcDataAdapter(sql_select,conn);
                   //Fill dataset with the records from CSV file
                   obj_oledb_da.Fill(ds,"Actuals");
                  
                   //Close Connection to CSV file
                   conn.Close ();                
               }
               catch (Exception e) //Error
               {
                   throw e;
               }
               return ds;
    }

    When I run this code, It throws out an exception when trying to open the connection as "
    ERROR [IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified".

    Please anyone let me know the cause for this and how this can be rectified.

    Thank you

    Smith

    Wednesday, June 22, 2011 1:31 PM

All replies

  • User3866881 posted

    Hello:)

    I guess there's something wrong with your file path, would you mind listing it out for us to see?

    Thx

    Thursday, June 23, 2011 10:15 PM
  • User1525137291 posted

    Thank you for your reply. the filepath is \\networkshare\xyzfoldername\XYZfilename.csv.

    Please let me know. How can i correct this error.

    I appreciate your responses.

    Thank You,

    Smith

    Tuesday, June 28, 2011 2:10 PM
  • User3866881 posted

    Thank you for your reply. the filepath is \\networkshare\xyzfoldername\XYZfilename.csv.

    Please let me know. How can i correct this error.

    Hello:)

    I think You can try to change your app settings like this——

     <add key="ImportLocation" value="\networkshare\xyzfoldername\XYZfilename.csv"/>  <!--Just use a single "\"-->

    BTW: make sure that your remote csv can be downloaded or accessed. You've got the authentication or authorization.

    Thx

    Tuesday, June 28, 2011 9:06 PM