帮忙看看这个数据库的访问类是否有问题?
请大家帮忙,帮忙看看这个数据库的访问类是否有问题?
如果是一个操作基本没有什么问题,但是同时有很多人使用的时候就报错。
以下是代码
public class SQLServerHelper
{
private static string conString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
public static void AddParamInCmd(SqlCommand cmd, string paramName, SqlDbType type, int size, object value)
{
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = paramName;
parameter.SqlDbType = type;
parameter.Size = size;
parameter.Value = value;
cmd.Parameters.Add(parameter);
}
private static SqlCommand BuildIntCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = BuildQueryCommand(connection, storedProcName, parameters);
command.Parameters.Add(new SqlParameter("ReturnValue", SqlDbType.Int, 4, ParameterDirection.ReturnValue, false, 0, 0, string.Empty, DataRowVersion.Default, null));
return command;
}
private static SqlCommand BuildQueryCommand(SqlConnection connection, string storedProcName, IDataParameter[] parameters)
{
SqlCommand command = new SqlCommand(storedProcName, connection);
command.CommandType = CommandType.StoredProcedure;
foreach (SqlParameter parameter in parameters)
{
command.Parameters.Add(parameter);
}
return command;
}
public static int ExecuteNonQuery(SqlTransaction transaction, string commandText, params SqlParameter[] commandParameters)
{
SqlCommand cmd = new SqlCommand();
PrepareCommand(cmd, transaction.Connection, transaction, commandText, commandParameters);
int num = cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
return num;
}
public static SqlDataReader ExecuteReader(string sqlString)
{
SqlDataReader reader2;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand command = new SqlCommand(sqlString, connection);
SqlDataReader reader = null;
try
{
connection.Open();
reader = command.ExecuteReader(CommandBehavior.CloseConnection);
reader2 = reader;
}
catch (SqlException exception)
{
connection.Close();
throw new Exception(exception.Message);
}
finally
{
if (reader == null)
{
command.Dispose();
connection.Close();
}
}
return reader2;
}
public static SqlDataReader ExecuteReader(string sqlString, params SqlParameter[] cmdParms)
{
SqlDataReader reader2;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader = null;
try
{
PrepareCommand(cmd, conn, null, sqlString, cmdParms);
reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
cmd.Parameters.Clear();
reader2 = reader;
}
catch (SqlException exception)
{
conn.Close();
throw new Exception(exception.Message);
}
finally
{
if (reader == null)
{
cmd.Dispose();
conn.Close();
}
}
return reader2;
}
public static int ExecuteSql(string sqlString)
{
int num2 = 0;
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand(sqlString, connection))
{
try
{
connection.Open();
num2 = command.ExecuteNonQuery();
}
catch (SqlException exception)
{
connection.Close();
throw new Exception(exception.Message);
}
finally
{
command.Dispose();
connection.Close();
}
}
}
return num2;
}
public static int ExecuteSql(string sqlString, string content)
{
int num2;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(sqlString, connection);
SqlParameter parameter = new SqlParameter("@content", SqlDbType.NText);
parameter.Value = content;
command.Parameters.Add(parameter);
try
{
connection.Open();
num2 = command.ExecuteNonQuery();
}
catch (SqlException exception)
{
throw new Exception(exception.Message);
}
finally
{
command.Dispose();
connection.Close();
}
}
return num2;
}
public static int ExecuteSql(string sqlString, params SqlParameter[] cmdParms)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand command = new SqlCommand())
{
try
{
PrepareCommand(command, connection, null, sqlString, cmdParms);
int num = command.ExecuteNonQuery();
command.Parameters.Clear();
return num;
}
catch (SqlException exception)
{
throw new Exception(exception.Message);
}
finally
{
command.Dispose();
connection.Close();
}
}
}
}
public static int ExecuteSqlInsertImg(string sqlString, byte[] fs)
{
int num2;
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand command = new SqlCommand(sqlString, connection);
SqlParameter parameter = new SqlParameter("@fs", SqlDbType.Image);
parameter.Value = fs;
command.Parameters.Add(parameter);
try
{
connection.Open();
num2 = command.ExecuteNonQuery();
}
catch (SqlException exception)
{
throw new Exception(exception.Message);
}
finally
{
command.Dispose();
connection.Close();
}
}
return num2;
}
public static void ExecuteSqlTran(ArrayList sqlStringList)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
SqlCommand command = new SqlCommand();
command.Connection = connection;
SqlTransaction transaction = connection.BeginTransaction();
command.Transaction = transaction;
try
{
for (int i = 0; i < sqlStringList.Count; i++)
{
string str = sqlStringList[i].ToString();
if (str.Trim().Length > 1)
{
command.CommandText = str;
command.ExecuteNonQuery();
}
}
transaction.Commit();
}
catch (SqlException exception)
{
transaction.Rollback();
throw new Exception(exception.Message);
}
finally
{
command.Dispose();
connection.Close();
}
}
}