Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 对象引用未设置为con.Open()上对象的实例;_C#_Jquery - Fatal编程技术网

C# 对象引用未设置为con.Open()上对象的实例;

C# 对象引用未设置为con.Open()上对象的实例;,c#,jquery,C#,Jquery,我在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 {

我在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;
        }
}
}
使用系统;
使用系统数据;
使用System.Data.Common;
使用系统配置;
使用System.Data.SqlClient;
{
公共类DbHelper:IDisposable
{
公共静态字符串_connString=ConfigurationManager.AppSettings[“ConfigurationString”];
受保护的静态SqlConnection _conn=null;
受保护的SqlTransaction _trans=null;
受保护的bool\u disposed=false;
/// 
///设置或返回此类的所有实例使用的连接字符串。
/// 
/// 
公共静态字符串连接字符串{get;set;}
/// 
///返回当前SqlTransaction对象,如果没有事务,则返回null
///这是有效的。
/// 
公共SqlTransaction事务{get{return{u trans;}}
/// 
///使用全局连接字符串的构造函数。
/// 
公共DbHelper()
{
_connString=连接字符串;
Connect();
}
/// 
///使用连接字符串重写的构造
/// 
///此实例的连接字符串
公共DbHelper(字符串connString)
{
_connString=connString;
Connect();
}
//使用当前连接字符串创建SqlConnection
公共静态SqlConnection Connect()
{
_conn=新的SqlConnection(\u connString);
返回连接;
}
/// 
///用给定的参数构造一个SqlCommand。此方法通常被调用
///从其他方法中调用,而不是直接调用。但是如果需要访问,则在这里
///去吧。
/// 
///SQL查询或存储过程名称
///SQL命令的类型
///查询参数。参数应该成对出现,其中一个是
///参数的名称,第二个是值。最后一个参数可以
///也可以是用于指定自定义参数类型的SqlParameter对象
/// 
公共SqlCommand CreateCommand(字符串qry,命令类型,参数对象[]args)
{
SqlCommand cmd=新的SqlCommand(qry,_conn);
//与当前交易记录关联(如果有)
如果(_trans!=null)
cmd.Transaction=\u trans;
//设置命令类型
cmd.CommandType=type;
//构造SQL参数
for(int i=0;i
我认为这是一个问题,因为您同时访问(从15个单独的线程)静态
DbHelper
static SqlConnection\u conn
成员。我将从
DbHelper
类中删除
static SqlConnection\u conn
成员,因为您正在
Function1
中管理它。 既然你在写这篇文章:

using (SqlConnection con = DbHelper.Connect())
{
   // code stuff here...
}
在使用结束时,将释放SqlConnection con。 在另一个线程中,Function1可能只是在调用 con.Open();
从而导致空指针引用。

也许您想向我们展示
DbHelper.Connect
。它似乎返回空侧注释:不要使用
throw-ex。它正在从异常中删除有用的信息。如果在
catch
块中没有执行任何有用的操作,并且仍然打算重新抛出异常,那么首先不要捕获它。您确定您的连接字符串有效吗?