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.

1 comment:

  1. I'm sorry dear. All of this is very much over my head. I have read this over and over and the first database code? looks just as confusing as the second, but even though I do not understand anything you say in your posts, I can see that it is hard. Thank you for trying and learning this incredibly confusing language for us. You are so smart and talented, I am so lucky to have you.

    ReplyDelete