读书人

帮忙看看这个数据库的访问类是否有有关

发布时间: 2012-08-15 16:57:16 作者: rapoo

帮忙看看这个数据库的访问类是否有问题?
请大家帮忙,帮忙看看这个数据库的访问类是否有问题?
如果是一个操作基本没有什么问题,但是同时有很多人使用的时候就报错。
以下是代码


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();
}
}
}


[解决办法]
谁会费工夫给你看这个,很多人报错,先看看明白错误信息,然后就看看是不是多人同时操作了数据库,导致主键重复等问题
[解决办法]

换个DBhelper
或者把 错误报错 信息 贴出来
[解决办法]

C# code
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;} 


[解决办法]
有了 using 就不需要再 try finally 来关闭连接了,Dispose 包含了 Close 的功能
[解决办法]
把你的数据库访问类做成单例模式。否则都是静态的东西,所有人一起用,很容易出现连接对象没关闭或者被其他人给关掉的情况

你的GetSingle这个方法不知道是不是为了实现单例,但是怎么看都不对。
[解决办法]
楼主,你好歹把错误信息贴出来啊,,
这么多怎么看啊,,
你把别人当成VS编译器了?
[解决办法]
不一定是这个 SqlServerHelper 的问题,错误显示
AGu.SQLServerDAL.Member.MemberAccount.GetModel(String UserId)
这个方法里,有一个类型的实例为 null ,但是还是调用了自身的方法或属性,把这个方法贴出来看看吧
[解决办法]
错误在这里,你只判断了ds是否为空,却没有判断是否存在那个dsUser表,当返回0行记录时,不是判断Rows.Count>0,而是判断那个Tables.Count>0,因为连表都不会给你创建。

C# code
  if (ds != null)  {     if (ds.Tables["dsUser"].Rows.Count > 0)
[解决办法]
19楼说的是,ds 根本不需要判断,它一定不会是 null,另外 DataAdapter.Fill 方法有重载直接填充 DataTable ,没必要用 DataSet

读书人网 >C#

热点推荐