several windowsforms

G

Guest

Hi!

I'm building a windowsapplication with several windowsforms including a
MDI-parent with MDIchildren. They are all belonging to the same namespace.
Each windowsform has its own connectionstring connecting up to one
SQL-server. This is created every time I configure a sqlDataAdapter if there
isn't a connectionstring from before.
It works fine for my purpose, but I find it bothersome when I decide to
take the application home and test it to a local SQL-server. Then I need to
redirect each connectionstring.

Is it possible for me to create one public instance of a connectionstring
for every
windowsform? If so... How??? And how do I bind my sqlDataAdapters when I
drag and drop them from toolbox into my form?

Appreciate every help!

Hans
 
D

Dmitriy Lapshin [C# / .NET MVP]

Hi Hans,

Actually, you should create a data access tier where all data access logic
would be encapsulated. It could be as simple as a single class designed
according to the Singleton design pattern to ensure only one instance of the
class. This class should expose methods required by the forms to read and
update the data. Thus, you will have a single point where the connection
string is defined, and it will be very easy to change it.
 
A

Alx Sharp

Hi...

I'm not really sure if I understood what you're trying to do, but any way,
here it goes...

You can create a single place for storing some application settings such as
connection strings, so let's create a class named AppConfig (or any other
name you choose):

public class AppConfig
{
public static string SqlConnectionString
{
get
{
// Retrieve the connection string from any data store
return ""; // A real connection string must be returned
}
set
{
// Save the new connection string in your data store
}
}
}

Notice that you can declare properties and any member of the AppConfig class
as
static members since you don't need an instance to access your settings.

Now let's examine what happens in your Form.
First, in the constructor you can see a call to the InitializeComponent
method, wich is generated by the Windows Forms Designer.
When you add components to your form, such as SqlConnection or
SqlDataAdapter, the Windows Forms Designer adds some code to the body of the
InitializeComponent method.
The code generated for a SqlConnection object includes the assignment of the
ConnectionString property
based on your desing-time conditions, for example if you write your
application with a server named "SERVER1", that machine name will be stored
in the connection string.
You may think you have to know the final server name in design-time and even
to have a connection to it available, but you haven't.
What you can do is something like this:

public MyForm() // Form constructor
{
InitializeComponent(); // Line added by the Windows Forms Designer
mySqlConnection.ConnectionString = AppConfig.SqlConnectionString; //
This line will override whatever the InitializeComponent method has set

// Writing your code this way, you can even test your application with
your local machine and let the Windows Forms Designer to set the server name
as "localhost"
// and while you are developing your application set the sever name to
"localhost" in your AppConfig.SqlConnectionString property, and when you
want to deploy your application
// you just have to change a small part of your code in the
AppConfig.SqlConnectionString property
}

As you can see in the definition of AppConfig.SqlConnectionString property,
you don't have to write the connection string as a string literal in your
code, because you can retrieve it from any data store for application
settings.

By doing so, you just have to write a sentence like
mySqlConnection.ConnectionString = AppConfig.SqlConnectionString; in any
Form constructor after the InitializeComponent call in your project.

Besides, you can provide your end-user with features like Settings Dialogs
and he or she will be able to set that options as desired.

A simple way to save and restore the current state of an object (all public
properties and fields) is Serialization. You can find helpful information in
the MSDN documentation.

The last thing I want to tell you is about the answer to your message sent
by Dmitry.
The better way of developing applications is based on a multi-tier model,
where you separate your whole application in modules like the Data Access
Layer, Business Rules Layer and the Presentation Layer,so the Windows Forms
never "talks" directly to the Data Access components, because the
Presentation Layer communicates with Business Layer and tthe Business Layer
interacts with the Data Access Layer.
This is the better aproach, but you have to study it in much more detail.

I hope it'll be helpful for you.

You can contact me via e-mail ([email protected]) or via the news groups.

Bye...

"Hans <[email protected]>" <[email protected]>
escribió en el mensaje
 
G

Guest

Hi Alx!

I really appreciate your comments. I have stored your hotmail for later use
:)
Thanks.


Hans.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top