473,513 Members | 2,461 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Correct usage of connections/data adapters?


Hey all -

I've found a lot of 'snippet' examples of hitting a sqlserver db from a
csharp windows app, but I want to make sure I understand the proper usage.

In the examples, they usually create the sqlconnection and pass the
connection string in the form, and then open it and create a sqlDataAdapter
to load a widget... immediately following the sqlconnection is closed.

For what I want to accomplish, I'm going to have many forms coming off of a
MDI control window. Would the following be ok?

1. Create a static sqlconnection in a static dbaccess class
2. Open the connection, create the dataadapter/dataset to load the widget,
close the connection.
3. Repear #2 for each data bound widget on each form...
Do I need to create a different dataadapter for each widget that I want to
populate from the db?

Do I need to close the connection after each hit, or can I just keep it
open for the entire time they have the app running?

Just trying to get a handle on how the small examples I've found apply to a
larger application...

Thanks!
May 8 '06 #1
5 1410
As a general rule of them, you'll want to isolate your
database code outside the ui layer. Typically,
you create your sql objects, use them, close them,
and then destroy them immediately.

http://www.eggheadcafe.com/articles/...plications.asp

--
Robbe Morris - 2004-2006 Microsoft MVP C#
Earn money answering .NET questions
http://www.eggheadcafe.com/forums/merit.asp

"Alex" <78********@null.com> wrote in message
news:Xn*******************************@130.81.64.1 96...

Hey all -

I've found a lot of 'snippet' examples of hitting a sqlserver db from a
csharp windows app, but I want to make sure I understand the proper usage.

In the examples, they usually create the sqlconnection and pass the
connection string in the form, and then open it and create a
sqlDataAdapter
to load a widget... immediately following the sqlconnection is closed.

For what I want to accomplish, I'm going to have many forms coming off of
a
MDI control window. Would the following be ok?

1. Create a static sqlconnection in a static dbaccess class
2. Open the connection, create the dataadapter/dataset to load the widget,
close the connection.
3. Repear #2 for each data bound widget on each form...
Do I need to create a different dataadapter for each widget that I want to
populate from the db?

Do I need to close the connection after each hit, or can I just keep it
open for the entire time they have the app running?

Just trying to get a handle on how the small examples I've found apply to
a
larger application...

Thanks!

May 9 '06 #2
The .Net Data model relies heavily on disconnected data. It uses Connection
Pooling to optimize the most expensive aspect of database operations, which
is opening a Connection. Your best bet is to follow along with the plan.
Load your data, and then re-connect only to change it, or to synchronize
multiple users.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"Alex" <78********@null.com> wrote in message
news:Xn*******************************@130.81.64.1 96...

Hey all -

I've found a lot of 'snippet' examples of hitting a sqlserver db from a
csharp windows app, but I want to make sure I understand the proper usage.

In the examples, they usually create the sqlconnection and pass the
connection string in the form, and then open it and create a
sqlDataAdapter
to load a widget... immediately following the sqlconnection is closed.

For what I want to accomplish, I'm going to have many forms coming off of
a
MDI control window. Would the following be ok?

1. Create a static sqlconnection in a static dbaccess class
2. Open the connection, create the dataadapter/dataset to load the widget,
close the connection.
3. Repear #2 for each data bound widget on each form...
Do I need to create a different dataadapter for each widget that I want to
populate from the db?

Do I need to close the connection after each hit, or can I just keep it
open for the entire time they have the app running?

Just trying to get a handle on how the small examples I've found apply to
a
larger application...

Thanks!

May 9 '06 #3
Thanks for the replies... I'm making my way through the linked articles,
but I wanted to follow up on this about destroying the connection.

Are you saying that if I have, say 10 listboxes on a form, each of those
listboxes should create it's own 'new' sqlConnection object (passing the
connections string, etc), set up a data reader, grab a record set and then
dispose of the sqlConnection... 10 times - 10 sqlConnection .Open()'s?

