Published 2005-02-16 15:57:59

Another day at the C# school of torture.

Querying a database is nice and easy in PHP, especially with some of the nice code in PEAR, In the quick example below I just get a user account from the database and update the activity log (using DBDO - although DB_DataObject works the same.):

$tbl = DBDO::factory('mydb','user');
if (!$tbl->get('username', $_POST['username']) ||
!($tbl->password == md5($_POST['password'])) {
return false; // access denied....
}

Logging the access:
$log = DBDO::factory('mydb','activity');
$log->user_id = $tbl->id;
$log->at_time = date('Y-m-d H:i:s');
$log->insert();

Now turning to C#, this nice clear piece of code turns into a noisy mess with potential to explode at any time.

SqlCommand dbcmd = new SqlCommand(
"SELECT * FROM user_details_basic WHERE " +
"username = @username"
,dbcon);
SqlParameter param = new SqlParameter("@username",
SqlDbType.VarChar );
param.Value = Request.params['username'];
dbcmd.Parameters.Add(param);
ArrayList user = getResults(dbcmd); // This is a 40 line method!
if (user.Count < 1) {
closeDB();
return 0; // not found..
}
// as I said before (md5 is a 10 line method)
if (0 != String.Compare((String) qmd5(password) ,
(String) ((Hashtable) user[0])["password"])) {
closeDB();
return 0;
}
.....
SqlCommand dbcmd = new SqlCommand(
"INSERT INTO activity " +
" ( user_id, at_time )" +
" VALUES ( @user_id , GETDATE() )",dbcon);
param = new SqlParameter("@user_id", SqlDbType.Int);
param.Value = (int) ((Hashtable) user[0])["id"])
dbcmd.Parameters.Add(param);
// imagine this code with quite a few more parameters.
reader = dbcmd.ExecuteReader();
The more I code in C#, The more I wonder if it's ever going to grow on me... it seems that although it's got a standard library, the quality of it is extremely poor, and not designed to help you solve problems, but rather waste time coding up simple things over and over again..


Mentioned By:
google.com : C# explode (218 referals)
google.com : february (208 referals)
google.com : december (77 referals)
google.com : explode c# (57 referals)
google.com : SQLite c# (52 referals)
google.com : c# explode string (47 referals)
google.com : C# SQLite (36 referals)
google.com : c# string explode (36 referals)
www.planet-php.net : Planet PHP (26 referals)
google.com : explode string C# (23 referals)
google.com : akbkhome.com (16 referals)
google.com : C# TODO (15 referals)
google.com : sqlite c# example (15 referals)
www.artima.com : PHP Buzz Forum - fighting C# just to do simple things. (13 referals)
google.com : c# php explode (11 referals)
google.com : Explode in C# (10 referals)
google.com : PHP explode C# (10 referals)
google.com : string explode c# (10 referals)
www.artima.com : PHP Buzz (8 referals)
google.com : c# php comparison (8 referals)

Comments

My deepest sympathies...
Alan, I (unfortunatly) *so* know what you're talking about... sometimes I wonder if Microsoft invented this as the new programming standard for Windows just to slow down the competition, who now has to code in this crap.

Everytime I look at .NET code, it just seems so overly complicated, it's plain silly. Gives me the creeps.

