I added a MS Access file into a project and VS created me a DataSet. I was very happy with that, but now I’ve a small problem:
I want to have the connection string in the app.config file, to allow customer to edit it. I go to the app.config file and I saw this connection String, “GREAT” I thought.
But it seems that this is not used. I found that the connectionString which is used is contained into the Settings.settings file, which I cannot edit at run-time.
Any idea how to resolve this? I even can specify myself the connection string programmatically if needed, but I didn’t find how to specify this.
Thank you very much!
EDIT: Here a some code:
One example of the generated code which seems to refer to the Settings.settings file:
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
private void InitConnection() {
this._connection = new global::System.Data.OleDb.OleDbConnection();
this._connection.ConnectionString = global::Infoteam.CliniqueLaSource.DocuShare.Communication.DatabaseAccess.Properties.Settings.Default.ClsConnectionString;
}
What I’ve in my app.config file:
<connectionStrings>
<add name="Infoteam.CliniqueLaSource.DocuShare.DatabaseAccess.Properties.Settings.ClsConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\Workspace\CliniqueLaSource.CLS-DocuShare\Main\Resources\Cls.mdb;Persist Security Info=True"
providerName="System.Data.OleDb" />
</connectionStrings>
What I’ve in the Settings.settings file:
<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="Infoteam.CliniqueLaSource.DocuShare.DatabaseAccess.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="ClsConnectionString" Type="(Connection string)" Scope="Application">
<DesignTimeValue Profile="(Default)">
<?xml version="1.0" encoding="utf-16"?>
<SerializableConnectionString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<ConnectionString>Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Cls.mdb;Persist Security Info=True</ConnectionString>
<ProviderName>System.Data.OleDb</ProviderName>
</SerializableConnectionString>
</DesignTimeValue>
<Value Profile="(Default)">Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\Cls.mdb;Persist Security Info=True</Value>
</Setting>
</Settings>
</SettingsFile>
Here is a small example of the usage I’m doing:
ClsDataSet dataSet = new ClsDataSet();
DOCDOCUMENTSTableAdapter adapter = new DOCDOCUMENTSTableAdapter();
adapter.Fill(dataSet.DOCDOCUMENTS);
return dataSet.DOCDOCUMENTS.ToList();
When VS creates a strongly typed
DataSetfor you, it will also create strongly typed wrappers aroundDataAdaptersto fill yourDataSetand to update the database.These strongly typed wrappers or
TableAdapterscan be configured to use any connection you want. Simply set theConnectionproperty to a connection initialized with the connection string you wish and you are all set.If setting the connection everytime you create a new
TableAdapteris too cumbersome you can also take advantage of them beingpartialclasses. This means you can add another code file that wont be overwritten everytime the DataSet designer tool updates the underlying code and overload the DataTable constructor so it takes a connection string or a connection instance and you set the property accordingly inside the constructor:If you have a lot of
TableAdaptersthis might be a bore and your better off just setting the connection manually each time you instantiate aTableAdapter