I Made Krisna's Times My Way to Write the Times

10Aug/110

Get All Local Network Interface Card (NIC) C# Source Code

Yeah, actually i'm back to blogger activities... This is the part of my next projects, M-NIC Projects. The goal is to get all information of our network interface card (NIC) information such as IP addresses, link speed, name, etc. So after doing some head-to-head within some programming languages, my choices is C# .NET, because i think it's easiest way to get that. The result is something like below, i retrieve all of my NIC which status is UP

Name            : Wireless Network Connection 2
Type            : Wireless80211
Status          : Up
Speed           : 4294967295
Description     : Microsoft Virtual WiFi Miniport Adapter
InterNetworkV6  : fe80::e1cf:b2ee:f5c7:e23e%31
InterNetwork    : 192.168.2.1
=======================================
Name            : Local Area Connection
Type            : Ethernet
Status          : Up
Speed           : 100000000
Description     : Atheros AR8152 PCI-E Fast Ethernet Controller
InterNetwork    : 192.168.1.3
=======================================
Name            : Loopback Pseudo-Interface 1
Type            : Loopback
Status          : Up
Speed           : 1073741824
Description     : Software Loopback Interface 1
InterNetworkV6  : ::1
InterNetwork    : 127.0.0.1
=======================================

And here is the code:

10Jan/110

Rail-Fence Cipher C# Source Code

The rail-fence cipher algorithm has been posted before so this post just show how to implement the algorithm to programming language in C#. First of all always the encryption method:

public static string Encrypt(int rail, string plainText)
{
    List<string> railFence = new List<string>();
    for (int i = 0; i < rail; i++)
    {
        railFence.Add("");
    }

    int number = 0;
    int increment = 1;
    foreach (char c in plainText)
    {
        if (number + increment == rail)
        {
            increment = -1;
        }
        else if (number + increment == -1)
        {
            increment = 1;
        }
        railFence[number] += c;
        number += increment;
    }

    string buffer = "";
    foreach (string s in railFence)
    {
        buffer += s;
    }
    return buffer;
}

28Oct/100

Hello World MPI C Source Code

Got something interesting, MPI (Message Passing Interface) is the language or library which needed to create parallel programming. So let's start with the usual way to begin code, "Hello World". This post will be show how to make the simplest MPI code in C language.

#include "stdio.h"
#include "mpi.h"

int main(int argc, char** argv)
{
    int rank;
    int size;

    /* Initialize MPI  */
    MPI_Init(&argc, &argv);

    /* Getting the World Size and Process Number (rank) */
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    /* Routine for process 0 */
    if (rank == 0)
    {
        printf("P%d : I Found this world size is %d processes\n", rank, size);
        printf("P%d : Hello World! This is process number %d writing!\n", rank, rank);
    }
    /* Routine for process 1 */
    else if (rank == 1)
    {
        printf("P%d : I Found this world size is %d processes\n", rank, size);
        printf("P%d : Hello World! This is process number %d writing!\n", rank, rank);
    }
    /* Routine for Other Processes */
    else
    {
        printf("P%d : I'm Unused Process...\n", rank);
    }

    /* End of MPI */
    MPI_Finalize();
    return 0;
}

31May/100

NHibernate .NET Framework ORM

NHibernateNHibernate is the most common ORM (Object-Relational Mapping) that used on .NET Framework Programming. With NHibernate the relational object such as Database Tables can be represented with Object such as Classes on Object Oriented Programming. So how to start it?

First step, of course you must download the NHibernate library. Where? Just go to http://sourceforge.net/projects/nhibernate/files/ and there is many files but the most important is bin package. Extract the zip compressed package to some folder.

Second step, adding the library or references to Visual Studio Project. Open your Visual Studio 2008 and create new project. Just right click on the References then Add References, Browse the .dll files named NHibernate.dll on Required_Bins. Then Add the LazyLoading references, i'm using LinFu so add the NHibernate.ByteCode.LinFu.dll

