• missing xbfish.com image

Tag Archives: C#

Bubble Sort in C#

Bubble sort is an algorithm of sorting an array whereby larger elements will be push to the back of the array.

The bubble sort (ascending to descending) code in C#:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
using System;
 
class Program
{
    static void Main(string[] args)
    {
        int [] array =  new int [10] {100, 50, 20, 40, 10, 60, 80, 70, 90, 30};
        int array_size = 10;
 
        Console.WriteLine("The array before Bubble Sort is: ");
        for (int i = 0; i < array_size; i++){
            Console.WriteLine("array[" +i +"] = " +array[i]);
        } 
 
        // Now we will use bubble sort
        int temp;
        for (int index = array_size - 2; index >= 0; index--) {
            for (int i = 0; i <= index; i++) {
                if (array[i] > array[i + 1]) {
                    temp = array[i];
                    array[i] = array[i + 1];
                    array[i + 1] = temp;
                }
            }
        }
 
        Console.WriteLine();
        Console.WriteLine("The array after Bubble Sort is: ");
        for (int i = 0; i < array_size; i++) {
            Console.WriteLine("array[" + i + "] = " + array[i]);
        } 
    }
}

Here is a dancing video illustrating bubble sort:

Selection Sort in C#

Selection sort is an algorithm of sorting an array where it loop from the start of the loop, and check through other elements to find the minimum value. After the end of the first iteration, the minimum value is swapped with the current element. The iteration then continues from the 2nd element and so on.

The selection sort (ascending to descending) code in C#:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using System;
 
class Program
{
    static void Main(string[] args)
    {
        int array_size = 10;
        int [] array =  new int [10] {100, 50, 20, 40, 10, 60, 80, 70, 90, 30};
 
 
        Console.WriteLine("The array before Selection Sort is: ");
        for (int i = 0; i < array_size; i++){
            Console.WriteLine("array[" +i +"] = " +array[i]);
        }
 
        // Now we will use selection sort
        int tmp, min_key;
 
        for (int j = 0; j < array_size - 1; j++){
            min_key = j;
 
            for (int k = j + 1; k < array_size; k++){
                if (array[k] < array[min_key]){
                    min_key = k;
                }
            }
 
            tmp = array[min_key];
            array[min_key] = array[j];
            array[j] = tmp;
        }
 
        Console.WriteLine("The array after Selection Sort is: ");
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("array[" +i +"] = " +array[i]);
        }
    }
}

To compliment the above code, here is a dancing video to illustrate selection sort:

connecting C# to MS Access

To connect C# to Microsoft Access Database, we need to make use of OLEDB class. Below is an example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System.Data.OleDb;
 
class Test
{
    static void Main(string[] args)
    {
        //create the database connection
            OleDbConnection Connection = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\database.mdb");
 
            //create the command object and store the sql query
            OleDbCommand Command = new OleDbCommand("SELECT rank, name FROM EMPLOYEE WHERE Employee_ID = 99999", Connection);
 
            Connection.Open();
 
           //create the datareader object to connect to table
            OleDbDataReader Reader = Command.ExecuteReader();
    }
}

Lets say if we decided to display the query result into some textbox, we do the following:

1
2
3
4
5
6
7
8
9
10
           //Iterate through the database
           while(Reader.Read())
           {
                //Assuming the first column data is a string
                name.Text = Reader.GetString(0); 
                //Assuming the second column data is a integer
                age.Text = Reader.GetInt32(1); 
           }
           //Close the database connection
           Connection.Close();

Simple eh? :D Hope it helps!

connecting C# to MySQL

This is pretty hard initially as C# also has OLEDB, ODBC drivers to communicate with a database. However, I’ll be using none of these. Let me guide you through as I have enlightened:

Firstly, download the latest MySQL Connector/Net, for connecting to MySQL from .NET

Secondly, add a reference to your project, it is probably in C:\\Program Files\\MySQL\\MySQL Connector Net 5.0.7\\Binaries\\.NET 2.0 folder (it depends on your connector version), add the MySql.Data.dll file as a reference.

Thirdly, make your connection string, the following code will shows a standard MySQL connection string.

1
2
3
4
5
6
7
8
9
10
11
12
13
using MySql.Data.MySqlClient;
 
class Program
{
    static void Main(string[] args)
    {
        string my_connection = "SERVER=localhost;" +
            "DATABASE=rabbit;" +
            "UID=root;" +
            "PASSWORD=;";
        MySqlConnection connection = new MySqlConnection(my_connection);
    }
}

Lastly, you can make use of the Connector methods and class by reading Connector/NET Examples and Usage Guide

This marks the end of my simple tutorial in setting up C# with MySQL. When I am free, I will do how a tutorial on how to retrieve data or maybe how C# interact with MySQL !