Cor,
I don't understand your concerns or your wish to challenge this. First and
formost I believe we both agree that Dynamic SQL is not the way to go.
Having said that, I consolidated guidelines for doing so.
Cor Ligthert said:
Amdrit,
"> However, if you feel you must build a string, and there are reasons to
do
Goes certainly wrong in countries as the Netherlands or Belgium where
users use a workstation setted in the Dutch or French mode while the
Server has almost forever US settings.
Formatting datetime values to strings has to be done since you cannot pass a
date in a SQL statement without doing so. Since you only need to satisfy
the datetime settings of the target SQL server, locality is irrelevant. How
is this an issue? For all I care, pass in an EPOCH date value and let SQL
convert it.
Why, this is agains everything writen in this newsgroup, do you have a
reference who stated this beside you?
Hmm, nope this one was all me addressing readability.
string blah = "sometext" + "sometext" + "sometext" seems less readable than
string blah = string.format("{0}{0}{0}", "sometext") -- So this is my
assertion alone, since it is my comentary I am allowed to interject my
thoughts.
string SQL = string.format("Insert Into Table (Field1, Field2, Field3)
Values({0}, {1}, {2}", "test", 1, MyDateTime.ToString("LongDate")) may seem
trival but when the SQL is much more complex, I think I can present a very
clean view of the code over & or +
You cannot choose those who attacking you, do you have a reference who
stated this beside you?
Say WHAT? Are you not in control of your application? Do you not implement
the workflow for fulfill your user's needs? No one, and I am fairly
confident in this, outside of the development group cares how you make a SQL
statement, they only care that the application works as they expect. No,
anyone is a potential threat, and thus, everyone should be treated as such.
Granting more power to those with more to loose is a generally accepted
approach in workflow, and no I don't have anyone to quote, but you are
welcome to challenge the statement - bring your A game.
In general, normal users perform simple CRUD and rarely have need for
specialized queries. Oh sure there are occasions where developers attempt
to access dataobjects dynamically to gain generality or commonanlity
Select Count(*) from [TableName] where <Criteria> --Access's DCount for
example. And so, you may not beable to restrict who fires this code since
it has been made so generic and most likely incorporated throughout the
code. In this case, you have to test the input values or at a minimum
restrict the points of failure.
public int DCount(TableName table, params[] whereclause)
{
//Params[] is carefully managed by the UI to ensure that values make sense
string sqlcmd = string.empty;
switch(table)
{
case TableName.Users:
cmd.commandtext = string.format("select count(userid) from users");
foreach (param in params)
{
cmd.paramaters.add(dbfactory.createinparameter(param.fieldname,
param.fieldvalue)) //'In the case of a Between clause or an IN statement you
may want to do more work
}
break;
}
return cmd.ExecuteScalar();
}
However, there are times where you just can't/wont break down a SQL
statement into parameters:
public IUserListing GetUserListing(bool fullUser, bool
includeInActiveAccounts, int sortColumn)
{
string blah = string.empty; //stringbuilder would be my preferred option
here, but I don't want to have to defend that as well.
if (fullUser)
blah = "Select userid, username, firstname, lastname, dob,
datelastpwdchng from users ";
else
blah = "Select userid, username, datelastpwdchng from users ";
if (!includeInActiveAccounts)
blah = blah + " Where active = 0 ";
if(sortcolumn > 0 and sortcolumn < 6)
{
blah = blah + " order by ";
switch (sortColumn)
{
case 1:
blah = blah + " userid asc"; //Yeah I know ordering generally
shouldn't be done here, this is just for illustration.
break;
}
}
}
There are other examples as well. All I was proposing here is that when you
make SQL dynamic and allow the user to provide input, you restrict (as much
as you can) who has access to it. Convience store clerks are told to let
robbers take what they want and offer no resistance, while the store owner
might actually challenge the robber. My point is, rank and file endusers
have nothing at stake really and so if careless are likely to break the
application, it disgruntled go out of their way to do harm.
You can do that with dynamic Sql as well, do you have a reference who
stated this beside you?
You know, I really think you read this and thought "Who is this usupur, I'll
fix him with these obtuse statements." Cor, certainly you can issue DML/DDL
statements dynamically. That wasn't the point, the point was to control
when and how those statements were called and to what extent. Our
application is only responsible for it's user base and their actions, we are
not going to attempt to control direct access to the SQL server via
nefarious means beyond our control that is what the IT dept is for. So in
the event that some fool has figured out how to issue a buffer overrun or
issue a sql injection attact against our code, we stop them with good ol
fashioned security on the sql server -- they may have the application, but
the data is still ours. By revoking the user DML/DDL access to the tables
and views and grating execute permissions on a stored proc allows the
application to keep things more inline.
Now, these are my own interruptations of stored proc's, of which, this very
thread had like minded sentiments, since you wanted a reference.
In my original post, I covered the three ways mentioned throughout the
thread in an attempt to consolidate the responses and throw in my 2 cents at
the same time. You, Cor, took exception to my commentary and challenged my
thinking; however, you did not cite any reason, other than your own
commentary. I don't think we are too far offbase from each other, I think
we just have different rationale or implimentations in mind on the subject.
Please forgive the C# pseudo-code, it has been a while since I've used VB
and C# is so second nature now.