我在jquery中有15个ajax调用,它同时执行,调用它们各自的c#函数(每个ajax都有单独的c#函数)以下是样本c#sql函数
public static DataSet Function1(int arg, string arg2, string arg3, string arg4, int arg5, int arg6)
{
DataSet ds = new DataSet();
try
{
using (SqlConnection con = DbHelper.Connect())
{
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "Procedure_Name";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@arg", arg);
cmd.Parameters.AddWithValue("@arg1", arg1);
cmd.Parameters.AddWithValue("@arg2", arg2);
cmd.Parameters.AddWithValue("@arg3", arg3);
cmd.Parameters.AddWithValue("@arg4", arg4);
// cmd.Parameters.AddWithValue("@arg5", arg5);
cmd.CommandTimeout = 0;
var dataAdaptor = new SqlDataAdapter(cmd);
dataAdaptor.Fill(ds);
}
con.Close();
}
ds.Tables[0].TableName = "Test";
}
catch (Exception ex)
{
throw ex;
}
return ds;
}
我偶尔会遇到异常
Message:Object reference not set to an instance of an object.
Source:System.Data
Target site:Boolean TryOpenInner(System.Threading.Tasks.TaskCompletionSource1[System.Data.ProviderBase.DbConnectionInternal])
Stack Trace: at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource1 retry)
at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry)
at System.Data.SqlClient.SqlConnection.Open()
任何帮助将不胜感激
using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;
{
public class DbHelper : IDisposable
{
public static string _connString = ConfigurationManager.AppSettings["ConfigurationString"];
protected static SqlConnection _conn = null;
protected SqlTransaction _trans = null;
protected bool _disposed = false;
/// <summary>
/// Sets or returns the connection string use by all instances of this class.
/// </summary>
///
public static string ConnectionString { get; set; }
/// <summary>
/// Returns the current SqlTransaction object or null if no transaction
/// is in effect.
/// </summary>
public SqlTransaction Transaction { get { return _trans; } }
/// <summary>
/// Constructor using global connection string.
/// </summary>
public DbHelper()
{
_connString = ConnectionString;
Connect();
}
/// <summary>
/// Constructure using connection string override
/// </summary>
/// <param name="connString">Connection string for this instance</param>
public DbHelper(string connString)
{
_connString = connString;
Connect();
}
// Creates a SqlConnection using the current connection string
public static SqlConnection Connect()
{
_conn = new SqlConnection(_connString);
return _conn;
}
/// <summary>
/// Constructs a SqlCommand with the given parameters. This method is normally called
/// from the other methods and not called directly. But here it is if you need access
/// to it.
/// </summary>
/// <param name="qry">SQL query or stored procedure name</param>
/// <param name="type">Type of SQL command</param>
/// <param name="args">Query arguments. Arguments should be in pairs where one is the
/// name of the parameter and the second is the value. The very last argument can
/// optionally be a SqlParameter object for specifying a custom argument type</param>
/// <returns></returns>
public SqlCommand CreateCommand(string qry, CommandType type, params object[] args)
{
SqlCommand cmd = new SqlCommand(qry, _conn);
// Associate with current transaction, if any
if (_trans != null)
cmd.Transaction = _trans;
// Set command type
cmd.CommandType = type;
// Construct SQL parameters
for (int i = 0; i < args.Length; i++)
{
if (args[i] is string && i < (args.Length - 1))
{
SqlParameter parm = new SqlParameter();
parm.ParameterName = (string)args[i];
parm.Value = args[++i];
cmd.Parameters.Add(parm);
}
else if (args[i] is SqlParameter)
{
cmd.Parameters.Add((SqlParameter)args[i]);
}
else throw new ArgumentException("Invalid number or type of arguments supplied");
}
return cmd;
}
}
}
答案 0 :(得分:2)
我认为由于您同时访问(来自15个单独的线程)静态static SqlConnection _conn
的{{1}}成员,因此存在问题。我会从DbHelper
课程中删除static SqlConnection _conn
成员,因为您正在DbHelper
进行管理。
既然你写的是这个:
Function1
在使用结束时,正在处理SqlConnection con。 在另一个线程中,可能会发生Funtion1正在调用 con.Open(); 从而产生空指针引用。