首页 新闻 会员 周边 捐助

高难度问题,高手请教

0
悬赏园豆:100 [已关闭问题] 关闭于 2012-03-24 21:33
/*
* 由SharpDevelop创建。
* 用户: qinshichuan
* 日期: 2011-12-18
* 时间: 15:38
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Web;

namespace Qin.Orm
{
///<summary>
/// Description of MSSQL.
///</summary>
public class MSSQL:DB
{
public MSSQL(string connectionString)
{
this.ConnectionString = connectionString;
this.DBType= DatabasesType.MSSQL;
}

public override DbCommand GetProcDbCommand(string procName)
{
SqlConnection dbConnection = (SqlConnection) this.GetDbConnection();
SqlCommand command = new SqlCommand();
command.CommandText = procName;
command.CommandType = CommandType.StoredProcedure;
return command;
}
public override List<T> GetPagedListDesc<T>(int startIndex,int endIndex,Field primaryKey,Field onlyMark)
{
string sqlSum="select count(*) from "+primaryKey.TableName+ "where "+WhereChecker.ToSafeString(onlyMark.WhereContent);
long sum=Convert.ToInt64(DBFactory.DefaultDB.GetFirstData(sqlSum));
long end=sum-startIndex+1;
long start=sum-endIndex+1;
string cmdString="select top "+(endIndex-startIndex+1)+" * from (select top "+(end)+
" * from "+primaryKey.TableName+" where "+WhereChecker.ToSafeString(onlyMark.WhereContent)+" order by "+primaryKey.FieldName+" asc) as "+
primaryKey.TableName+" order by "+primaryKey.FieldName+" DESC";



DBFactory.CurrentSql=cmdString;
DbDataReader reader=Qin.Orm.DBFactory.DefaultDB.GetDbDataReader(cmdString);
List<T> list=new List<T>();
while (reader.Read()) {
T data=DynamicBuilder<T>.CreateBuilder(reader).Build(reader);
list.Add(data);

}
reader.Close();
return list;

}
public override List<T> GetWebPagedListDesc<T>(Field primaryKey,Field onlyMark)
{
HttpRequest Request=System.Web.HttpContext.Current.Request;
int p_start=int.Parse(Request.QueryString["p_start"]);
int p_end=int.Parse(Request.QueryString["p_end"]);
return GetPagedListDesc<T>(p_start,p_end,primaryKey,onlyMark);
}

public override object GetFirstData(string cmdString)
{
SqlCommand dbCommand = (SqlCommand) this.GetDbCommand(cmdString);
dbCommand.Connection.Open();
object firstData = dbCommand.ExecuteScalar();
dbCommand.Connection.Close();
return firstData;
}
///<summary>
/// 执行一条sql语句通过参数形式
///</summary>
///<param name="parameters">Dictionary形式的参数列表</param>
///<param name="sql">sql语句</param>
///<param name="startTransaction">是否开启事务</param>
///<param name="type">执行语句的类型 增删改查 1 2 3 4</param>
///<returns>增加则返回增加后的主键值,否则是受影响的函数</returns>
public override object ExecuteSqlByParameters(Dictionary<string,object> parameters,string sql,bool startTransaction,int type){
object newPrimaryKey=null;
if (parameters==null||parameters.Count==0) {
throw new ArgumentException("parameters 参数为空,方法InsertDataByParameters调用出错!");
}
else{
//增加
if (type==1) {
sql+="\r\n "+"select SCOPE_IDENTITY()";
}
SqlCommand procDbCommand = (SqlCommand) this.GetDbCommand(sql);
int index=1;
foreach (KeyValuePair<string,object> parameter in parameters)
{
procDbCommand.Parameters.AddWithValue("@p"+index, parameter.Value);
index++;
}

if (procDbCommand.Connection.State == ConnectionState.Closed)
{
procDbCommand.Connection.Open();
}
if (startTransaction==false) {
newPrimaryKey=procDbCommand.ExecuteScalar();
procDbCommand.Connection.Close();
}
else{
SqlTransaction dbTransaction=procDbCommand.Connection.BeginTransaction();
procDbCommand.Transaction=(SqlTransaction)dbTransaction;
try {
newPrimaryKey=procDbCommand.ExecuteScalar();
dbTransaction.Commit();

} catch (InvalidOperationException) {
dbTransaction.Rollback();
throw new InvalidOperationException("InvalidOperationException ExecuteInsertSqlByParameters");
}
finally{
procDbCommand.Connection.Close();

}
}
return newPrimaryKey;
}
}
public override DbDataReader GetDbDataReader(string cmdString)
{
SqlCommand dbCommand = (SqlCommand) this.GetDbCommand(cmdString);
dbCommand.Connection.Open();
return dbCommand.ExecuteReader(CommandBehavior.CloseConnection);
}

public override DbConnection GetDbConnection()
{
return new SqlConnection(this.ConnectionString);
}

public override DbCommand GetDbCommand(string cmdString)
{
return new SqlCommand(cmdString, (SqlConnection) this.GetDbConnection());
}

public override DataView GetDataView(string cmdString, string RowFilter, string Sort, DataViewRowState RowState)
{
return new DataView(this.GetDataTable(cmdString), RowFilter, Sort, RowState);
}

public override DataTable GetDataTable(string cmdString)
{
return this.GetDataSet(cmdString).Tables[0];
}

public override DataSet GetDataSet(string cmdString)
{
SqlCommand dbCommand = (SqlCommand) this.GetDbCommand(cmdString);
dbCommand.Connection.Open();
SqlDataAdapter adapter = new SqlDataAdapter(dbCommand);
DataSet dataSet = new DataSet();
adapter.Fill(dataSet);
dbCommand.Connection.Close();
return dataSet;
}

public override int ExecuteProc(string procName, Queue<SuperSqlParameter> superSqlParameters)
{
if (superSqlParameters.Count <= 0)
{
throw new Exception("SuperSqlParameter里面的参数为零");
}
SqlCommand procDbCommand = (SqlCommand) this.GetProcDbCommand(procName);
int num = -1;
foreach (SuperSqlParameter parameter in superSqlParameters)
{
procDbCommand.Parameters.Add(procName, (SqlDbType) parameter.ParameterDbType, parameter.Size);
}
if (procDbCommand.Connection.State == ConnectionState.Closed)
{
procDbCommand.Connection.Open();
}
num = procDbCommand.ExecuteNonQuery();
procDbCommand.Connection.Close();
return num;
}

public override int ExecuteOneSql(string cmdString)
{
SqlCommand dbCommand = (SqlCommand) this.GetDbCommand(cmdString);
dbCommand.Connection.Open();
int num = dbCommand.ExecuteNonQuery();
dbCommand.Connection.Close();
return num;
}
private string _connetionString;
public override string ConnectionString {
get {
return _connetionString;
}
set {
_connetionString=value;
}
}
}
}




这样的一个MSSQL操作类实例化为一个单例.我们可以在数据库操作时会出现数据乱串的情况吗?
仔细看完代码,有理者全给分.

[秦时明月]的主页 [秦时明月] | 小虾三级 | 园豆:738
提问于:2012-03-01 11:07
< >
分享
所有回答(2)
0

连接的打开与关闭是有问题的,打开后如果不sql语句执行错误,就无法关闭连接,然后再打开的时候就报错了。

多线程并发的情况下,就会出现你说的情况,不并发就没有问题。

az235 | 园豆:8483 (大侠五级) | 2012-03-01 12:19

连接的打开与关闭是有问题的,打开后如果不sql语句执行错误,就无法关闭连接

 

这句话让我无语.你看看我每一个

dbCommand.Connection.Close();干嘛的
支持(0) 反对(0) [秦时明月] | 园豆:738 (小虾三级) | 2012-03-01 16:54

怎么个并发情况?

支持(0) 反对(0) [秦时明月] | 园豆:738 (小虾三级) | 2012-03-01 16:55

@[秦时明月]: 假如你这里出错误了呢

  num = procDbCommand.ExecuteNonQuery();
想想后果会这么样?
dbCommand.Connection.Close();
这句还能执行吗?
支持(0) 反对(0) az235 | 园豆:8483 (大侠五级) | 2012-03-01 16:59

@[秦时明月]: 去看看多线程的资料。

支持(0) 反对(0) az235 | 园豆:8483 (大侠五级) | 2012-03-01 16:59

@az235:

可以明确的告诉你,你很out,这里出错的原因是你的逻辑出现错误.既然逻辑出错了.你的程序何以正确运行,所以这是我向来放对你等的不分青红皂白来个try catch finally不客气的愿意!

支持(0) 反对(0) [秦时明月] | 园豆:738 (小虾三级) | 2012-03-02 08:41

@az235:

错误是要报给上成使用者看的.你给隐藏了或者另类了方便差错吗?新手

支持(0) 反对(0) [秦时明月] | 园豆:738 (小虾三级) | 2012-03-02 08:43
0

如果中是单例,那错误就很明显:GetPagedListDesc 函数。 没有考虑数据量大和并发双重因素。

祥龙 | 园豆:205 (菜鸟二级) | 2012-03-02 13:38

小姐,我这一问问出很多问题来:什么

没有考虑数据量大和并发双重因素

支持(0) 反对(0) [秦时明月] | 园豆:738 (小虾三级) | 2012-03-02 13:44

我没有读写什么'公共资源了',何来的数据的情况?

支持(0) 反对(0) [秦时明月] | 园豆:738 (小虾三级) | 2012-03-02 13:48

@[秦时明月]: 你把你读取第十页的SQL 放到数据库里查询 看下所需时间吧

支持(0) 反对(0) 祥龙 | 园豆:205 (菜鸟二级) | 2012-03-02 14:09

@祥龙: 呵呵,这个根据实际出发.和数据每页大小有关,我已经应用在实际项目中,其实这个不是问题.问题是我提出的问题.速度和并发每页关系,(对公共资源的访问是否安全这是多线程应该考虑的问题,实际上我的多线程测试中没有出现资源共享导致的问题,因此我需要向大家求证)

支持(0) 反对(0) [秦时明月] | 园豆:738 (小虾三级) | 2012-03-03 08:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册