网站越来越慢,大家帮我看看数据库访问的代码有问题么?
我是C#初学者。任何方面的问题都有可能。
namespace SimCity.Data
{
using System;
using System.Data;
using System.Data.SqlClient;
/// <summary>
/// SqlHelper 的摘要说明。
/// </summary>
public class SqlHelper
{
public SqlHelper()
{
}
public static void ExecuteStoreProcedure(string strConnection, string strStoredProcedure, SqlParameter []arParas, out int nReturnValue)
{
SqlConnection connection = new SqlConnection(strConnection);
connection.Open();
SqlCommand command = new SqlCommand(strStoredProcedure, connection);
if(arParas != null && arParas.Length > 0)
{
foreach(SqlParameter para in arParas)
{
command.Parameters.Add(para);
}
}
SqlParameter parRet = command.Parameters.Add( "@returnvalue ",SqlDbType.NVarChar,30);
parRet.Direction = ParameterDirection.ReturnValue;
command.CommandType = CommandType.StoredProcedure;
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
nReturnValue = (int)parRet.Value;
}
public static void ExecuteStoreProcedure(string strConnection, string strStoredProcedure, SqlParameter []arParas)
{
SqlConnection connection = new SqlConnection(strConnection);
connection.Open();
SqlCommand command = new SqlCommand(strStoredProcedure, connection);
if(arParas != null && arParas.Length > 0)
{
foreach(SqlParameter para in arParas)
{
command.Parameters.Add(para);
}
}
command.CommandType = CommandType.StoredProcedure;
command.ExecuteNonQuery();
command.Dispose();
connection.Close();
}
public static DataSet FillDataSet(string strConnection, string strStoredProcedure, SqlParameter []arParas, string strTableName)
{
DataSet dataset = null;
SqlConnection connection = new SqlConnection(strConnection);
dataset = new DataSet();
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(strStoredProcedure, connection);
if(arParas != null && arParas.Length > 0)
{
foreach(SqlParameter para in arParas)
{
command.Parameters.Add(para);
}
}
command.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand = command;
adapter.Fill(dataset, strTableName);
command.Dispose();
adapter.Dispose();
connection.Close();
return dataset;
}
public static DataSet FillDataSet(string strConnection, string strStoredProcedure, SqlParameter []arParas, out int nReturnValue)
{
DataSet dataset = null;
SqlConnection connection = new SqlConnection(strConnection);
dataset = new DataSet();
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(strStoredProcedure, connection);
if(arParas != null && arParas.Length > 0)
{
foreach(SqlParameter para in arParas)
{
command.Parameters.Add(para);
}
}
command.CommandType = CommandType.StoredProcedure;
SqlParameter parRet = command.Parameters.Add( "@returnvalue ",SqlDbType.NVarChar,30);
parRet.Direction = ParameterDirection.ReturnValue;
adapter.SelectCommand = command;
adapter.Fill(dataset);
command.Dispose();
adapter.Dispose();
connection.Close();
nReturnValue = (int)parRet.Value;
return dataset;
}
public static DataSet FillDataSet(string strConnection, string strStoredProcedure, SqlParameter []arParas)
{
DataSet dataset = null;
SqlConnection connection = new SqlConnection(strConnection);
dataset = new DataSet();
connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter();
SqlCommand command = new SqlCommand(strStoredProcedure, connection);
if(arParas != null && arParas.Length > 0)
{
foreach(SqlParameter para in arParas)
{
command.Parameters.Add(para);
}
}
command.CommandType = CommandType.StoredProcedure;
adapter.SelectCommand = command;
adapter.Fill(dataset);
command.Dispose();
adapter.Dispose();
connection.Close();
return dataset;
}
}
}
[解决办法]
访客量大`或 空间问题`
也不一定是代码`问题`而且你是说越来越慢 `也就是以前不会这样
那代码不一定有问题`
-------------------------
愚见 ! 愚见 !
[解决办法]
没仔细看,这段代码好像是微软的写的访问类,还是你自己写的?
[解决办法]
如果你手动Dispose那些用过的东西,不如用using语句:
using (SqlConnection connection = new SqlConnection(connectionString)
{
//你的代码
}
using结束后,自动帮你进行Dispose的。
[解决办法]
微软PETSHOP 4.0的代码吗?
没看出啥问题.
[解决办法]
释放资源
使用缓存
[解决办法]
看晕了,可以使用facade外观模式把你的这段代码重新写一下了。
这个是我用的,不知道对你有帮助么 facade.cs 封装数据库链接并提供Adapter及Command属性操作数据库
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI.WebControls;
namespace course
{
/// <summary>
/// facade抽象基类:封装基本的连接数据库操作
/// </summary>
public abstract class facade
{
private SqlConnection sqlConn = new SqlConnection();
private SqlCommand sqlComm = new SqlCommand();
private SqlDataAdapter sqlAdapter = new SqlDataAdapter();
private DataSet DSguestbook = new DataSet();
/// <summary>
/// 从web.config中读取AppSettings节点connectionString创建数据库连接
/// </summary>
public facade()
{
sqlConn = new SqlConnection(ConfigurationSettings.AppSettings[ "connectionString "]);
sqlComm.Connection = sqlConn;
}
/// <summary>
/// 自定义connectionString创建数据库连接
/// </summary>
/// <param name= "connectionString "> 连接数据库字符串 </param>
public facade(string connectionString)
{
sqlConn = new SqlConnection(connectionString);
sqlComm.Connection = sqlConn;
}
/// <summary>
/// 公共只读属性command,类型为SqlCommand
/// 为表现层引用提供入口
/// </summary>
public SqlCommand command
{
get
{
return sqlComm;
}
}
/// <summary>
/// 公共只读属性adapter,类型为SqlDataAdapter
/// 为表现层引用提供入口
/// </summary>
public SqlDataAdapter adapter
{
get
{
return sqlAdapter;
}
}
/// <summary>
/// 重新初始化SQL链接,清空command,adapter,dataset
/// </summary>
public void Reset()
{
if(this.sqlComm != null)
{
sqlComm = new SqlCommand();
sqlComm.Connection = sqlConn;
}
if(this.sqlAdapter != null)
{
sqlAdapter = new SqlDataAdapter();
}
if(this.DSguestbook != null)
{
DSguestbook = new DataSet();
}
}
/// <summary>
/// 执行查询操作,获取数据源select
/// </summary>
/// <returns> 返回DSguestbook,值类型为DataSet </returns>
public virtual DataSet Query()
{
try
{///如果连接是关闭的那么打开它
if(sqlConn.State == ConnectionState.Closed)
sqlConn.Open();
sqlAdapter.SelectCommand = command;
sqlAdapter.Fill(DSguestbook);
}
catch{}
finally
{
///关闭链接
sqlConn.Close();
}
return DSguestbook;
}
/// <summary>
/// 执行非查询操作,编辑数据源insert,update,delete
/// </summary>
/// <returns> 返回非查询操作所影响行数rows 值类型int </returns>
public virtual int NonQuery()
{
int rows=0;
try
{
if(sqlConn.State == ConnectionState.Closed)
sqlConn.Open();
rows = sqlComm.ExecuteNonQuery();
}
catch
{
}
finally
{
sqlConn.Close();
}
return rows;
}
}
}
这里是operate.cs 封装具体操作数据库方法
using System;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace course
{
/// <summary>
/// operate 的摘要说明。
/// </summary>
public class operate : facade
{
/// <summary>
/// 继承父类构造方法I 读取web.config值
/// </summary>
public operate():base(){}
/// <summary>
/// 继承父类构造方法II自定义sConnection
/// </summary>
/// <param name= "sConnection "> </param>
public operate(string sConnection):base(sConnection){}
/// <summary>
/// 在成绩表中查找学生名字
/// </summary>
/// <param name= "name "> 学生姓名必填 * </param>
public DataSet search(string name)
{
SqlParameter SPname = new SqlParameter( "@name ",SqlDbType.NVarChar,50);
SPname.Value = name;
command.CommandText = "query_student ";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(SPname);
return Query();
}
/// <summary>
/// 删除指定编号的留言
/// </summary>
/// <param name= "id "> 留言的编号id 值类型为int </param>
/// <returns> 返回操作影响的行数值类型为int </returns>
public int delete(int id)
{
SqlParameter SPid = new SqlParameter( "@id ",SqlDbType.Int,4);
SPid.Value = id;
command.CommandText = "delete_message ";
command.CommandType = CommandType.StoredProcedure;
command.Parameters.Add(SPid);
return NonQuery();
}
}
}
[解决办法]
搞不好是服务器的事吧。
[解决办法]
性能调整应该基于测试之上,而最好不是推断,找一个性能测试工具如VISUAL TRACE,或自加入跟踪代码,确定问题所在,再想办法作优化;
[解决办法]
估计是服务器的问题