Custom Data Adapter for Data Security (Basic)


Custom Data Adapter to show how to achieve data security for the data base tables (Very Basic Text Manipulation only)

Before we start, let me tell you about what I intended to do here.

The scenario:
I have a table "menus" in my database(MySQL)
with fields "menu_id", "menu_name", "Starters", "Deserts"

Table contains 100 rows

I want to encrypt my tables data in such a way :
For eg: One of my row is:


"menu_id" "menu_name" "Starters" "Deserts"
47 Burger Chips icecream


Should be converted into:

"menu_id" "menu_name" "Starters" "Deserts"
47 SaltyBurger Chips icecream


and on decryption it will be converted, into earlier format

My idea of encryption/decryption here is, not to use any complex crypto methods,
but to keep it simple to text manipulation only, but should affect multiple rows of the table.
(You can use complex crypto methods also, but I will discuss that another time)

My Idea, for solving the above problem is :
Create a Custom Data Adapter having its own fill and update method

So lets begin

The first thing I said about the problem was that, my data base is MySql, but you can use the same concept for any database

For information on how to connect to MySql database using Visual Studio go here:-

MySQL Connector for .Net:-
http://dotnetspider.com/resources/40729-MySql-Connector-For-net.aspx


So first Part: Create a Custom Data Adapter


If you are using MySql Connectors source code, you can modify the DataAdapter to your advantage.

Open the source DataAdapter included in MySql Connector for .net
On opening create a new constructor for the data adapter class

#region New DataAdapter Constructor
///NEW CONSTRUCTOR ///
///takes parameter as the name of the table and the connection ///
///build a select command based on the table name ///
///Store the table name in a private string member called "data_table_name" ///

private string data_table_name;//to store the data table name

public CustomDataAdapter(string My_table_name, MySqlConnection My_connection): this()
{
data_table_name = My_table_name;
string my_command_text = "SELECT * FROM " + My_connection.Database.ToString() + "." + data_table_name;
SelectCommand = new MySqlCommand(my_command_text, My_connection);
}

#endregion


So my new constructor gets the name of the table and a connection

Second Part: Create a Update Method


In this method my fields gets encrypted, by adding "Salty" before every values of the column "menu_name"

public new int Update(DataTable dataTable)
{

foreach (DataRow row in dataTable.Rows)
{
if (row.RowState != DataRowState.Deleted)
row["menu_name"] = "Salty" + row["menu_name"];
}

return base.Update(dataTable);
}


Third Part: Create a Fill Method


In this method my fields gets decrypted, by removing "Salty" before every values of the column "menu_name"

public new int Fill(DataTable dataTable)
{

base.Fill(dataTable);
dataTable.TableName = data_table_name;

foreach (DataRow row in dataTable.Rows)
{
row["menu_name"] = row["menu_name"].ToString().Substring(5);
}


return dataTable.Rows.Count;
}


Fourth Part: Create a Normal Fill Method


Normal Fill Method to load the table as it is from database

public int normal_fill(DataTable dataTable)
{
base.Fill(dataTable);
dataTable.TableName = data_table_name;

return dataTable.Rows.Count;
}


Final Part: Implementation of Custom Data Adapter


First Fill the datatable as it is from the data base

DataTable data = new DataTable();

da = new CustomDataAdapter(my_db_table, conn);
cb = new CustomCommandBuilder(da);

da.normal_fill(data);

dataGrid.DataSource = data;


Second Update the table

da.Update(data);
data.AcceptChanges();


Third Decrypt the table for your usege

DataTable data = new DataTable();

da = new CustomDataAdapter(my_db_table, conn);
cb = new CustomCommandBuilder(da);

da.Fill(data);

dataGrid.DataSource = data;


Please view my application attached for a better understanding.
In the application attached I have made the use of MySql Connector for .net

Any doubts, give a response

Cheers
Paul

Reference: http://dotnetspider.com/resources/40729-MySql-Connector-For-net.aspx


Attachments

  • custom data adapter application (40730-27834-customdataadapter.rar)
  • Comments

    No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: