473,382 Members | 1,480 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

SqlConnection vs IDbConnection

I normally use SqlConnection, SqlDataReader, SqlCommand for most of my
queries but recently a friend of mine who is a long time java
programmer suggested I use the IDbConnection, IDbCommand interfaces
because the queries where compiled instead of just executed like on
the open SqlConnection. Is he correct and if so does it really make a
difference as far as performance/security, etc?

Thanks for any suggestions

Apr 13 '07 #1
8 14246
JoeW,

No, he isn't correct. IDbConnection, IDbCommand, and the like are just
interfaces to abstract functionality on the provider-specific
implementations (like SqlConnection, SqlDataReader, SqlCommand, etc, etc).
It doesn't imply anything.

If your command points to a stored procedure, then it is already
compiled. If you have a command that is a string that you are just passing,
and need it prepared by the underlying provider, you would call the Prepare
method on the IDbCommand implementation (this method is exposed publically
on the implementation as well, most likely). That will prepare the query
for you with the underlying provider.

Note, if your command is a stored procedure, you don't want to call
Prepare. The execution plan for the stored proc has already been generated.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JoeW" <te********@gmail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
>I normally use SqlConnection, SqlDataReader, SqlCommand for most of my
queries but recently a friend of mine who is a long time java
programmer suggested I use the IDbConnection, IDbCommand interfaces
because the queries where compiled instead of just executed like on
the open SqlConnection. Is he correct and if so does it really make a
difference as far as performance/security, etc?

Thanks for any suggestions

Apr 13 '07 #2
PS

"JoeW" <te********@gmail.comwrote in message
news:11*********************@y80g2000hsf.googlegro ups.com...
>I normally use SqlConnection, SqlDataReader, SqlCommand for most of my
queries but recently a friend of mine who is a long time java
programmer suggested I use the IDbConnection, IDbCommand interfaces
because the queries where compiled instead of just executed like on
the open SqlConnection. Is he correct and if so does it really make a
difference as far as performance/security, etc?
The reason you would code against the interface (or the abstract class
DbCommand in .Net 2.0) is so that your code could be used across multiple
data providers. In 2.0 there are factories provided to make this easier. If
you are only supporting Sql Server as a database then I see no reason to
chnage anything (YAGNI).

PS
>
Thanks for any suggestions

Apr 13 '07 #3
On Apr 13, 2:24 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
JoeW,

No, he isn't correct. IDbConnection, IDbCommand, and the like are just
interfaces to abstract functionality on the provider-specific
implementations (like SqlConnection, SqlDataReader, SqlCommand, etc, etc).
It doesn't imply anything.

If your command points to a stored procedure, then it is already
compiled. If you have a command that is a string that you are just passing,
and need it prepared by the underlying provider, you would call the Prepare
method on the IDbCommand implementation (this method is exposed publically
on the implementation as well, most likely). That will prepare the query
for you with the underlying provider.

Note, if your command is a stored procedure, you don't want to call
Prepare. The execution plan for the stored proc has already been generated.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"JoeW" <teh.sn1...@gmail.comwrote in message

news:11*********************@y80g2000hsf.googlegro ups.com...
I normally use SqlConnection, SqlDataReader, SqlCommand for most of my
queries but recently a friend of mine who is a long time java
programmer suggested I use the IDbConnection, IDbCommand interfaces
because the queries where compiled instead of just executed like on
the open SqlConnection. Is he correct and if so does it really make a
difference as far as performance/security, etc?
Thanks for any suggestions- Hide quoted text -

- Show quoted text -
Ahh, I see. So this code would be fine?

string selectQuery = "SELECT time FROM " + TABLE_PREFIX + "referrers
WHERE url = @url AND ip = @ip";

_sqlCommand = new SqlCommand(selectQuery, _sqlConnector);

_sqlCommand.Parameters.AddWithValue("@url", pUrl);
_sqlCommand.Parameters.AddWithValue("@ip", pIp);
_sqlCommand.Prepare();

Apr 13 '07 #4
JoeW,

