views:

154

answers:

1

Hello,

I have a simple test using vsts load test that is using datasource. The connection string for the source is as follows

<connectionStrings>
    <add name="MyExcelConn" connectionString="Driver={Microsoft Excel Driver (*.xls)};Dsn=Excel Files;dbq=loginusers.xls;defaultdir=.;driverid=790;maxbuffersize=4096;pagetimeout=20;ReadOnly=False" providerName="System.Data.Odbc" />
  </connectionStrings>

the datasource configuration is as follows

and i am getting following error

estError TestError 1,000 The unit test adapter failed to connect to the data source or to read the data. For more information on troubleshooting this error, see "Troubleshooting Data-Driven Unit Tests" (http://go.microsoft.com/fwlink/?LinkId=62412) in the MSDN Library. Error details: ERROR [42000] [Microsoft][ODBC Excel Driver] Cannot update. Database or object is read-only. ERROR [IM006] [Microsoft][ODBC Driver Manager] Driver's SQLSetConnectAttr failed ERROR [42000] [Microsoft][ODBC Excel Driver] Cannot update. Database or object is read-only.

I wrote a test, just to check if i could create an odbc connection would work and that works

the test is as follows

[TestMethod]       
        public void TestExcelFile()
        {
            string connString = ConfigurationManager.ConnectionStrings["MyExcelConn"].ConnectionString;
            using (OdbcConnection con = new OdbcConnection(connString))
            {
                con.Open();
                System.Data.Odbc.OdbcCommand objCmd = new OdbcCommand("SELECT * FROM [loginusers$]");
                objCmd.Connection = con;
                OdbcDataAdapter adapter = new OdbcDataAdapter(objCmd);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                Assert.IsTrue(ds.Tables[0].Rows.Count > 1);
            }


    }

any ideas ?

+2  A: 

I've been thru the same thing. ConfigurationManager.ConnectionStrings referes to the connection strings in the .config file for the current project.

However, the tests run in their own project; they have their own .config file. The only way around this that I've found is to do the following:

1) Go in to your test run configuration and add the app.config or web.config that contains your configuration strings as a deployment item.

2) Create a batch file with the following command:

copy app.config test_project_name.dll.config

3) Set the initialization script (in the test run configuration) to be the batch file created in Step #2.

It's a kludge in my mind, but a senior co-worker tells me that ordinarily, the .net framework doesn't like you using outside .config files for the ConfigurationManager. When you do steps 1-3, you will copy your App.config from your original project to where the .net framework expects to find the .config file for your test project.

Rice Flour Cookies