473,387 Members | 1,812 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,387 software developers and data experts.

SQL command parameters Advantages of?

Hi all, a quick ADO.NET question in regards to the command object.
What are the advantages (if any) of specifying command parameters when
executing a stored procedure over just calling the stored procedure via
dynamic SQL?

// Pseudo code below using dynamic SQL
private void InsertPerson(string sName)
{
SqlCommand myCommand = new SqlCommand(........
// Build the dynamic SQL
myCommand.CommandText = "EXEC spAddPerson '" + sName + "'";
// Execute the command against the database
myCommand.ExecuteNonQuery();
}

// Pseudo code below using SQL Parameters
private void InsertPerson(string sName)
{
SqlCommand myCommand = new SqlCommand(........
myCommand.Parameters.Add("@Name", SqlDbType.VarChar);
myCommand.Parameters["@Name"].Value = sName;
myCommand.ExecuteNonQuery();
}

Thanks in advance
Mark
Nov 16 '05 #1
2 3429
Mark,

SQL parameters offer *potentially* many advantages. When presenting the SQL
text to the ADO.NET provider, the text is delimited by the double quote, and
text values within the string are delimited by the single quote. This setup
precludes the use of either of these characters in the value for any fields
in the string - notwithstanding a lot of extra work to escape the sequence
to allow these delimeter character values in as literal characters.

Second - most modern SQL processing engines will parse the SQL statement and
generate an execution plan and cache the two intermediate results - the
parsed form of the SQL statement and the execution plan. If you use bind
variables in the statement, the SQL engine can resue SQL statements that
differ only by the values of fields in the SQL statement. There is a
substantial performance boost in systems with a large number of
transaction - for run-once SQL statements, the advantages are not quite to
apparent.

regards
roy fine
"Mark" <ma*******@n0Sp8mTAIRAWHITIdotAC.NZ> wrote in message
news:4q******************@news.xtra.co.nz...
Hi all, a quick ADO.NET question in regards to the command object.
What are the advantages (if any) of specifying command parameters when
executing a stored procedure over just calling the stored procedure via
dynamic SQL?

// Pseudo code below using dynamic SQL
private void InsertPerson(string sName)
{
SqlCommand myCommand = new SqlCommand(........
// Build the dynamic SQL
myCommand.CommandText = "EXEC spAddPerson '" + sName + "'";
// Execute the command against the database
myCommand.ExecuteNonQuery();
}

// Pseudo code below using SQL Parameters
private void InsertPerson(string sName)
{
SqlCommand myCommand = new SqlCommand(........
myCommand.Parameters.Add("@Name", SqlDbType.VarChar);
myCommand.Parameters["@Name"].Value = sName;
myCommand.ExecuteNonQuery();
}

Thanks in advance
Mark

Nov 16 '05 #2
Hi Roy, thankyou for your help.
Regards
Mark

"Roy Fine" <rl****@twt.obfuscate.net> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
Mark,

SQL parameters offer *potentially* many advantages. When presenting the SQL text to the ADO.NET provider, the text is delimited by the double quote, and text values within the string are delimited by the single quote. This setup precludes the use of either of these characters in the value for any fields in the string - notwithstanding a lot of extra work to escape the sequence
to allow these delimeter character values in as literal characters.

Second - most modern SQL processing engines will parse the SQL statement and generate an execution plan and cache the two intermediate results - the
parsed form of the SQL statement and the execution plan. If you use bind
variables in the statement, the SQL engine can resue SQL statements that
differ only by the values of fields in the SQL statement. There is a
substantial performance boost in systems with a large number of
transaction - for run-once SQL statements, the advantages are not quite to
apparent.

regards
roy fine
"Mark" <ma*******@n0Sp8mTAIRAWHITIdotAC.NZ> wrote in message
news:4q******************@news.xtra.co.nz...
Hi all, a quick ADO.NET question in regards to the command object.
What are the advantages (if any) of specifying command parameters when
executing a stored procedure over just calling the stored procedure via
dynamic SQL?

// Pseudo code below using dynamic SQL
private void InsertPerson(string sName)
{
SqlCommand myCommand = new SqlCommand(........
// Build the dynamic SQL
myCommand.CommandText = "EXEC spAddPerson '" + sName + "'";
// Execute the command against the database
myCommand.ExecuteNonQuery();
}

// Pseudo code below using SQL Parameters
private void InsertPerson(string sName)
{
SqlCommand myCommand = new SqlCommand(........
myCommand.Parameters.Add("@Name", SqlDbType.VarChar);
myCommand.Parameters["@Name"].Value = sName;
myCommand.ExecuteNonQuery();
}

Thanks in advance
Mark


Nov 16 '05 #3

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

Similar topics

5
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm...
5
by: Gene | last post by:
What can I do if I want to get the result using the sql command? for example, the select command is "select Name from Employee where StaffID=10" How to get the "Name"??? dim Name as string and...
12
by: Perre Van Wilrijk | last post by:
Hi there, When I started using VB6, I used to write classes with properties and functions as following ... Private lngf1 As Long Private strf2 As String Public Property Get f1() As Long...
9
by: red floyd | last post by:
Anybody have any preferences as to declaring primitive type parameter as const, if the function body doesn't change it? e.g.: int f(int x) { return x+2; }
9
by: orenr | last post by:
Hi We have a web site for 100 users using SQL Server. In our DAL all the selections when we need to pass parameters are using the SqlCommand and they are something like: SqlCommand com = new...
5
by: mabond | last post by:
Hi VB.NET 2005 Express edition Microsoft Access 2000 (SP-3) Having trouble writing an "insert into" command for a Microsoft table I'm accessing through oledb. I've tried to follow the same...
2
by: explode | last post by:
I made nova oledbdataadapter select update insert and delete command and connection veza. dataset is Studenti1data, I made it by the new data source wizard,and made datagridview and bindingsource...
3
by: iKiLL | last post by:
Hi all I am having problems getting my SqlCeDataAdapter to Update the SQL Mobile Data base. i am using C# CF2. I have tried this a number of different ways. Starting with the command builder...
3
by: Sagar | last post by:
Hi. I am working on a project to migrate a web application from 1.1 to 2.0 Within in the DAL of the application, there is a call to below function that builds a command object for later use. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
jinu1996
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...

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.