Programming an In-Text Advertising System under ASP.NET 3.5 - The Server-side Programming
(Page 3 of 4 )
When you download the source code accompanying this article, you will see that there are three .cs files, i.e. Tools.cs, ValidateCode.cs, and Sql. under the sub-folder App_Code of the sample website. First, the Tools.cs file holds the definition of the Tools class, which contains a static method named Encrypt(). This method is used to encrypt the password of the logged-in user, which used the DES encrypting algorithm provided by .NET framework. Since this strays from the main point of this article, we won’t dwell upon it any more. You can refer to the source code for details.
Second, the ValidateCode.cs file is used to generate the verifying code that you usually see on today’s web pages, which will be discussed in the next section below.
Finally, let’s take a look at the sql.cs file, which encapsulates the server-side database-related operations. On the whole, the Sql class defines four methods named getMyDataSet, ExecuteSql, getDataSet, and exec, respectively. Below I first list the codes for the getMyDataSet method:
public DataSet getMyDataSet(string sql)
{
command.Connection = Con;//setup the command object
command.CommandText = sql;
DataSet dt = new DataSet();//initialize the DataSet object
SqlDataAdapter da = new SqlDataAdapter(command);
Con.Open();
da.Fill(dt);//execute sentence
command.Connection.Close();//Close sentence
return dt;
}
Next, the ExecuteSql method is responsible for executing a non-query typed SQL query. Below I've indicated the related coding:
public int ExecuteSql(string sql)
{
if (Con.State != ConnectionState.Open)
Con.Open();//if the connection is closed, then open it
SqlCommand Com = new SqlCommand(sql, Con);
int result = Com.ExecuteNonQuery();//execute non-query sql clause
Con.Close();
return result;
}
Since the above code is basic database operation programming using C#, and there are also necessary comments, it’s unnecessary to give any further explanation. The implementations of the other two methods, i.e. getDataSet and exec, are given as follows:
public DataSet getDataSet(string produreName)
{
command.Connection = Con;//assign the connection object
command.CommandType = CommandType.StoredProcedure;//specify the execute type
command.CommandText = produreName;//assign the name of the SP to be executed
DataSet dt = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
Con.Open();//open the connection
da.Fill(dt);//fill data
command.Connection.Close();
return dt;//return the dataset
}
//Input:name of the stored procedure; execute non-query sql clause
public bool exec(string produreName)
{
bool flag = false;//identify whether the task is finished as expected; set to false at the beginning
command.Connection = Con;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = produreName;
if (Con.State == ConnectionState.Closed) Con.Open();
try
{
command.ExecuteNonQuery();//execute the SP
flag = true;//succeed in execution
}
finally
{
command.Connection.Close();//close the connection
}
return flag;
}
Since I've provided detailed comments between the lines, we will not discuss these further.
Next, we’ll shift our attention to discussing the three generic handlers that still need to be defined on the server side.
Next: The Three Generic Handlers Related Programming >>
More ASP.NET Articles
More By Xianzhong Zhu