This post originated from an RSS feed registered with PHP Buzz
by Alan Knowles.
Original Post: fighting C# just to do simple things.
Feed Title: Smoking toooo much PHP
Feed URL: http://www.akbkhome.com/blog.php/RSS.xml
Feed Description: More than just a blog :)
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..