Ads

Get STM32 tutorial using HAL at $10 for a limited time!

Showing posts with label PostgreSQL. Show all posts
Showing posts with label PostgreSQL. Show all posts

Saturday, May 2, 2015

Java and PosgreSQL Tutorial 1 - Database Connection

In this tutorial I will share how to make an application with Java that use PostgreSQL database. I will use Netbeans IDE for this tutorial. Netbeans already has PostgreSQL library, so to use this library you can add to your project from Library folder (right click) then select Add Library. In the Add Library dialog, select PostgreSQL JDBC Driver.


Wednesday, April 15, 2015

C# and PostgreSQL Using Npgsql Tutorial 3 - SELECT Command

In this tutorial, I will share to you about SQL SELECT command. For me there is two technique that commonly used for read the result of SELECT command. The first technique is use NpgsqlDataReader and the second is use NpgsqlDataAdapter. This is the example code when using NpgsqlDataReader:
// Select all records form tb_music using NpgsqlDataReader.
public List<Music> selectAllRecordsUsingList()
{
    List<Music> music = new List<Music>();

    openConnection();

    try
    {
        // Create select command.
        NpgsqlCommand command = new NpgsqlCommand("SELECT * FROM " + 
            "tb_music ORDER BY id ASC", connection);

        // Prepare the command.
        command.Prepare();

        // Execute SQL command.
        NpgsqlDataReader dr = command.ExecuteReader();

        // Fill results to music list.
        while (dr.Read())
        {
            music.Add(new Music(dr.GetInt32(0), dr.GetString(1), 
                dr.GetString(2)));
        }
    }
    catch (NpgsqlException ex)
    {
        showError(ex);
    }

    closeConnection();

    return music;
}

C# and PostgreSQL Using Npgsql Tutorial 2 - INSERT, UPDATE, and DELETE Command

In this tutorial, I will share to you how to make functions for performing basic SQL command such as INSERT, UPDATE, and DELETE. For this tutorial I will add 3 more functions that needed to perform SQL basic commands (INSERT, UPDATE, and DELETE). This is the function for INSERT new record to tb_music table.
// Insert new record to tb_music.
public void insertRecord(Int32 id, String title, String artist)
{
    openConnection();

    try
    {
        // Create insert command.
        NpgsqlCommand command = new NpgsqlCommand("INSERT INTO " +
            "tb_music(id, title, artist) VALUES(:id, :title, " +
            ":artist)", connection);

        // Add paramaters.
        command.Parameters.Add(new NpgsqlParameter("id",
            NpgsqlTypes.NpgsqlDbType.Integer));
        command.Parameters.Add(new NpgsqlParameter("title",
            NpgsqlTypes.NpgsqlDbType.Varchar));
        command.Parameters.Add(new NpgsqlParameter("artist",
            NpgsqlTypes.NpgsqlDbType.Varchar));

        // Prepare the command.
        command.Prepare();

        // Add value to the paramater.
        command.Parameters[0].Value = id;
        command.Parameters[1].Value = title;
        command.Parameters[2].Value = artist;

        // Execute SQL command.
        int recordAffected = command.ExecuteNonQuery();
        if (Convert.ToBoolean(recordAffected))
        {
            showInformation("Data successfully saved!");
        }
    }
    catch (NpgsqlException ex)
    {
        showError(ex);
    }

    closeConnection();
}

Monday, April 13, 2015

C# and PostgreSQL Using Npgsql Tutorial 1 - Database Connection

In this tutorial I will share how to make an application with .NET C# that use PostgreSQL database. I am using Visual Studio 2012 for this tutorial. To use Npgsql you must download the library from here. Then add Npgsql.dll and MonoSecurity.dll to project references. References can be found in solution explorer.


After you add Npgsql.dll and MonoSecurity.dll to project references, the next step is to include Npgsql library to your source code using this code:
using Npgsql;
For this tutorial, I will share how to make connection between the application and the database. I will make a class called DatabaseHelper. This class is responsible for handling database functions such as database connection, INSERT, UPDATE, DELETE, SELECT and so on.