Then next step is preparing the database. Let's see now i'm using MySQL as the DBMS you also can use other DBMS that recommended by NHibernate (look at Dialect that supported by NHibernate at reference package). Suppose there are MySQL Database named "nhibernate" with username and password = "nhibernate"

Then let's create the app.config for connection between NHibernate Framework and MySQL DBMS. To create app.config just right click on your project then "Add New Item" and choose Application Configuration File.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
  </configSections>
  <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
    <session-factory>
      <property name="dialect">NHibernate.Dialect.MySQL5Dialect</property>
      <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
      <property name="connection.connection_string">server=127.0.0.1;User Id=nhibernate;password=nhibernate;Persist Security Info=True;database=nhibernate</property>
      <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property>
      <mapping assembly="SimpleNHibernate"/>
    </session-factory>
  </hibernate-configuration>
</configuration>

For helping you creating the App.config file, you can set the xml schema. Click on editor window where App.config file opened and look at properties window and set the Schemas, browse to "nhibernate-configuration.xsd" at Required_Bins folder from NHibernate bin package.

Some explanation :

  1. Dialect is what DBMS that you're using, look at the references for the list of Dialects
  2. Connection String can be got from ADO.NET Connection string, it's same
  3. Proxy Factory is the LazyLoading library that you choose
  4. mapping is for map the relational table to object class on .NET usually use the Project Name

31May/100

MySQL Query with C# ADO.NET

This post is using MySQL .NET Connector for connecting .NET Framework with MySQL Database Management System, so if you're looking for OBDC connection that's not here but you still can learn this because this much easier than ODBC Connection. Please look at my previous post for preparing your Visual Studio to be able connect with MySQL [How to Connect MySQL Database with C# ADO.NET]

Directly to the code, leave the default library using and add two additional lines below the defaults.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Common;
using MySql.Data.MySqlClient;

Now the main part, creating ADO.NET connection for MySQL Database. I've found two ways that's very similar but the result for programme portability is very different. First way is using Database Factories and the second way is using directly MySQL Connection object from the reference which was added before (MySql.Data.dll)

Here is the example code using Database Factories

        static void connectUsingFactory()
        {
            DbProviderFactory factory = DbProviderFactories.GetFactory("MySql.Data.MySqlClient");
            DbConnection connection = factory.CreateConnection();
            connection.ConnectionString = "server=10.151.34.31;User Id=adonet;password=adonet;Persist Security Info=True;database=adonet";
            connection.Open();

            DbCommand command = connection.CreateCommand();
            command.CommandText = "SELECT * FROM item";

            DbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["itmid"]+"\t"+reader[1]+"\t"+reader[2]+"\t"+reader["price"]);
            }
            reader.Close();
            connection.Close();
        }

And the second way, using MySQL Connection object

        static void connectUsingReference()
        {
            DbConnection connection =
                new MySqlConnection("server=10.151.34.31;User Id=adonet;password=adonet;Persist Security Info=True;database=adonet");
            connection.Open();

            DbCommand command = connection.CreateCommand();
            command.CommandText = "SELECT * FROM item";

            DbDataReader reader = command.ExecuteReader();
            while (reader.Read())
            {
                Console.WriteLine(reader["itmid"] + "\t" + reader[1] + "\t" + reader[2] + "\t" + reader["price"]);
            }
            reader.Close();
            connection.Close();
        }

And the last is the main method for testing the codes above...

        static void Main(string[] args)
        {
            connectUsingFactory();
            Console.WriteLine();
            connectUsingReference();
            Console.ReadKey();
        }

The first way, using Database Factories is easier if we didn't know the Database Connection Class or if we're using the connector that the default ADO.NET Driver already support it because we just insert the namespace for parameter. But the first way didn't work if you're using MySQL Database .NET Connector when the computer that you're use didn't installed by the connector or only using non-installer connector. So we must use second way to make the project portable.

Download the complete Project example here : MySQLNET Connection VS2008 Project