发布出去的WCF服务,有一个执行存储过程向数据库中写数据的方法。用“DBHelp”类:
public class DBaseHelper
{
private string connectiostring = System.Configuration.ConfigurationManager.AppSettings["MyConnString"].ToString(); private SqlConnection connection = null; public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connectiostring);
connection.Open();
}
else if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == ConnectionState.Broken)
{
connection.Close();
connection.Dispose();
connection.Open();
}
return connection;
}
}
public int ExecuteNonQuery(string strSql, params SqlParameter[] paramter)
{
SqlCommand cmd = new SqlCommand(strSql, Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(paramter);
int count = Convert.ToInt32(cmd.ExecuteNonQuery());
connection.Close();
//connection.Dispose();
return count;
}}问题:运行存储过程,if (new DAL.DBaseHelper().ExecuteNonQuery("Usp_AddCollectDatas", parameters) > 0)
return true;
else
return false;因为服务是被持续调用的,一秒钟被调用“50”次,也就是一秒钟写入50条数据。这样反复的“打开连接”、“关闭连接”,一秒钟开关50次岂不是非常浪费资源,增大资源开销。问题一: 有别的方法,让连接一直开着吗??? 比如说“10分钟”内没有数据写入了那么就关掉。问题二:WCF服务是可以并发的对吧,多个客户端可以同时写入数据。但是,它们用的对数据库的“Session”是不一样的吧???
public class DBaseHelper
{
private string connectiostring = System.Configuration.ConfigurationManager.AppSettings["MyConnString"].ToString(); private SqlConnection connection = null; public SqlConnection Connection
{
get
{
if (connection == null)
{
connection = new SqlConnection(connectiostring);
connection.Open();
}
else if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
else if (connection.State == ConnectionState.Broken)
{
connection.Close();
connection.Dispose();
connection.Open();
}
return connection;
}
}
public int ExecuteNonQuery(string strSql, params SqlParameter[] paramter)
{
SqlCommand cmd = new SqlCommand(strSql, Connection);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddRange(paramter);
int count = Convert.ToInt32(cmd.ExecuteNonQuery());
connection.Close();
//connection.Dispose();
return count;
}}问题:运行存储过程,if (new DAL.DBaseHelper().ExecuteNonQuery("Usp_AddCollectDatas", parameters) > 0)
return true;
else
return false;因为服务是被持续调用的,一秒钟被调用“50”次,也就是一秒钟写入50条数据。这样反复的“打开连接”、“关闭连接”,一秒钟开关50次岂不是非常浪费资源,增大资源开销。问题一: 有别的方法,让连接一直开着吗??? 比如说“10分钟”内没有数据写入了那么就关掉。问题二:WCF服务是可以并发的对吧,多个客户端可以同时写入数据。但是,它们用的对数据库的“Session”是不一样的吧???
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货