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 8 14419
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.co m
"JoeW" <te********@gma il.comwrote in message
news:11******** *************@y 80g2000hsf.goog legroups.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
"JoeW" <te********@gma il.comwrote in message
news:11******** *************@y 80g2000hsf.goog legroups.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
On Apr 13, 2:24 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om
"JoeW" <teh.sn1...@gma il.comwrote in message
news:11******** *************@y 80g2000hsf.goog legroups.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(sele ctQuery, _sqlConnector);
_sqlCommand.Par ameters.AddWith Value("@url", pUrl);
_sqlCommand.Par ameters.AddWith Value("@ip", pIp);
_sqlCommand.Pre pare();
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.co m
"JoeW" <te********@gma il.comwrote in message
news:11******** **************@ w1g2000hsg.goog legroups.com...
On Apr 13, 2:24 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote:
>JoeW,
No, he isn't correct. IDbConnection, IDbCommand, and the like are just interfaces to abstract functionality on the provider-specific implementation s (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.c om
"JoeW" <teh.sn1...@gma il.comwrote in message
news:11******* **************@ y80g2000hsf.goo glegroups.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(sele ctQuery, _sqlConnector);
_sqlCommand.Par ameters.AddWith Value("@url", pUrl);
_sqlCommand.Par ameters.AddWith Value("@ip", pIp);
_sqlCommand.Pre pare();
On Apr 13, 2:49 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om
"JoeW" <teh.sn1...@gma il.comwrote in message
news:11******** **************@ w1g2000hsg.goog legroups.com...
On Apr 13, 2:24 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om
"JoeW" <teh.sn1...@gma il.comwrote in message
>news:11******* **************@ y80g2000hsf.goo glegroups.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(sele ctQuery, _sqlConnector);
_sqlCommand.Par ameters.AddWith Value("@url", pUrl);
_sqlCommand.Par ameters.AddWith Value("@ip", pIp);
_sqlCommand.Pre pare();- 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(sele ctQuery, _sqlConnector);
_sqlCommand.Par ameters.AddWith Value("@url", pUrl);
_sqlCommand.Par ameters.AddWith Value("@ip", pIp);
_sqlCommand.Pre pare();
try
{
_sqlConnector.O pen();
_sqlReader = _sqlCommand.Exe cuteReader();
while (_sqlReader.Rea d())
{
// do something
}
}
Thanks again for the help, I appreciate it
JoeW,
Well, the documentation for the Prepare method on the SqlCommand class
states that an InvalidOperatio nException 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.co m
"JoeW" <te********@gma il.comwrote in message
news:11******** **************@ p77g2000hsh.goo glegroups.com.. .
On Apr 13, 2:49 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.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.c om
"JoeW" <teh.sn1...@gma il.comwrote in message
news:11******* *************** @w1g2000hsg.goo glegroups.com.. .
On Apr 13, 2:24 pm, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guar d.caspershouse. comwrote: JoeW,
> No, he isn't correct. IDbConnection, IDbCommand, and the like are just interfaces to abstract functionality on the provider-specific implementation s (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.c om
>"JoeW" <teh.sn1...@gma il.comwrote in message
>>news:11****** *************** @y80g2000hsf.go oglegroups.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(sele ctQuery, _sqlConnector);
_sqlCommand.Par ameters.AddWith Value("@url", pUrl);
_sqlCommand.Par ameters.AddWith Value("@ip", pIp);
_sqlCommand.Pre pare();- 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(sele ctQuery, _sqlConnector);
_sqlCommand.Par ameters.AddWith Value("@url", pUrl);
_sqlCommand.Par ameters.AddWith Value("@ip", pIp);
_sqlCommand.Pre pare();
try
{
_sqlConnector.O pen();
_sqlReader = _sqlCommand.Exe cuteReader();
while (_sqlReader.Rea d())
{
// do something
}
}
Thanks again for the help, I appreciate it
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+
DbProvideFactor y 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)
{
DbProviderFacto ry dbf = DbProviderFacto ries.GetFactory (provider);
IDbConnection con = dbf.CreateConne ction();
con.ConnectionS tring = constr;
con.Open();
IDbCommand cmd = con.CreateComma nd();
cmd.CommandText = "SELECT * FROM T1";
IDataReader rdr = cmd.ExecuteRead er();
while(rdr.Read( )) {
int f1 = (int)rdr[0];
string f2 = (string)rdr[1];
Console.WriteLi ne(f1 + " " + f2);
}
con.Close();
}
It is not my impression that the usage of IDb interfaces
is that big.
Arne
On Apr 14, 12:29 pm, Arne Vajhøj <a...@vajhoej.d kwrote:
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+
DbProvideFactor y 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)
{
DbProviderFacto ry dbf = DbProviderFacto ries.GetFactory (provider);
IDbConnection con = dbf.CreateConne ction();
con.ConnectionS tring = constr;
con.Open();
IDbCommand cmd = con.CreateComma nd();
cmd.CommandText = "SELECT * FROM T1";
IDataReader rdr = cmd.ExecuteRead er();
while(rdr.Read( )) {
int f1 = (int)rdr[0];
string f2 = (string)rdr[1];
Console.WriteLi ne(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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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 Dispose method of
IDbConnection. This Dispose method is called from a destructor. In the
dispose method, i also call GC.SuppressFinalize(this) so as to avoid
finalizer. All this was OK untill i came across one article that says - not
to call...
|
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 IDbDataAdatper's Fill method only takes DataSet as parameter, so I
can define my table name in the dataset for later use. at the moment I am
using id number of table in the dataset, and when I try to call my second
table it fail. I think system overwrite...
|
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 earlier.
Thus, if you think the unmanaged resources are important, you call Dispose
explicitly.
My question is what's the criteria to decide the unmanaged resources are
important.
|
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, IDbConnection,
ICloneable
So, I've tried:
| |
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 occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.
Exception Details: System.Data.SqlClient.SqlException: Login failed for user...
|
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
ConnectionPool? Do they have the same effection source code? If they
are different, who can tell me the differences? If they are same, why
MS gives the SqlConnection.IDisposable.Dispose, but only
SqlConnection.Dispose() method?
In the MSDN, there are...
|
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 that "#42000You have an error in your
SQL syntax; ..."
I tried variuos format and I get regular statement(with parameters) and
stored procedure works fine in my c# code, but just not for cmd.Prepare().
|
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 conn = GetConnection()) // where "GetConnection gets a
database connection
{
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |