Monday, February 1, 2010

Database Connecting.

All the sites I work on generally have a Database of some kind. So it is of course, important for me, to learn to pull and push data to and from a database.

the PHP way:

$dbh = mysql_connect('hostname', 'username', 'password') or die( mysql_errno().': '.mysql_error());
    mysql_select_db('db_name');
$sql = "SELECT * FROM `table`";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result)){
print_r($row);
}
?>

the ASP way:
Okay...it took me an hour just to get setup to connect to the database. I had to download a VS plugin from mysql just to put the proper classes in place. I then had to add Assembly DLLS to point to my MySql class objects that I downloaded and installed.

Here is the "small" script to connect to the database. Just to connect! I don't even know how to read the information I pull and use it as it doesn't have a simple function to do so!

string connStr = "server=localhost;user=user;database=db_name;port=3306;password=password;";
        MySqlConnection conn = new MySqlConnection(connStr);
        try
        {
            conn.Open();
            MySqlCommand cmd = new MySqlCommand();
            cmd.CommandText = "SELECT * FROM `test`";
            cmd.Connection = conn;
            MySqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                StatusLabel.Text = reader.ToString();
            }
            conn.Close();
        }
        catch (Exception ex)
        {
            StatusLabel.Text = ex.ToString();
        }

Not to mention having to put a punch of class includes int he top of the file:
using System;
using System.Data;
using MySql.Data;
using MySql.Data.MySqlClient;

This is the worst implementation of Mysql I have ever seen. It is so ridiculous to try and connect to the database.

It should be as Simple as:

MySqlConnection conn = new MySqlConnection("hostname", "username", "password");
conn.selectDb("db_name");
conn.query("YOUR FRIGGIN QUERY HERE");
while(conn.getResults()){
conn.currentRow();
}

But no...they had to make it ridiculously difficult to do.

I hate you .NET

I hate you.

First Website

As with every programming language, the first step is Hello World.

I come from a very heavy Object Oriented Background and have Experience with C++, so the switch to C# was not that startling to me.

I really enjoyed the Code behind the page setup, even if it is a bit bulky. Essentially every page has another page, that controls the functions and elements on that page. A little weird getting used to. I setup a page that had a label and a button, when the button is pressed, the page reloads and the label says something different.

How is this accomplished in .NET:

Add the Label:


This will create a Label Object in ASP, this is essentially a SPAN element when rendered out in HTML,

On the backend code, I can then modify this object however I please:


How this is Accomplished in PHP:



The next step after adding the Label, is to add the button, in PHP you must wrap it in a

element, while in ASP the entire page is already encased in a form element.
ASP:



You'll notice that it uses an onclick event, this is NOT the javascript onclick. THis tells ASP what to do, when the button is clicked (when the form is submitted).

This is controlled in the backend cs file, with the function

PHP:
We must wrap our button into a form element, easy to do


We then want it to change the $helloWorld variable to say something different, we can do this with a simple if then statement; So back where we set our $helloWorld variable we change it with:



And that is how I made my first webpage in ASP that actually did something.

Thoughts So Far?

.NET Blows. I hate it. I have to modify 2 files to get 1 result. It is nifty in that it is separated and easy to read, but the sheer time it takes to edit the functions and test and develop...is ....ugh....

A Little Background Information

This blog, is about my attempt to learn .NET after having used PHP for over 8 Years.

There are always arguments back and forth on why PHP is better than .NET, and why .NET is "better" than PHP. I am learning .NET and I will unveil the truth, of which is better, and why.

Come on this journey with me, as we explore the dark dank halls of ASP development.