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;
}
NHibernate .NET Framework ORM
NHibernate 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 :
- Dialect is what DBMS that you're using, look at the references for the list of Dialects
- Connection String can be got from ADO.NET Connection string, it's same
- Proxy Factory is the LazyLoading library that you choose
- mapping is for map the relational table to object class on .NET usually use the Project Name
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