Seems like it's really redundant to do it that way, but I know it'll be
more difficult later if I don't 'go with the flow'... if that's the flow.

Thx!
"Robbe Morris [C# MVP]" <in**@eggheadcafe.com> wrote in
news:eL**************@TK2MSFTNGP05.phx.gbl:
As a general rule of them, you'll want to isolate your
database code outside the ui layer. Typically,
you create your sql objects, use them, close them,
and then destroy them immediately.

http://www.eggheadcafe.com/articles/...object_oriente
d_applications.asp


May 10 '06 #4
connection pooling large removes the concerns you have.

--
Robbe Morris - 2004-2006 Microsoft MVP C#
Earn money answering .NET questions
http://www.eggheadcafe.com/forums/merit.asp

"Alex" <78********@null.com> wrote in message
news:Xn****************************@130.81.64.196. ..
Thanks for the replies... I'm making my way through the linked articles,
but I wanted to follow up on this about destroying the connection.

Are you saying that if I have, say 10 listboxes on a form, each of those
listboxes should create it's own 'new' sqlConnection object (passing the
connections string, etc), set up a data reader, grab a record set and then
dispose of the sqlConnection... 10 times - 10 sqlConnection .Open()'s?

Seems like it's really redundant to do it that way, but I know it'll be
more difficult later if I don't 'go with the flow'... if that's the flow.

Thx!
"Robbe Morris [C# MVP]" <in**@eggheadcafe.com> wrote in
news:eL**************@TK2MSFTNGP05.phx.gbl:
As a general rule of them, you'll want to isolate your
database code outside the ui layer. Typically,
you create your sql objects, use them, close them,
and then destroy them immediately.

http://www.eggheadcafe.com/articles/...object_oriente
d_applications.asp

May 10 '06 #5
JT
For the most part, I agree with Robbe, but if your code is going to
look like:
create connection
create adapter
open connection
get data
close connection
populate listbox
....
repeat 10 times

Then I believe it would be fine to substitute "get data" with "get all
data for all 10 listboxes" and just go through it once. The other
alternative is to write a function like GetData(ListBoxDataReceptacle,
QueryString) and just make 10 calls to this function with the
appropriate object and query string pairs and let it handle the
connection and adapter generically.

May 10 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1541
by: Jim | last post by:
I'm writing an Invoicing Windows app but I'm writing it to make the code as easy to maintain as possible. Basically, to get any records from my DB, I use two classes: one that sets up the SQL...
8
21896
by: Grant Richard | last post by:
Using the TcpListener and TcpClient I created a program that just sends and receives a short string - over and over again. The program is fine until it gets to around 1500 to 1800 messages. At...
5
2436
by: amanatio | last post by:
I have a huge form with many data bound controls on it and 34 tables in database (and of course 34 data adapters and 34 datasets). The form is extremely slow to design (huge delay when I go to code...
5
2436
by: Rick | last post by:
The data adapter wizard allows you to add more than one table, but that doesn't seem to work right when setting up a dataset. Some of the documentation I have read states that only one table...
2
856
by: Jason L James | last post by:
Hi All, I used the data connections wizard to automatically create the connections, data adapters and then data set to contain the date from my database. The typed dataset that is created is...
2
922
by: Dave Cullen | last post by:
Can someone please explain the relationship between data adapters, connections, and datasets? Why would an application have one adapter and multiple data connection objects? Does each form that...
0
1173
by: dimpy | last post by:
Hi everyone, I am using some big size data adapters to fill datasets to be sent to a web service. Now every time i get an error in datatype or constraints violations while filling the dataset, I...
2
1243
by: Paul Craig | last post by:
Hi everyone, I have recently upgraded several of my projects from Visual Studio 2003 to 2005 and everything went across quite smoothly. The main problem that I am having is I have used SQL Data...
5
1798
by: =?Utf-8?B?VGhvbWFzWg==?= | last post by:
Hi, I got a project coming that requires my app to connect together 3 or more computers by Wireless network without a WLAN router available. I believe it could be done with some kind of software...
0
7384
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
802
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.