Re: SQL Parameterized Command versus Custom String
- From: "Gregory A. Beamer" <NoSpamMgbworld@xxxxxxxxxxxxxxxxxx>
- Date: Tue, 25 Aug 2009 10:19:01 -0700
"Sam" <samf@xxxxxxxxxx> wrote in
news:u7k0jsSJKHA.1376@xxxxxxxxxxxxxxxxxxxx:
Hi Greg,@e34g2000vbm.googlegroups.com
I am only new to C#
I would use the EXEC String
What do you mean by
Attaching the params is so much cleanerDo you have a eg or link to eg
TIA
Samf
"Gregory A. Beamer" <NoSpamMgbworld@xxxxxxxxxxxxxxxxxx> wrote in
message news:Xns9C71998B0D0E2gbworld@xxxxxxxxxxxxxxxx
"henry.lee.jr@xxxxxxxxx" <henry.lee.jr@xxxxxxxxx> wrote in
news:3b006db4-0636-46eb-aefc-fc3a3c2ec369
:
Is there a way to see exactly what SQL the parameterized
command object is trying to execute so you can test it against SQL
Server?
SQL Profiler? That is the tool I would use in development. Just run
and watch the profiler spit out the incorrect SQL. ;-)
I don't like the way you are calling sprocs, however. Attaching the
params is so much cleaner than attempting to make an "exec" string.
Peace and Grace,
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com
*******************************************
| Think outside the box! |
*******************************************
string connString = ConfigurationManager
.ConnectionStrings["myConn"].ConnectionString;
string sql = "{stored proc name}";
SqlConnection connection = new SqlConnection(connString);
SqlCommand command = new SqlCommand(conn, sql);
command.CommandType = CommandType.StoredProcedure;
//Add parameters without EXEC (option 1)
command.AddWithValue("@paramname1", param1);
//Option 2 (short verison, consult help file for more
// explicit versions)
SqlParameter param = new SqlParameter("@paramname");
param.Vlaue = param2;
Calling in this way avoids the possibility of SQL injection and is much
smoother.
As for running a command, it is generally like this:
try
{
connection.Open();
//Run command here, one option is DataSet
adapter.Fill(myDataSet);
}
finally
{
connection.Dispose();
}
Peace and Grace,
--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA
Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com
*******************************************
| Think outside the box! |
*******************************************
.
- References:
- SQL Parameterized Command versus Custom String
- From: henry.lee.jr@xxxxxxxxx
- Re: SQL Parameterized Command versus Custom String
- From: Gregory A. Beamer
- Re: SQL Parameterized Command versus Custom String
- From: Sam
- SQL Parameterized Command versus Custom String
- Prev by Date: RE: Extracting data from XML
- Next by Date: Re: Extracting data from XML
- Previous by thread: Re: SQL Parameterized Command versus Custom String
- Next by thread: WCF Architecture Advice
- Index(es):
Relevant Pages
|