B
Bruno Rodrigues
Hi all
I'm using the following code standart in class
libraries. I will not split this function in more layers,
but I want to know if there are some architetural
improvement to do in my code. E.g: Database connection;
Try/Catch use; DataTable as a set of rows; etc...
Any suggestions are welcome.
Thanks!
Bruno Rodrigues
==========================================================
using System;
using System.Data;
using System.Data.SqlClient;
namespace CodeDesign
{
/// <summary>
/// Test class for code design.
/// </summary>
public class Test
{
SqlConnection conn = new SqlConnection
("Persist Security Info=False;Integrated
Security=False;database=Test;server=Computer;Connect
Timeout=30;User ID=sa;Password=sa;");
private void openDataBase()
{
try
{
conn.Open();
}
catch(Exception e)
{
throw new Exception("An
error ocurred while opening the database.\n" + e.Message,
e);
}
}
private void closeDataBase()
{
try
{
conn.Close();
}
catch(Exception e)
{
throw new Exception("An
error ocurred while closing the database.\n" + e.Message,
e);
}
}
/// <summary>
/// Retrieves a specific record.
/// </summary>
/// <param name="id">Record ID.</param>
/// <returns>DataRow with record's
data.</returns>
public DataRow Return(int id)
{
openDataBase();
string sql = "SELECT * FROM Tests
WHERE PK = " + id.ToString();
SqlDataAdapter adapter = new
SqlDataAdapter(sql, conn);
DataTable table = new DataTable();
try
{
adapter.Fill(table);
return table.Rows[0];
}
catch(Exception e)
{
throw new Exception("An
error ocurred while retrieving the records.\n" +
e.Message, e);
}
finally
{
closeDataBase();
}
}
/// <summary>
/// Retrieves all records.
/// </summary>
/// <returns>DataTable with all
records.</returns>
public DataTable ReturnAll()
{
string sql = "SELECT * FROM
Tests";
SqlDataAdapter adapter = new
SqlDataAdapter(sql, conn);
DataTable table = new DataTable();
try
{
adapter.Fill(table);
return table;
}
catch(Exception e)
{
throw new Exception("An
error ocurred while retrieving all records.\n" +
e.Message, e);
}
finally
{
closeDataBase();
}
}
/// <summary>
/// Returns the number of records.
/// </summary>
/// <returns>Number of records.</returns>
public int Count()
{
string sql = "SELECT Count(PK) AS
Total FROM Tests";
SqlCommand command = new
SqlCommand(sql, conn);
try
{
return Convert.ToInt32
(command.ExecuteScalar());
}
catch(Exception e)
{
throw new Exception("An
error ocurred while counting records.\n" + e.Message, e);
}
finally
{
closeDataBase();
}
}
/// <summary>
/// Delete a record.
/// </summary>
/// <param name="id">Record ID.</param>
public void Delete(int id)
{
string sql = "DELETE FROM Tests
WHERE PK = " + id.ToString();
SqlCommand command = new
SqlCommand(sql, conn);
try
{
command.ExecuteNonQuery();
}
catch(Exception e)
{
throw new Exception("An
error ocurred while deleting a record.\n" + e.Message, e);
}
finally
{
closeDataBase();
}
}
}
}
I'm using the following code standart in class
libraries. I will not split this function in more layers,
but I want to know if there are some architetural
improvement to do in my code. E.g: Database connection;
Try/Catch use; DataTable as a set of rows; etc...
Any suggestions are welcome.
Thanks!
Bruno Rodrigues
==========================================================
using System;
using System.Data;
using System.Data.SqlClient;
namespace CodeDesign
{
/// <summary>
/// Test class for code design.
/// </summary>
public class Test
{
SqlConnection conn = new SqlConnection
("Persist Security Info=False;Integrated
Security=False;database=Test;server=Computer;Connect
Timeout=30;User ID=sa;Password=sa;");
private void openDataBase()
{
try
{
conn.Open();
}
catch(Exception e)
{
throw new Exception("An
error ocurred while opening the database.\n" + e.Message,
e);
}
}
private void closeDataBase()
{
try
{
conn.Close();
}
catch(Exception e)
{
throw new Exception("An
error ocurred while closing the database.\n" + e.Message,
e);
}
}
/// <summary>
/// Retrieves a specific record.
/// </summary>
/// <param name="id">Record ID.</param>
/// <returns>DataRow with record's
data.</returns>
public DataRow Return(int id)
{
openDataBase();
string sql = "SELECT * FROM Tests
WHERE PK = " + id.ToString();
SqlDataAdapter adapter = new
SqlDataAdapter(sql, conn);
DataTable table = new DataTable();
try
{
adapter.Fill(table);
return table.Rows[0];
}
catch(Exception e)
{
throw new Exception("An
error ocurred while retrieving the records.\n" +
e.Message, e);
}
finally
{
closeDataBase();
}
}
/// <summary>
/// Retrieves all records.
/// </summary>
/// <returns>DataTable with all
records.</returns>
public DataTable ReturnAll()
{
string sql = "SELECT * FROM
Tests";
SqlDataAdapter adapter = new
SqlDataAdapter(sql, conn);
DataTable table = new DataTable();
try
{
adapter.Fill(table);
return table;
}
catch(Exception e)
{
throw new Exception("An
error ocurred while retrieving all records.\n" +
e.Message, e);
}
finally
{
closeDataBase();
}
}
/// <summary>
/// Returns the number of records.
/// </summary>
/// <returns>Number of records.</returns>
public int Count()
{
string sql = "SELECT Count(PK) AS
Total FROM Tests";
SqlCommand command = new
SqlCommand(sql, conn);
try
{
return Convert.ToInt32
(command.ExecuteScalar());
}
catch(Exception e)
{
throw new Exception("An
error ocurred while counting records.\n" + e.Message, e);
}
finally
{
closeDataBase();
}
}
/// <summary>
/// Delete a record.
/// </summary>
/// <param name="id">Record ID.</param>
public void Delete(int id)
{
string sql = "DELETE FROM Tests
WHERE PK = " + id.ToString();
SqlCommand command = new
SqlCommand(sql, conn);
try
{
command.ExecuteNonQuery();
}
catch(Exception e)
{
throw new Exception("An
error ocurred while deleting a record.\n" + e.Message, e);
}
finally
{
closeDataBase();
}
}
}
}