Question

We have a VS2010 solution that includes a web app (the startup project) and a class library for data access. The connection string is stored in the web app in a connectionString.config, which is referenced in the web app's web.config. The existing code in the class library uses System.Data.SqlClient to hit the database. This code gets the connections string via:

ConfigurationManager.ConnectionStrings["OurDbName"].ToString();

There are no connection string settings in the class iibrary, so at runtime this pulls the connection string from the web app's configuration.

I want to add "visual" xsd datasets to the class library. However, to do so you need a connection string defined at design-time, and the designer does not recognize the run-time default configuration in the web app. So as things currently are, to create and work with xsd datasets in the class library, I need to define a separate connection string in there (via the project's Settings window). At least I do if I want the drag/drop and context menu functionality in the dataset designer.

I know how to set the connection string at runtime. None of the several ways I've tried were available during design-time.

Is there a way have a connection string defined somewhere other than the data access class library yet still have the connection available to xsd datasets in the class library at design-time?

I think the first obvious answer is to have the connection configuration in the data access class library and if necessary have other projects reference it there. I'm not at this point able to guarantee that's an option, though.

Thanks!

Was it helpful?

Solution

"Is there a way have a connection string defined somewhere other than the data access class library yet still have the connection available to xsd datasets in the class library at design-time?"

[Know this is an old thread, but I had the same issue and didn't find the full answer anywhere else ... so, for future reference ... ]

The best way I've found to have the benefits of the XSD designer and be able to change the connection settings for staging and production is as follows: - NOTE: this works in VS 2012 ... think it should also in 2010 - Also, DataSets were created by dragging from the Server Explorer, which saves connection information in the class library app.config and Properties.Settings.settings which is used at design time.

1) For each DataTable, click on the TableAdapter header (below the attributes and above the methods) and look at the properties 2) Change the Connection Modifier to Public 3) Then, the code to access the table should be something like the following (where the class library is named DALib) ...

using DALib;
using System.Web.Configuration;

PriceData.averageSalePriceFDataTable priceTable;
DALib.PriceDataTableAdapters.averageSalePriceFTableAdapter priceAdapter
  = new DALib.PriceDataTableAdapters.averageSalePriceFTableAdapter();

priceAdapter.Connection.ConnectionString 
  =  WebConfigurationManager.ConnectionStrings["MyConnection"].ConnectionString;

priceTable = priceAdapter.GetData( ... parameters ...);

This uses the connection string in the class library for design and picks it from Web.config when running.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top