Yep, that looks fine to me.

Just remember that you have to keep the connection open as long as you
want to use the prepared statement.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JoeW" <te********@gmail.comwrote in message
news:11**********************@w1g2000hsg.googlegro ups.com...
On Apr 13, 2:24 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>JoeW,

No, he isn't correct. IDbConnection, IDbCommand, and the like are
just
interfaces to abstract functionality on the provider-specific
implementations (like SqlConnection, SqlDataReader, SqlCommand, etc,
etc).
It doesn't imply anything.

If your command points to a stored procedure, then it is already
compiled. If you have a command that is a string that you are just
passing,
and need it prepared by the underlying provider, you would call the
Prepare
method on the IDbCommand implementation (this method is exposed
publically
on the implementation as well, most likely). That will prepare the query
for you with the underlying provider.

Note, if your command is a stored procedure, you don't want to call
Prepare. The execution plan for the stored proc has already been
generated.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"JoeW" <teh.sn1...@gmail.comwrote in message

news:11*********************@y80g2000hsf.googlegr oups.com...
>I normally use SqlConnection, SqlDataReader, SqlCommand for most of my
queries but recently a friend of mine who is a long time java
programmer suggested I use the IDbConnection, IDbCommand interfaces
because the queries where compiled instead of just executed like on
the open SqlConnection. Is he correct and if so does it really make a
difference as far as performance/security, etc?
Thanks for any suggestions- Hide quoted text -

- Show quoted text -

Ahh, I see. So this code would be fine?

string selectQuery = "SELECT time FROM " + TABLE_PREFIX + "referrers
WHERE url = @url AND ip = @ip";

_sqlCommand = new SqlCommand(selectQuery, _sqlConnector);

_sqlCommand.Parameters.AddWithValue("@url", pUrl);
_sqlCommand.Parameters.AddWithValue("@ip", pIp);
_sqlCommand.Prepare();

Apr 13 '07 #5
On Apr 13, 2:49 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
JoeW,

Yep, that looks fine to me.

Just remember that you have to keep the connection open as long as you
want to use the prepared statement.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"JoeW" <teh.sn1...@gmail.comwrote in message

news:11**********************@w1g2000hsg.googlegro ups.com...
On Apr 13, 2:24 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
JoeW,
No, he isn't correct. IDbConnection, IDbCommand, and the like are
just
interfaces to abstract functionality on the provider-specific
implementations (like SqlConnection, SqlDataReader, SqlCommand, etc,
etc).
It doesn't imply anything.
If your command points to a stored procedure, then it is already
compiled. If you have a command that is a string that you are just
passing,
and need it prepared by the underlying provider, you would call the
Prepare
method on the IDbCommand implementation (this method is exposed
publically
on the implementation as well, most likely). That will prepare the query
for you with the underlying provider.
Note, if your command is a stored procedure, you don't want to call
Prepare. The execution plan for the stored proc has already been
generated.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
"JoeW" <teh.sn1...@gmail.comwrote in message
>news:11*********************@y80g2000hsf.googlegr oups.com...
I normally use SqlConnection, SqlDataReader, SqlCommand for most of my
queries but recently a friend of mine who is a long time java
programmer suggested I use the IDbConnection, IDbCommand interfaces
because the queries where compiled instead of just executed like on
the open SqlConnection. Is he correct and if so does it really make a
difference as far as performance/security, etc?
Thanks for any suggestions- Hide quoted text -
- Show quoted text -
Ahh, I see. So this code would be fine?
string selectQuery = "SELECT time FROM " + TABLE_PREFIX + "referrers
WHERE url = @url AND ip = @ip";
_sqlCommand = new SqlCommand(selectQuery, _sqlConnector);
_sqlCommand.Parameters.AddWithValue("@url", pUrl);
_sqlCommand.Parameters.AddWithValue("@ip", pIp);
_sqlCommand.Prepare();- Hide quoted text -

- Show quoted text -
Does it matter if the connection is opened before or after the prepare
statement?

As of now I have it like this:

