2

Hi i have CSV upload code in .net

In C#

 string strConnString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=D:\\csv;Extensions=asc,csv,tab,txt;Persist Security Info=False";
            DataSet ds;
            using (OdbcConnection oConn = new OdbcConnection(strConnString))
            {
                using (OdbcCommand oCmd = new OdbcCommand())
                {
                    oCmd.Connection = oConn;
                    oCmd.CommandType = System.Data.CommandType.Text;
                    oCmd.CommandText = "select * from [my.csv]";

                    OdbcDataAdapter oAdap = new OdbcDataAdapter();
                    oAdap.SelectCommand = oCmd;

                    ds = new DataSet();
                    oAdap.Fill(ds, "my");
                    oAdap.Dispose();                     
                    ds.Dispose();
                }
            }

In .VB

  Dim strConnString As String = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=D:\\csv;Extensions=asc,csv,tab,txt;Persist Security Info=False"

        Dim lOdbcConnection As New OdbcConnection(strConnString)
        '            lOdbcConnection.ConnectionString = strConnString
        'lOdbcConnection.Open()
        Using lOdbcCommand As New OdbcCommand, lOdbcDataAdapter As New OdbcDataAdapter
            lOdbcCommand.Connection = lOdbcConnection
            lOdbcCommand.CommandType = System.Data.CommandType.Text
            lOdbcCommand.CommandText = "select * from [my.csv]"

            lOdbcDataAdapter.SelectCommand = lOdbcCommand
            Dim ds As New DataSet()
            lOdbcDataAdapter.Fill(ds, "my")
            ds.Dispose()

            lOdbcDataAdapter.Dispose()

In C# is working fine But in .VB its giving error when filling the dataset .-

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

what I am doing wrong?

1
  • Not sure, but try change Dbq=D:\\csv; to Dbq=D:\csv;. Commented Sep 11, 2013 at 10:35

1 Answer 1

1

You don't need to escape the \ character in VB.NET strings so "Dbq=D:\\csv" should be "Dbq=D:\csv". That's why your data source isn't found.

Sign up to request clarification or add additional context in comments.

2 Comments

Other than the double \, the only other problem in your posted code that it isn't complete, lacking a Dim for the first variable and an End Using statement at the end. It compiles OK when these are corrected and runs OK as well (in VS2010) as long as the code points to a valid path and CSV filename. The error you're getting indicates that the CSV file can't be found or read so I'd trace through the code and make sure it's seeing the file OK. Also, make sure it isn't being locked by another process.
hi error is not in code actual problem with driver as in another machine working fine .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.