Informix DB Connection and Loading to DataTable

We connect to informix database in our web application and retrieve the data and store it in a DataTable.


As we use for the DataTable dt parameter in the GetData () method, we use the "ref" key to pass a parameter by reference.


using System.Data;
using IBM.Data.Informix;

public bool GetNameOfCustomerByID(int id)
{

	string query = "select name from customer where ID=" + id.ToString();
	DataTable dt = new DataTable();
	bool flag = GetData(query, ref dt);
	string name = "";
	if (flag)
	{
		foreach(DataRow item in dt.Rows)
		{
			if (item[0] != null)
				name = item[0].ToString();
			else
				name = null;
			
		}
	}
	else
	{
		return flag;
	}

}


public bool GetData(string query, ref DataTable dt)
{
	IfxConnection conn = new IfxConnection();
	conn.ConnectionString = "enter your connection string";
	IfxCommand cmd = new IfxCommand();
	
	conn.Open();
	
	IfxTransaction myTrans = conn.BeginTransaction();
	
	cmd.Connection = conn;
	
	try
	{
		try
		{
			cmd.Transaction = myTrans;
			cmd.CommandText = query;

			IfxDataReader dr = cmd.ExecuteReader();

			DataSet ds = new DataSet();
			while (!dr.IsClosed)
				ds.Tables.Add().Load(dr);

			dt = ds.Tables[0];

			cmd.Dispose();
			myTrans.Dispose();
			
			conn.Close();
			conn.Dispose();

			return true;
		}
		catch { }
	}
	catch (IfxException e)
	{
		//e.Message
	}

	conn.Close();
	conn.Dispose();

	return false;
}