帮忙看看这个数据库的访问类是否有问题?
请大家帮忙,帮忙看看这个数据库的访问类是否有问题?
如果是一个操作基本没有什么问题,但是同时有很多人使用的时候就报错。
以下是代码
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();
}
}
}
如果是一个操作基本没有什么问题,但是同时有很多人使用的时候就报错。
以下是代码
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();
}
}
}
...全文
相关推荐
JDBC访问MySQL数据库.docx
导入驱动包,加载具体的驱动类 导包: o新建一个Java Project文件,在此文件夹下新建Folder文件命名lib(此文件夹下放一些导入的包) o将mysql-connector-java-xxxx.jar拖进来,右键Build Path → Add to Build Path;(这里我用的是mysql-connector-java-8.0.20.jar) 加载具体的驱动类:
在数据库‘master’中拒绝CREATE DATABASE权限问题的解决方法
今天使用Linq To SQL,要求判断数据库是否存在,不存在是自动创建数据库。
Oracle 数据库(Database, DB)
数据库基本概念 数据库(Database, DB) 数据库管理系统(Database Management System, DBMS) 数据库管理员( Database Administrator, DBA) 数据库系统( Database System, DBS ) 关系型数据库 E-R 模型(Entry-Relation)
idea中database不显示问题的解决
主要介绍了idea中database不显示问题的解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
QT连接数据库的QSqlDatabase类
详解QT数据库类,详细讲解了QT连接Access的具体操作,以及个人的一些理解