string selectQuery = "SELECT time FROM " + TABLE_PREFIX + "referrers
WHERE url = @url AND ip = @ip";

_sqlCommand = new SqlCommand(selectQuery, _sqlConnector);

_sqlCommand.Parameters.AddWithValue("@url", pUrl);
_sqlCommand.Parameters.AddWithValue("@ip", pIp);
_sqlCommand.Prepare();

try
{
_sqlConnector.Open();

_sqlReader = _sqlCommand.ExecuteReader();

while (_sqlReader.Read())
{
// do something
}
}

Thanks again for the help, I appreciate it

Apr 14 '07 #6
JoeW,

Well, the documentation for the Prepare method on the SqlCommand class
states that an InvalidOperationException will be thrown if you call the
Prepare method when the connection is not open, so it might not be a good
idea =)
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"JoeW" <te********@gmail.comwrote in message
news:11**********************@p77g2000hsh.googlegr oups.com...
On Apr 13, 2:49 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
>JoeW,

Yep, that looks fine to me.

Just remember that you have to keep the connection open as long as
you
want to use the prepared statement.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"JoeW" <teh.sn1...@gmail.comwrote in message

news:11**********************@w1g2000hsg.googlegr oups.com...
On Apr 13, 2:24 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:
JoeW,
> No, he isn't correct. IDbConnection, IDbCommand, and the like are
just
interfaces to abstract functionality on the provider-specific
implementations (like SqlConnection, SqlDataReader, SqlCommand, etc,
etc).
It doesn't imply anything.
> If your command points to a stored procedure, then it is already
compiled. If you have a command that is a string that you are just
passing,
and need it prepared by the underlying provider, you would call the
Prepare
method on the IDbCommand implementation (this method is exposed
publically
on the implementation as well, most likely). That will prepare the
query
for you with the underlying provider.
> Note, if your command is a stored procedure, you don't want to
call
Prepare. The execution plan for the stored proc has already been
generated.
> Hope this helps.
>--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com
>"JoeW" <teh.sn1...@gmail.comwrote in message
>>news:11*********************@y80g2000hsf.googleg roups.com...
>I normally use SqlConnection, SqlDataReader, SqlCommand for most of
my
queries but recently a friend of mine who is a long time java
programmer suggested I use the IDbConnection, IDbCommand interfaces
because the queries where compiled instead of just executed like on
the open SqlConnection. Is he correct and if so does it really make
a
difference as far as performance/security, etc?
Thanks for any suggestions- Hide quoted text -
>- Show quoted text -
Ahh, I see. So this code would be fine?
string selectQuery = "SELECT time FROM " + TABLE_PREFIX + "referrers
WHERE url = @url AND ip = @ip";
_sqlCommand = new SqlCommand(selectQuery, _sqlConnector);
_sqlCommand.Parameters.AddWithValue("@url", pUrl);
_sqlCommand.Parameters.AddWithValue("@ip", pIp);
_sqlCommand.Prepare();- Hide quoted text -

- Show quoted text -

Does it matter if the connection is opened before or after the prepare
statement?

As of now I have it like this:

string selectQuery = "SELECT time FROM " + TABLE_PREFIX + "referrers
WHERE url = @url AND ip = @ip";

_sqlCommand = new SqlCommand(selectQuery, _sqlConnector);

_sqlCommand.Parameters.AddWithValue("@url", pUrl);
_sqlCommand.Parameters.AddWithValue("@ip", pIp);
_sqlCommand.Prepare();

try
{
_sqlConnector.Open();

_sqlReader = _sqlCommand.ExecuteReader();

while (_sqlReader.Read())
{
// do something
}
}

Thanks again for the help, I appreciate it

Apr 14 '07 #7
JoeW wrote:
I normally use SqlConnection, SqlDataReader, SqlCommand for most of my
queries but recently a friend of mine who is a long time java
programmer suggested I use the IDbConnection, IDbCommand interfaces
because the queries where compiled instead of just executed like on
the open SqlConnection. Is he correct and if so does it really make a
difference as far as performance/security, etc?
No difference for performance or security.

