Sunday, April 10, 2011

ASP.NET OLEDB – Informix

I will explain how ASP.NET can connect to Informix 11 database using Ole db Informix Provider by IBM

First download IBM Informix client sdk from IBM's site, for the current example I used version 3.5 - 32 bit for windows. The SDK must be installed on client's computer in order to get connection. After installation go to the Start Menu, all program, IBM Informix Client SDK and start SetNet 32 . Enter the server tab and set Informix database parameters. For my example the parameters were

IBM Informix server : ol_ids_1150_1
Hostname : 192.168.52.2
Protocol : olsoctcp
Service name : svc_ids_1150_1

Go to the Host tab
Host:192.168.1.2
User: Administrator

Click OK

Then go to C:\WINDOWS\system32\drivers\etc and open for edit the file “services”, at the end of the file, depending on the target's database, enter corresponding values.
For my example
svc_ids_1150_1 9088/tcp #ol_ids_1150_1
svc_drda 9089/tcp #svc_drda
save the file.

After that go to Start, All programs, IBM Informix Client SDK and open ILoginDemo. On File click run and enter appropriate values in the login form, use some existing database for the stores database entry. After hitting the ok button if no error appears than you are on the right way. If an error appear, then download the document called Informix error messages from internet, and look for the appeared error number.

If everything is fine then in the ASP.NET program type following

OleDbConnection conno = new OleDbConnection();
conno.ConnectionString = "Provider=Ifxoledbc;Data Source=databaseName@IBMInformixServer;User ID=user;Password=
conno.Open();
OleDbCommand commo = new OleDbCommand("select * from tabletest", conno);
commo.CommandType = CommandType.Text;
OleDbDataAdapter da = new OleDbDataAdapter(commo);
DataSet ds = new DataSet();
da.Fill(ds);
conno.Close();

The Dataset variable is filled with data from table tabletest

Good luck

Sunday, July 5, 2009

Refactoring Tips and C# examples

Refactoring Tips are solution to the spaghetti code problem

What is refactoring ?


Refactoring is the process of changing a software system in such a way that it does not alter the external behavior of the code yet improves its internal structure
Refactoring (noun): a change made to the internal structure of software to make it easier to understand and cheaper to modify without changing its observable behavior.
Refactor (verb): to restructure software by applying a series of refactorings without changing its observable behavior.

I use most of the refactoring methods, described in the reference book.
This way the standard of code is reaching higher level as in my company
Redigon Software Solutions

Why should we refactoring?


In essence when you refactor you are improving the design of the code after it has been written,
refactoring Makes Software Easier to Understand, helps you find bugs and makes you program faster

What will heppend when future developer works on not refactored code?


Someone will try to read your code in a few months' time to make some changes. The programmer will take a week to make a change that would have taken only an hour if she had understood your code.

When to Refactor ?


Refactor when you add Function
Refactor when you need to fix a bug
Refactor as you do code review

When you should not refactor?


There are times when the existing code is such a mess that although you could refactor it, it would be easier to start from the beginning.
The other time you should avoid refactoring is when you are close to a deadline. At that point the productivity gain from refactoring would appear after the deadline and thus be too late.

What is Bad Smell in Code?


Duplicated code
Long Method
Large Class
Long Parameter List

Refactoring Methods


Extract Method


Before refactoring

void printOwing(double amount)
{
printBanner();

//print details
WriteLine("name:" + _name);
WriteLine("amount" + amount);
}
After Refactoring

void printOwing(double amount)
{
printBanner();
printDetails(amount);
}
void printDetails(double amount)
{
WriteLine("name:" + _name);
WriteLine("amount" + amount);
}

Method: Split Temporary Variable


You have a temporary variable assigned to more than once, but is not a loop variable nor a collecting temporary variable. Make a separate temporary variable for each assignment.
Before Refactoring

double temp = 2 * (_height + _width);
WriteLine(temp);
temp = _height * _width;
WriteLine(temp);
After Refactoring

double perimeter = 2 * (_height + _width);
WriteLine(perimeter);
double area = _height * _width;
WriteLine(area);

Move Method


A method is, or will be, using or used by more features of another class than the class on which it is defined.
Create a new method with a similar body in the class it uses most. Either turn the old method into a simple delegation, or remove it altogether.


The Rule of Three(Don Roberts)


The first time you do something, you just do it
The second time you do something similar, you wince at the duplication, but you do the duplicate thing anyway.
The third time you do something similar, you refactor.
Tip: Three strikes and you refactor



Text and code samples are taken from the book by Martin Fowler. I recommend to everybody to read this fantastic book.
Reference
Improving the Design of Existing Code (Addison-Wesley) by Martin Fowler.