主题:基于SQL数据库的网页设计器
我不把访问次数保存在数据库,显示登陆次数是正常有效的. 我想把登陆次数保存在数据库,一调用DbPageCounter.cs 结果登陆次数每次显示都是0.
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// DbPageCounter 的摘要说明
/// </summary>
public class DbPageCounter:IPageCounter
{
private SqlConnection conn;
public DbPageCounter()
{
//从配置文件web.config中读取数据库连接串
string strConn = ConfigurationManager.AppSettings["ConnStr"];
conn = new SqlConnection(strConn);
}
~DbPageCounter()
{
conn.Close();
}
#region IPageCounter成员
public int GetPageCount()
{
//打开数据库连接
conn.Open();
//创建SqlCommand
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT PageCount FROM SiteStat";
int iCount = 0;
try
{
//读取PageCount字段的值
iCount = (int)cmd.ExecuteScalar();
}
catch (SqlException ex)
{
}
catch (Exception ex)
{
}
finally
{
//关闭数据库连接
conn.Close();
}
return iCount;
}
public void SetPageCount(int iCount)
{
//打开数据库连接
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText =string.Format("UPDATA SiteStat SET PageCount = {0}",iCount);
try
{
//执行update语句
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
}
catch (Exception ex)
{
}
finally
{
//关闭数据库连接
conn.Close();
}
}
public void IncreasePageCount()
{
//打开数据库连接
conn.Open();
//创建SqlCommand
SqlCommand cmd = conn.CreateCommand();
//开始事务
SqlTransaction trans = conn.BeginTransaction();
cmd.Connection = conn;
cmd.Transaction = trans;
cmd.CommandText = "UPDATA SiteStat SET PageCount = PageCoun + 1";
try
{
//执行update语句
cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
try
{
//若发生异常则回滚
trans.Rollback();
}
catch(SqlException se)
{
}
}
finally
{
//关闭数据库连接
conn.Close();
}
}
#endregion
}
哪位大虾帮忙看看 是那里有问题.谢谢
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// DbPageCounter 的摘要说明
/// </summary>
public class DbPageCounter:IPageCounter
{
private SqlConnection conn;
public DbPageCounter()
{
//从配置文件web.config中读取数据库连接串
string strConn = ConfigurationManager.AppSettings["ConnStr"];
conn = new SqlConnection(strConn);
}
~DbPageCounter()
{
conn.Close();
}
#region IPageCounter成员
public int GetPageCount()
{
//打开数据库连接
conn.Open();
//创建SqlCommand
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT PageCount FROM SiteStat";
int iCount = 0;
try
{
//读取PageCount字段的值
iCount = (int)cmd.ExecuteScalar();
}
catch (SqlException ex)
{
}
catch (Exception ex)
{
}
finally
{
//关闭数据库连接
conn.Close();
}
return iCount;
}
public void SetPageCount(int iCount)
{
//打开数据库连接
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText =string.Format("UPDATA SiteStat SET PageCount = {0}",iCount);
try
{
//执行update语句
cmd.ExecuteNonQuery();
}
catch (SqlException ex)
{
}
catch (Exception ex)
{
}
finally
{
//关闭数据库连接
conn.Close();
}
}
public void IncreasePageCount()
{
//打开数据库连接
conn.Open();
//创建SqlCommand
SqlCommand cmd = conn.CreateCommand();
//开始事务
SqlTransaction trans = conn.BeginTransaction();
cmd.Connection = conn;
cmd.Transaction = trans;
cmd.CommandText = "UPDATA SiteStat SET PageCount = PageCoun + 1";
try
{
//执行update语句
cmd.ExecuteNonQuery();
trans.Commit();
}
catch (Exception ex)
{
try
{
//若发生异常则回滚
trans.Rollback();
}
catch(SqlException se)
{
}
}
finally
{
//关闭数据库连接
conn.Close();
}
}
#endregion
}
哪位大虾帮忙看看 是那里有问题.谢谢