Big difference for portability between databases.

If you use only IDb interfaces and the .NET 2.0+
DbProvideFactory then you code is database independent.

It can be a very good benefit.

Obviously you also loose the capability to use
database specific features.

Code snippet:

private static void test(string provider, string constr)
{
DbProviderFactory dbf = DbProviderFactories.GetFactory(provider);
IDbConnection con = dbf.CreateConnection();
con.ConnectionString = constr;
con.Open();
IDbCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM T1";
IDataReader rdr = cmd.ExecuteReader();
while(rdr.Read()) {
int f1 = (int)rdr[0];
string f2 = (string)rdr[1];
Console.WriteLine(f1 + " " + f2);
}
con.Close();
}

It is not my impression that the usage of IDb interfaces
is that big.

Arne
Apr 14 '07 #8
On Apr 14, 12:29 pm, Arne Vajhøj <a...@vajhoej.dkwrote:
JoeW wrote:
I normally use SqlConnection, SqlDataReader, SqlCommand for most of my
queries but recently a friend of mine who is a long time java
programmer suggested I use the IDbConnection, IDbCommand interfaces
because the queries where compiled instead of just executed like on
the open SqlConnection. Is he correct and if so does it really make a
difference as far as performance/security, etc?

No difference for performance or security.

Big difference for portability between databases.

If you use only IDb interfaces and the .NET 2.0+
DbProvideFactory then you code is database independent.

It can be a very good benefit.

Obviously you also loose the capability to use
database specific features.

Code snippet:

private static void test(string provider, string constr)
{
DbProviderFactory dbf = DbProviderFactories.GetFactory(provider);
IDbConnection con = dbf.CreateConnection();
con.ConnectionString = constr;
con.Open();
IDbCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT * FROM T1";
IDataReader rdr = cmd.ExecuteReader();
while(rdr.Read()) {
int f1 = (int)rdr[0];
string f2 = (string)rdr[1];
Console.WriteLine(f1 + " " + f2);
}
con.Close();
}

It is not my impression that the usage of IDb interfaces
is that big.

Arne
Great advice guys, thanks again for your help. That helped me out a
lot.

Apr 14 '07 #9

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

Similar topics

7
by: cody | last post by:
What is the difference between OleDBConnection and SqlConnection? Or better expressed, what is OleDB? -- cody www.deutronium.de.vu || www.deutronium.tk
3
by: faktujaa | last post by:
Hi All, A small confusion. I have defined a connection class that has System.Data.IDbConnection as a member variable and implements IDisposable interface. I have implemented Dispose method to call...
1
by: pei_world | last post by:
I am create an generic class for Database access using these interfaces. I run my program with the idea of IDbConnection and IDbDataAdatper without any problem in single table. But since...
15
by: Sam Sungshik Kong | last post by:
Hello! A disposable object's Dispose() method can be called either explicitly by the programmer or implicitly during finalization. If you call Dispose, the unmanaged resources are released...
2
by: Carlos | last post by:
I would like to create a component that inherits from sqlconnection, but it is sealed. What ancestor can I inherit from? I've seen this: public sealed class SqlConnection : Component,...
1
by: Claudiu Tescu | last post by:
I make an project in ASP that use SQL Server that is on the same computer at compilation time I have the problem : Login failed for user 'NOSTRU\IUSR_NOSTRU'. Description: An unhandled exception...
1
by: jinfeng_Wang | last post by:
hi, I have a question about the difference between SqlConnection.IDisposable.Dispose() and SqlConnection.Dispose(). Both of them realize the function of releasing the connection to the...
0
by: Ryan Liu | last post by:
Hi, I downloaded the latest version of mysql 5.0 and its .NET driver, I copied the sample code from mysql 5.0 manual to do use prepared statement in C#. But indeed I got a run time error say...
3
by: Peter Kirk | last post by:
Hi do I need to explicitly close IDbConnection and IDbCommand objects? Or are they disposed of when the method they are used in ends? Normally I use a construct like: using (IDbConnection...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.