Unfortunately, at the place where I currently work, I see this stuff quite often :-(
#0 - Markus Wolff ( Link) on 2005-02-16 17:52:03 Delete Comment
Not just DB stuff
Personally I can't fault C# itself, it's a great language. The problem lies as you've illustrated in some of the libraries. Another example is the schei�e that pretends to be the regex classes. Simple regexs with a few parenthesised groups turn into a nightmare to code for.

FWIW, for this reason, the SQLite wrapper I wrote (http://www.phpguru.org) is modelled loosely on the PEAR::DB API instead of the ADO stuff, and as a result, is (IMO) a heck of a lot easier to use.
#1 - Richard Heyes ( Link) on 2005-02-16 21:08:20 Delete Comment
Once you go PHP, you'll never look back
I really don't know if its because PHP was the first language I really became familiar with, but it is just so darn easy to make very complex software in it.

Java and C# just seem to abstract you so much from the real work of the program. To me, PHP is very straight forward.

I really miss PHP when I've got to open a file for reading in Java: http://kickthedonkey.net/archives/2004/07/26/java-snobs-the-next-comic-book-guy/
#2 - Kick The Donkey ( Link) on 2005-02-16 21:17:19 Delete Comment
It really *is* the libraries...
Richard is correct, it really *is* the libraries. We also do a lot of VB.NET and it's all the same complicated crap there as well, although the language itself is entirely different.

Java and C#, however, look almost the same except for a few details. When I look at Java code, however, I feel a lot more comfortable as the libraries (for example, JDBC) are a lot less complicated, much more down-to-earth than the .NET equivalents. If Java was loosely typed, it would essentially be a lot like PHP ;-)

So, I'm not bitching C#, I'm bitching the .NET framework.
#3 - Markus Wolff ( Link) on 2005-02-16 23:29:32 Delete Comment
Apple and Oranges.
How is it possible to compare an OR mapping (DBDO) in php with an database abstraction layer in .NET? Apples and Oranges.
#4 - Wei ( Link) on 2005-02-17 06:52:57 Delete Comment
A little unfair, but still true
While it is a bit unfar to compare apples an oranges, even if I just used PDO (which is more an db abs. layer, it would illustrate the same point.

Alot of the .NET framework libraries are so incomplete and badly thought out as to be more a hinderance than a benefit.
#5 - Alan Knowles ( Link) on 2005-02-17 12:10:41 Delete Comment
Apples and Oranges indeed
I agree to WEI, that the comparison is not really a good one.

Have you ever looked into the Gentle object persistence framework (http://freshmeat.net/projects/gentle/)? I am using it in a business management application I'm writing right now and I'm quite happy with it.

Besides, I'd rather have a framework like .NET where I actually know what is happening then PHP, which does so many things implicitly just inviting you to write bad code.

Torben
#6 - Torben Nehmer ( Link) on 2005-02-21 22:47:07 Delete Comment
its handcoding vs VS.net
trying to handcode C# can be annoying yes, but if you let a tool like Visual Studio.net do the heavy lifting for you then you can develop apps super quick. I was able to create a C# application that gathered environmental variable data from a host, database settings, registry settings , then report that info back to a central database for processing... took about 3 hours to do the whole thing.
#7 - Jim Plush ( Link) on 2005-03-20 01:24:36 Delete Comment
tiny sample...
...and how will you get user's default language in php?
in C# its the only:
<%=Request.UserLanguages[0].Split(',')[0]%>
its only tiny example of it's power.
and only who knows it poor will claim its weak.
Regards.
#8 - Dm. ( Link) on 2005-06-23 19:39:18 Delete Comment
Microsoft and Zend to cooperate.
Microsoft has signed a contract with ZEND (PHP), to improve how Windows interprets and runs the code on IIS 5.1,6,7, This is a clear indication that PHP wins, due to its clear use, OO capabilies.
Personally I was a guru in C# and ASP.net before I switched to PHP.

Microsoft does that, because they want to keep Windows and IIS a "best option".

I say if PHP can run under IIS, it's a previlage for IIS and Windows.

Viva PHP !!
#9 - Naderman ( Link) on 2007-09-16 08:12:02 Delete Comment
Bad code?
Am I the only one to notice that your PHP example is inviting disaster by allowing SQL Injection Attacks?

And there is a reason why MS has introduced a very straight forward framework for developing user profiles (which is what you're trying to show here).

If I'm right, then all this fuss can be taken care of with the Profile.Data = Value method.

Not only is it completely baseles that you are doing any "fighting" in this case, but it seems you have gone out of your way to present an invalid comparison (in regard to "fighting"), but you've managed to show that C# is safer by default in this example since you used a parametized query.

I've used custom stuff for scripting with MVC and the like as well as off-the-shelf code like CakePHP+Debian+Lighttpd.
The same for C#+ProviderModel+IIS6/Mono and I've found varying degrees of success with each.
NEVER have I seen one platform work across the board for all situations.

"Fighting" will only take place when the wrong solution, the wrong code, and the wrong mindset rules the project.

There's an appropriate solution for every occasion.
If you want to further PHP, then please present valid comparisons.

Knowledge is advanced only the truth is presented.
#10 - Ghostnet ( Link) on 2007-09-23 14:40:09 Delete Comment

Add Your Comment

Follow us on