Most recommended way to create web or windows database applications is based on "allocation" of the data access layer (DAL) in a separate layer. There are many ways to create this layer, of course, including the new feature of the .NET 2.0 - the ObjectDataSource. In this article I will describe one of the ways to build data access layer component, using such feature of the VS 2005 as Generic.
In the project , named DA (this is "DataAccess") and having type of the "Class Library", we create the class "DataAccess". This class recieves SQL connection as string and has the methods, which always returns some value (some messages as string or some DataSets).
In the case we "use" the method for delete, insert, update operations (this is "Non Query"), the method returns string. If the process is completed successfully, the returned string looks like that: "OK;6 rows affected". Now with the Substring method you can retrieve a substring from this instance and to process it according your needed logic. If the process is completed non successfully, the returned string is just the error message. In the case we use stored procedure (as CommandType for the SqlConnection object, the method has at least two parameters: name of the stored procedure as string and SqlParameter as List< SqlParameter> (here Generic come in). The example of the code for the method is the following :
public string execNonQuery(List<SqlParameter> ListSqlParams, string NameSP)
{
SqlCommand sqlCommand = new SqlCommand();
SqlParameter sqlParameter;
SqlConnection sqlConnection = new SqlConnection();
int iListCount = ListSqlParams.Count;
int iCount;
string sMessageOut = "OK";
int iRowsAffected = 0;
if (iListCount > 0)
{
for (iCount = 0; iCount < iListCount; iCount++)
{
sqlParameter = new SqlParameter();
sqlParameter = ListSqlParams[iCount];
sqlCommand.Parameters.Add(sqlParameter);
}
try
{
sqlConnection.ConnectionString = s_SQLConnection;
if (sqlConnection.State == ConnectionState.Closed)
{
sqlConnection.Open();
}
sqlCommand.CommandText = NameSP;
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.Connection = sqlConnection;
iRowsAffected = sqlCommand.ExecuteNonQuery();
sqlConnection.Close();
sMessageOut += ";" + iRowsAffected.ToString() + " rows affected.";
}
catch (Exception E)
{
sMessageOut = E.Message;
}
finally
{
if (sqlConnection.State != ConnectionState.Closed)
{
sqlConnection.Close();
}
}
}
return (sMessageOut);
}
Here s_SQLConnection is the string, that we "set" in the constructor:
namespace DA
{
public class DataAccess
{
string s_SQLConnection;
public DataAccess(string SQLConnection)
{
s_SQLConnection = SQLConnection;
}
//code of the class
................................
................................
Now , let's pass to the case with some "select" query. In this case our method returns DataSet, but also has out parameter, which informs us on success of inquiry: "OK" or just error message. As we have decided, that all our methods always returns the value, we will use special method, that returns DataSet in "non successful" processes:
private DataSet getDataSetMessage(string sMessage)
{
DataTable dt = new DataTable("DataTableMessage");
DataColumn dCol;
DataRow dRow;
DataSet ds = new DataSet();
ds.Clear();
dCol = new DataColumn("ErrorMessage", Type.GetType("System.String"));
dt.Columns.Add(dCol);
dRow = dt.NewRow();
dRow["ErrorMessage"] = sMessage;
dt.Rows.Add(dRow);
ds.Tables.Add(dt);
return (ds);
}
One of the methods, that returns DataSet, is the following:
public DataSet getDataSetSP(List<SqlParameter> ListSqlParams, string NameSP,out string sMessageOut)
{
SqlDataAdapter sqlDataAdapter;
SqlCommand sqlCommand = new SqlCommand();
SqlParameter sqlParameter;
DataSet ds = new DataSet();
SqlConnection sqlConnection = new SqlConnection();
Amarkant SemwalPosted Aug 12, 2018, 12:24 PM
It's really good sir
Danie KoroliPosted Mar 6, 2007, 3:01 AM
It's really good idea: always to return some value (the message or/and dataset getDataSetMessage for errors). I liked it! Thanks Danie