is it a good idea to use SQL connection?

M

Mojtaba Faridzad

Hi,

(newbie in C#) is it a good idea to use SQL Connection, SQL Data Adapter,...
in C#? when I am design a form, I can use SQL Connection and set the
properties to connect to a database. I guess after unloading the form, this
connection will be closed. but as I know, it's better to open a connection
when we need it and after using, we should close it. I can do it without
using SQL Connection component (by using SqlConnection class). now which one
is better? using SQL Connection is very easy but I'd like to have the best
performance.

thanks,
Mojtaba
 
M

Miha Markic [MVP C#]

Hi Mojtaba,

In designer you only design the connection - it won't open it for you thus
it won't make much difference.
Though I preffer to create it by code right before I need it and dispose it
as soon as possible.
 
M

Mark Broadbent

also bear in mind Mojtaba that the SqlAdapter will dynamically open and
close the connection for you, so you should always let it do so (as opposed
to manually opening and closing the connection).

Miha Markic said:
Hi Mojtaba,

In designer you only design the connection - it won't open it for you thus
it won't make much difference.
Though I preffer to create it by code right before I need it and dispose it
as soon as possible.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Mojtaba Faridzad said:
Hi,

(newbie in C#) is it a good idea to use SQL Connection, SQL Data
Adapter,... in C#? when I am design a form, I can use SQL Connection and
set the properties to connect to a database. I guess after unloading the
form, this connection will be closed. but as I know, it's better to open a
connection when we need it and after using, we should close it. I can do
it without using SQL Connection component (by using SqlConnection class).
now which one is better? using SQL Connection is very easy but I'd like to
have the best performance.

thanks,
Mojtaba
 
M

Miha Markic [MVP C#]

Hi Mark,

Mark Broadbent said:
also bear in mind Mojtaba that the SqlAdapter will dynamically open and
close the connection for you, so you should always let it do so (as
opposed
to manually opening and closing the connection).

I tend to disagree.
You should manually open the connections if you have more than one operation
to do within the same connection.
Not to speak about Update when it is supposed to be a part of transaction
:)
 
M

Mojtaba Faridzad

thanks Mark!

so a combination of SqlConnection, SqlDataAdapter, SqlCommand, and DataSet
(by drag and drop it from toolbox) has the same performance as open them in
source code? in source code, I use a "try" block to open the connection and
fill the DataAdapter and close the connection.

the reason of asking this question is that I am reading "Prof. C# 2nd
Edition" which teaches programming method of connection to a database. but I
downloaded a sample program which opens and works with data just by toolbox
components. I confused a little bit which method it's better.

thanks again

Mark Broadbent said:
also bear in mind Mojtaba that the SqlAdapter will dynamically open and
close the connection for you, so you should always let it do so (as
opposed
to manually opening and closing the connection).

Miha Markic said:
Hi Mojtaba,

In designer you only design the connection - it won't open it for you
thus
it won't make much difference.
Though I preffer to create it by code right before I need it and dispose it
as soon as possible.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Mojtaba Faridzad said:
Hi,

(newbie in C#) is it a good idea to use SQL Connection, SQL Data
Adapter,... in C#? when I am design a form, I can use SQL Connection
and
set the properties to connect to a database. I guess after unloading
the
form, this connection will be closed. but as I know, it's better to
open a
connection when we need it and after using, we should close it. I can
do
it without using SQL Connection component (by using SqlConnection class).
now which one is better? using SQL Connection is very easy but I'd like to
have the best performance.

thanks,
Mojtaba
 
M

Mojtaba Faridzad

thanks Miha. I forgot to mention it on my previous post :)

just I wanted to let you know that I am using MySQLDirect connection (not
SqlConnection) but both they have the same components.

Mojtaba

Mark Broadbent said:
also bear in mind Mojtaba that the SqlAdapter will dynamically open and
close the connection for you, so you should always let it do so (as
opposed
to manually opening and closing the connection).

Miha Markic said:
Hi Mojtaba,

In designer you only design the connection - it won't open it for you
thus
it won't make much difference.
Though I preffer to create it by code right before I need it and dispose it
as soon as possible.

--
Miha Markic [MVP C#] - RightHand .NET consulting & development
miha at rthand com
www.rthand.com

Mojtaba Faridzad said:
Hi,

(newbie in C#) is it a good idea to use SQL Connection, SQL Data
Adapter,... in C#? when I am design a form, I can use SQL Connection
and
set the properties to connect to a database. I guess after unloading
the
form, this connection will be closed. but as I know, it's better to
open a
connection when we need it and after using, we should close it. I can
do
it without using SQL Connection component (by using SqlConnection class).
now which one is better? using SQL Connection is very easy but I'd like to
have the best performance.

thanks,
Mojtaba
 

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