I am building a winform app that needs to be used to create/edit numerous Access databases. I have created all of the datasets using the tools in VS2005 to create my objects to bind to my forms. I found the connectionstring in my appconfig and it points to my dummy/prototype MDB just fine.
I have created a Open function that allows me to select the Access MDB that I currently want to work with. How do I change the connectionstring to talk to talk to this MDB instead of the one set in my app.config.
I tried creating a new string and telling the XSD file to use this file, but the XSD keeps reverting back to it's default value for the this._connection = string.
Suggestions or Ideas
Thanks all!
David

HELP! Change the database at runtime?
Duncan Woods
What I ended up doing was just manipulating the dataset's connection. This worked better for the short-term. My plan is to next create a common connection property using generics and appy it to all datasets.
I understand your feeling, but it seems that we just need to think about things on a lower level.
Cheers!
David
Brad-RDA
Surely the connection property is not public, so this code won't work.
Vassilux
A technique I have used is to first create a procedure called GetConnection()
This returns either the one global connection that is created at startup, or a new connection (depending on connection pooling or other factors that determine whether one-global-connection is acceptable or not ) but in either case using a connection string that includes the end-user specified path to their database.
Then before each query involving a tableadapter, I just include the line:
taMyAdapter.connection = GetConnection()
The tableadapter then uses the runtime connection rather than the designtime one . .
HTH
pg
daph4ntom
OK this is frightening - Are you telling me that to manipulate the connection string for each of my datasets, I have to create a partial class for EVERY involved table adapter
That's insane. Why would MS assume that a connection string used in development would be EXACTLY the same as one used in production I'm constantly shocked by the amount of hand coding required to get datasets up and running. I see people and books everywhere handcoding entire objects around tables - why am I the only one that finds this totally unacceptable
I'm less and less impressed with ADO.NET in VS 2005 as time goes on - seems like two steps backwards, especially compared to Delphi's database tools.
At least DLINQ looks like it will solve some of this - at least I hope it does.
I hope I'm just misintepreting this post.
Nick H
jkeele
namespace Sedodream.DataAccess.DataSet.WorkFlowsDSTableAdapters
Sayed Ibrahim Hashimi{
public partial class WorkFlowsTableAdapter
{
public WorkFlowsTableAdapter(string connectionString)
: this()
{
this.Connection = new SqlConnection(connectionString);
}
public WorkFlowsTableAdapter(SqlConnection connection)
: this()
{
this.Connection = connection;
}
public SqlConnection GetConnection()
{
return this.Connection;
}
public void SetConnection(SqlConnection con)
{
this.Connection = con;
}
public void SetConnectionString(string connectionString)
{
this.Connection.ConnectionString = connectionString;
}
}
}
www.sedodream.com
goh6613
Ok, after thinking through my reply to you last night I got the idea of where you were going.
Thanks for the great feedback and I was able to use what you were saying to get my function to work!
Thanks again!
David
shiveta
Nick, this is what I call 'trade show functionality'. Microsoft get's it to work well enough to draw oohs and ahhs from a presentation audience and then moves on to the next ooh & ahh thing. Meanwhile, after our bosses did the oohing & ahhing we're left to make the ... stuff do actual work.
It would be nice if Microsoft just once stuck with the technology long enough to make it actually user friendly.
tovarish
Ok, you have lost me a little here. You are overriding these methods for the tableadapter.
Do you mean the Dataset
I can't seem to find a place to create a partial class for the tableadapter. In this case my tableadapter for the overall dataset is tblLeaseSaleParcelsTableAdapter and it resides within my current form object.
I tried doing a branch like your example but I got lots of errors about the the class not existing.
Thoughts
Thanks for your great reply, I just don't understand enough about ADO.Net to put it into play yet!
Thanks again!
David
IvanVC
works fine for me - certainly this isn't the only way to do it, but it fits well into the kind of stuff I do
pg
Aneel
Hi,
"You are overriding these methods for the tableadapter"...
Not really overriding becaues these methods don't exist on the tableadapter. The table adapter has the Connection property, but that is not useful because it is internal. So we need to make a wrapper get/set method that is public. That's what we are doing here. The DataSet doesn't have a connection stored with it, instead it uses the TableAdpater to populate the data sets. This is why we need to change the connection information that is stored with the TableAdapter.
You could place this code in a number of places but I suggest placing it side by side with your custom DataSet class. To get to this right-click on the DataSet in Solution explorer and select View Code. This will open up the code file for the DataSet. In my case I didn't have any other code for the DataSet so it looked like:
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace Latitude.WorkFlow.DataAccess.DataSet
{
public partial class WorkFlowsDS
{
}
}
After the above code, I placed the code from the previous post. What we want to do is to extend the TableAdapters behavior with a partial class. To achieve that we place the definition of the partial class in this file. But you have to be careful because the namespace of the DataSet and the TableAdapter are different. So you need to figure out the namespace for the TableAdapter. You can get this by viewing the auto generated code for the DataSet. Do this by right-clicking on the DATA_SET_NAME.designer.cs file in Solution Explorer and selecting view code. Find the declaration of the TableAdapter and get its name and namespace. Then create your partial class in the other file. So from the above snippet the resulting file looks like:
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;
namespace Latitude.WorkFlow.DataAccess.DataSet
{
public partial class WorkFlowsDS
{
}
}
namespace Latitude.WorkFlow.DataAccess.DataSet.WorkFlowsDSTableAdapters
{
public partial class WorkFlowsTableAdapter
{
public WorkFlowsTableAdapter(string connectionString)
: this()
{
this.Connection = new SqlConnection();
this.Connection.ConnectionString = connectionString;
}
public WorkFlowsTableAdapter(SqlConnection connection)
: this()
{
this.Connection = connection;
}
public SqlConnection GetConnection()
{
return this.Connection;
}
public void SetConnection(SqlConnection con)
{
this.Connection = con;
}
public void SetConnectionString(string connectionString)
{
this.Connection.ConnectionString = connectionString;
}
}
}
Sayed Ibrahim Hashimi
www.sedodream.com