首页 新闻 会员 周边 捐助

“/”应用程序中的服务器错误。

0
[已解决问题] 解决于 2015-01-08 11:56
 

/”应用程序中的服务器错误。


 

未将对象引用设置到对象的实例。

说明: 执行当前 Web 请求期间,出现未经处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 

异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。

源错误: 

行 61:             TC_TrukInfo model = null;
行 62:             //执行查询	
行 63:             using (SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.LocalTrafficConnectionString, CommandType.Text, sbSql.ToString(), parameters))
行 64:             {
行 65:                 model = new TC_TrukInfo();


源文件: e:\新建文件夹 (2)\HongXingLongTraffic3DCockpit\Traffic3DCockpit\Traffic3DCockpit.DAL\TC_TrukDAL.cs    行: 63 

堆栈跟踪: 

[NullReferenceException: 未将对象引用设置到对象的实例。]
   YYSSOFT.Lib2008.Systems.CommonHelper.SqlHelper..cctor() +63

[TypeInitializationException: “YYSSOFT.Lib2008.Systems.CommonHelper.SqlHelper”的类型初始值设定项引发异常。]
   BDHChina.EAI.TrafficManage.DAL.TC_TruckDAL.GetModelBySql(Guid TrukID) in e:\新建文件夹 (2)\HongXingLongTraffic3DCockpit\Traffic3DCockpit\Traffic3DCockpit.DAL\TC_TrukDAL.cs:63
   BDHChina.EAI.TrafficManage.BLL.TC_TruckBLL.GetModel(Guid TrukID) in e:\新建文件夹 (2)\HongXingLongTraffic3DCockpit\Traffic3DCockpit\Traffic3DCockpit.BLL\TC_TruckBLL.cs:61
   WinFrom_TC_TrukDetail.IniData() in e:\新建文件夹 (2)\HongXingLongTraffic3DCockpit\Traffic3DCockpit\Traffic3DCockpit.Web\WebForm\TC_TrukDetail.aspx.cs:57
   WinFrom_TC_TrukDetail.Page_Load(Object sender, EventArgs e) in e:\新建文件夹 (2)\HongXingLongTraffic3DCockpit\Traffic3DCockpit\Traffic3DCockpit.Web\WebForm\TC_TrukDetail.aspx.cs:39
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +51
   System.Web.UI.Control.OnLoad(EventArgs e) +92
   System.Web.UI.Control.LoadRecursive() +54
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

 


版本信息: Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.17929

 

请问上面的问题怎么解决?


博学多思的主页 博学多思 | 初学一级 | 园豆:76
提问于:2015-01-08 10:22
< >
分享
最佳答案
0

问题出在SqlHelper的静态构造函数中

奖励园豆:5
dudu | 高人七级 |园豆:31030 | 2015-01-08 10:38

请问我该如何解决?步骤是什么样的?

博学多思 | 园豆:76 (初学一级) | 2015-01-08 10:38

sqlHelper在元数据里,

博学多思 | 园豆:76 (初学一级) | 2015-01-08 10:42

@博学多思: 建议检查一下web.config中是否配置了对应的连接字符串

dudu | 园豆:31030 (高人七级) | 2015-01-08 10:45

@dudu: 

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <connectionStrings>
    <add name="LocalTrafficConnectionString" connectionString="data source=172.31.132.5;User ID=readdba;Password=111aaa***;Initial Catalog=BDHChinaTraffic;Min Pool Size=20;Max Pool Size=75;" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <appSettings>
    <add key="Traffic3DCockpitImgURL" value="http://172.31.132.59:8003"/>
  </appSettings>
</configuration>

博学多思 | 园豆:76 (初学一级) | 2015-01-08 10:50

@博学多思: 连接字符串没问题,建议用ILSpy看一下YYSSOFT.Lib2008.Systems.CommonHelper.SqlHelper的静态构造函数

dudu | 园豆:31030 (高人七级) | 2015-01-08 10:54

@dudu: using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace YYSSOFT.Lib2008.Systems.CommonHelper
{
    public abstract class SqlHelper
    {
        public static readonly string LocalTransactionConnectionString = ConfigurationManager.ConnectionStrings["LocalTransactionConnectionString"].ConnectionString;
        public static readonly string LocalWorkFlowConnectionString = (ConfigurationManager.ConnectionStrings["LocalWorkFlowConnectionString"] == null) ? "" : ConfigurationManager.ConnectionStrings["LocalWorkFlowConnectionString"].ConnectionString;
        public static readonly string LocalSMSConnectionString = (ConfigurationManager.ConnectionStrings["LocalSMSConnectionString"] == null) ? "" : ConfigurationManager.ConnectionStrings["LocalSMSConnectionString"].ConnectionString;
        public static readonly string LocalTrafficConnectionString = (ConfigurationManager.ConnectionStrings["LocalTrafficConnectionString"] == null) ? "" : ConfigurationManager.ConnectionStrings["LocalTrafficConnectionString"].ConnectionString;
        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
        public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
        {
            SqlHelper.parmCache[cacheKey] = commandParameters;
        }
        public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdText, sqlConnection);
            SqlHelper.PrepareCommand(sqlDataAdapter, sqlConnection, cmdType, cmdText, commandParameters);
            DataSet dataSet = new DataSet();
            sqlDataAdapter.Fill(dataSet);
            sqlDataAdapter.SelectCommand.Parameters.Clear();
            return dataSet;
        }
        public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, string dataSetName, string tableName, params SqlParameter[] commandParameters)
        {
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdText, sqlConnection);
            SqlHelper.PrepareCommand(sqlDataAdapter, sqlConnection, cmdType, cmdText, commandParameters);
            DataSet dataSet = new DataSet(dataSetName);
            DataTable dataTable = new DataTable(tableName);
            sqlDataAdapter.Fill(dataTable);
            dataSet.Tables.Add(dataTable);
            sqlDataAdapter.SelectCommand.Parameters.Clear();
            return dataSet;
        }
        public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, connection, null, cmdType, cmdText, commandParameters);
            int result = sqlCommand.ExecuteNonQuery();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, trans.Connection, trans, cmdType, cmdText, commandParameters);
            int result = sqlCommand.ExecuteNonQuery();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            int result;
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                int num = sqlCommand.ExecuteNonQuery();
                sqlCommand.Parameters.Clear();
                result = num;
            }
            return result;
        }
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataReader result;
            try
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                sqlCommand.Parameters.Clear();
                result = sqlDataReader;
            }
            catch
            {
                sqlConnection.Close();
                throw;
            }
            return result;
        }
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, out string[] returnParaValues, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataReader result;
            try
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                returnParaValues = SqlHelper.SetReturnParaValues(commandParameters);
                sqlCommand.Parameters.Clear();
                result = sqlDataReader;
            }
            catch
            {
                sqlConnection.Close();
                throw;
            }
            return result;
        }
        public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, connection, null, cmdType, cmdText, commandParameters);
            object result = sqlCommand.ExecuteScalar();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, trans.Connection, trans, cmdType, cmdText, commandParameters);
            object result = sqlCommand.ExecuteScalar();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            object result;
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                object obj = sqlCommand.ExecuteScalar();
                sqlCommand.Parameters.Clear();
                result = obj;
            }
            return result;
        }
        public static SqlParameter[] GetCachedParameters(string cacheKey)
        {
            SqlParameter[] array = (SqlParameter[])SqlHelper.parmCache[cacheKey];
            SqlParameter[] result;
            if (array == null)
            {
                result = null;
            }
            else
            {
                SqlParameter[] array2 = new SqlParameter[array.Length];
                int i = 0;
                int num = array.Length;
                while (i < num)
                {
                    array2[i] = (SqlParameter)((ICloneable)array[i]).Clone();
                    i++;
                }
                result = array2;
            }
            return result;
        }
        private static void PrepareCommand(SqlDataAdapter cmd, SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {
            cmd.SelectCommand.CommandType = cmdType;
            if (cmdParms != null)
            {
                for (int i = 0; i < cmdParms.Length; i++)
                {
                    SqlParameter value = cmdParms[i];
                    cmd.SelectCommand.Parameters.Add(value);
                }
            }
        }
        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
            {
                cmd.Transaction = trans;
            }
            cmd.CommandType = cmdType;
            if (cmdParms != null)
            {
                for (int i = 0; i < cmdParms.Length; i++)
                {
                    SqlParameter value = cmdParms[i];
                    cmd.Parameters.Add(value);
                }
            }
        }
        private static string[] SetReturnParaValues(params SqlParameter[] commandParameters)
        {
            string[] array = new string[0];
            int i = 0;
            int num = 0;
            while (i < commandParameters.Length)
            {
                if (commandParameters[i].Direction == ParameterDirection.ReturnValue)
                {
                    array[num] = commandParameters[i].Value.ToString();
                    num++;
                }
                i++;
            }
            return array;
        }
    }
}

博学多思 | 园豆:76 (初学一级) | 2015-01-08 11:07

@博学多思: 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace YYSSOFT.Lib2008.Systems.CommonHelper
{
    public abstract class SqlHelper
    {
        public static readonly string LocalTransactionConnectionString = ConfigurationManager.ConnectionStrings["LocalTransactionConnectionString"].ConnectionString;
        public static readonly string LocalWorkFlowConnectionString = (ConfigurationManager.ConnectionStrings["LocalWorkFlowConnectionString"] == null) ? "" : ConfigurationManager.ConnectionStrings["LocalWorkFlowConnectionString"].ConnectionString;
        public static readonly string LocalSMSConnectionString = (ConfigurationManager.ConnectionStrings["LocalSMSConnectionString"] == null) ? "" : ConfigurationManager.ConnectionStrings["LocalSMSConnectionString"].ConnectionString;
        public static readonly string LocalTrafficConnectionString = (ConfigurationManager.ConnectionStrings["LocalTrafficConnectionString"] == null) ? "" : ConfigurationManager.ConnectionStrings["LocalTrafficConnectionString"].ConnectionString;
        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
        public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
        {
            SqlHelper.parmCache[cacheKey] = commandParameters;
        }
        public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdText, sqlConnection);
            SqlHelper.PrepareCommand(sqlDataAdapter, sqlConnection, cmdType, cmdText, commandParameters);
            DataSet dataSet = new DataSet();
            sqlDataAdapter.Fill(dataSet);
            sqlDataAdapter.SelectCommand.Parameters.Clear();
            return dataSet;
        }
        public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, string dataSetName, string tableName, params SqlParameter[] commandParameters)
        {
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdText, sqlConnection);
            SqlHelper.PrepareCommand(sqlDataAdapter, sqlConnection, cmdType, cmdText, commandParameters);
            DataSet dataSet = new DataSet(dataSetName);
            DataTable dataTable = new DataTable(tableName);
            sqlDataAdapter.Fill(dataTable);
            dataSet.Tables.Add(dataTable);
            sqlDataAdapter.SelectCommand.Parameters.Clear();
            return dataSet;
        }
        public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, connection, null, cmdType, cmdText, commandParameters);
            int result = sqlCommand.ExecuteNonQuery();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, trans.Connection, trans, cmdType, cmdText, commandParameters);
            int result = sqlCommand.ExecuteNonQuery();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            int result;
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                int num = sqlCommand.ExecuteNonQuery();
                sqlCommand.Parameters.Clear();
                result = num;
            }
            return result;
        }
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataReader result;
            try
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                sqlCommand.Parameters.Clear();
                result = sqlDataReader;
            }
            catch
            {
                sqlConnection.Close();
                throw;
            }
            return result;
        }
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, out string[] returnParaValues, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataReader result;
            try
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                returnParaValues = SqlHelper.SetReturnParaValues(commandParameters);
                sqlCommand.Parameters.Clear();
                result = sqlDataReader;
            }
            catch
            {
                sqlConnection.Close();
                throw;
            }
            return result;
        }
        public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, connection, null, cmdType, cmdText, commandParameters);
            object result = sqlCommand.ExecuteScalar();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, trans.Connection, trans, cmdType, cmdText, commandParameters);
            object result = sqlCommand.ExecuteScalar();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            object result;
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                object obj = sqlCommand.ExecuteScalar();
                sqlCommand.Parameters.Clear();
                result = obj;
            }
            return result;
        }
        public static SqlParameter[] GetCachedParameters(string cacheKey)
        {
            SqlParameter[] array = (SqlParameter[])SqlHelper.parmCache[cacheKey];
            SqlParameter[] result;
            if (array == null)
            {
                result = null;
            }
            else
            {
                SqlParameter[] array2 = new SqlParameter[array.Length];
                int i = 0;
                int num = array.Length;
                while (i < num)
                {
                    array2[i] = (SqlParameter)((ICloneable)array[i]).Clone();
                    i++;
                }
                result = array2;
            }
            return result;
        }
        private static void PrepareCommand(SqlDataAdapter cmd, SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {
            cmd.SelectCommand.CommandType = cmdType;
            if (cmdParms != null)
            {
                for (int i = 0; i < cmdParms.Length; i++)
                {
                    SqlParameter value = cmdParms[i];
                    cmd.SelectCommand.Parameters.Add(value);
                }
            }
        }
        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
            {
                cmd.Transaction = trans;
            }
            cmd.CommandType = cmdType;
            if (cmdParms != null)
            {
                for (int i = 0; i < cmdParms.Length; i++)
                {
                    SqlParameter value = cmdParms[i];
                    cmd.Parameters.Add(value);
                }
            }
        }
        private static string[] SetReturnParaValues(params SqlParameter[] commandParameters)
        {
            string[] array = new string[0];
            int i = 0;
            int num = 0;
            while (i < commandParameters.Length)
            {
                if (commandParameters[i].Direction == ParameterDirection.ReturnValue)
                {
                    array[num] = commandParameters[i].Value.ToString();
                    num++;
                }
                i++;
            }
            return array;
        }
    }
}
View Code
博学多思 | 园豆:76 (初学一级) | 2015-01-08 11:13

@dudu: @博学多思: 

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
namespace YYSSOFT.Lib2008.Systems.CommonHelper
{
    public abstract class SqlHelper
    {
        public static readonly string LocalTransactionConnectionString = ConfigurationManager.ConnectionStrings["LocalTransactionConnectionString"].ConnectionString;
        public static readonly string LocalWorkFlowConnectionString = (ConfigurationManager.ConnectionStrings["LocalWorkFlowConnectionString"] == null) ? "" : ConfigurationManager.ConnectionStrings["LocalWorkFlowConnectionString"].ConnectionString;
        public static readonly string LocalSMSConnectionString = (ConfigurationManager.ConnectionStrings["LocalSMSConnectionString"] == null) ? "" : ConfigurationManager.ConnectionStrings["LocalSMSConnectionString"].ConnectionString;
        public static readonly string LocalTrafficConnectionString = (ConfigurationManager.ConnectionStrings["LocalTrafficConnectionString"] == null) ? "" : ConfigurationManager.ConnectionStrings["LocalTrafficConnectionString"].ConnectionString;
        private static Hashtable parmCache = Hashtable.Synchronized(new Hashtable());
        public static void CacheParameters(string cacheKey, params SqlParameter[] commandParameters)
        {
            SqlHelper.parmCache[cacheKey] = commandParameters;
        }
        public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdText, sqlConnection);
            SqlHelper.PrepareCommand(sqlDataAdapter, sqlConnection, cmdType, cmdText, commandParameters);
            DataSet dataSet = new DataSet();
            sqlDataAdapter.Fill(dataSet);
            sqlDataAdapter.SelectCommand.Parameters.Clear();
            return dataSet;
        }
        public static DataSet ExecuteDataSet(string connectionString, CommandType cmdType, string cmdText, string dataSetName, string tableName, params SqlParameter[] commandParameters)
        {
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(cmdText, sqlConnection);
            SqlHelper.PrepareCommand(sqlDataAdapter, sqlConnection, cmdType, cmdText, commandParameters);
            DataSet dataSet = new DataSet(dataSetName);
            DataTable dataTable = new DataTable(tableName);
            sqlDataAdapter.Fill(dataTable);
            dataSet.Tables.Add(dataTable);
            sqlDataAdapter.SelectCommand.Parameters.Clear();
            return dataSet;
        }
        public static int ExecuteNonQuery(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, connection, null, cmdType, cmdText, commandParameters);
            int result = sqlCommand.ExecuteNonQuery();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static int ExecuteNonQuery(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, trans.Connection, trans, cmdType, cmdText, commandParameters);
            int result = sqlCommand.ExecuteNonQuery();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static int ExecuteNonQuery(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            int result;
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                int num = sqlCommand.ExecuteNonQuery();
                sqlCommand.Parameters.Clear();
                result = num;
            }
            return result;
        }
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataReader result;
            try
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                sqlCommand.Parameters.Clear();
                result = sqlDataReader;
            }
            catch
            {
                sqlConnection.Close();
                throw;
            }
            return result;
        }
        public static SqlDataReader ExecuteReader(string connectionString, CommandType cmdType, string cmdText, out string[] returnParaValues, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlConnection sqlConnection = new SqlConnection(connectionString);
            SqlDataReader result;
            try
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(CommandBehavior.CloseConnection);
                returnParaValues = SqlHelper.SetReturnParaValues(commandParameters);
                sqlCommand.Parameters.Clear();
                result = sqlDataReader;
            }
            catch
            {
                sqlConnection.Close();
                throw;
            }
            return result;
        }
        public static object ExecuteScalar(SqlConnection connection, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, connection, null, cmdType, cmdText, commandParameters);
            object result = sqlCommand.ExecuteScalar();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static object ExecuteScalar(SqlTransaction trans, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            SqlHelper.PrepareCommand(sqlCommand, trans.Connection, trans, cmdType, cmdText, commandParameters);
            object result = sqlCommand.ExecuteScalar();
            sqlCommand.Parameters.Clear();
            return result;
        }
        public static object ExecuteScalar(string connectionString, CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            SqlCommand sqlCommand = new SqlCommand();
            object result;
            using (SqlConnection sqlConnection = new SqlConnection(connectionString))
            {
                SqlHelper.PrepareCommand(sqlCommand, sqlConnection, null, cmdType, cmdText, commandParameters);
                object obj = sqlCommand.ExecuteScalar();
                sqlCommand.Parameters.Clear();
                result = obj;
            }
            return result;
        }
        public static SqlParameter[] GetCachedParameters(string cacheKey)
        {
            SqlParameter[] array = (SqlParameter[])SqlHelper.parmCache[cacheKey];
            SqlParameter[] result;
            if (array == null)
            {
                result = null;
            }
            else
            {
                SqlParameter[] array2 = new SqlParameter[array.Length];
                int i = 0;
                int num = array.Length;
                while (i < num)
                {
                    array2[i] = (SqlParameter)((ICloneable)array[i]).Clone();
                    i++;
                }
                result = array2;
            }
            return result;
        }
        private static void PrepareCommand(SqlDataAdapter cmd, SqlConnection conn, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {
            cmd.SelectCommand.CommandType = cmdType;
            if (cmdParms != null)
            {
                for (int i = 0; i < cmdParms.Length; i++)
                {
                    SqlParameter value = cmdParms[i];
                    cmd.SelectCommand.Parameters.Add(value);
                }
            }
        }
        private static void PrepareCommand(SqlCommand cmd, SqlConnection conn, SqlTransaction trans, CommandType cmdType, string cmdText, SqlParameter[] cmdParms)
        {
            if (conn.State != ConnectionState.Open)
            {
                conn.Open();
            }
            cmd.Connection = conn;
            cmd.CommandText = cmdText;
            if (trans != null)
            {
                cmd.Transaction = trans;
            }
            cmd.CommandType = cmdType;
            if (cmdParms != null)
            {
                for (int i = 0; i < cmdParms.Length; i++)
                {
                    SqlParameter value = cmdParms[i];
                    cmd.Parameters.Add(value);
                }
            }
        }
        private static string[] SetReturnParaValues(params SqlParameter[] commandParameters)
        {
            string[] array = new string[0];
            int i = 0;
            int num = 0;
            while (i < commandParameters.Length)
            {
                if (commandParameters[i].Direction == ParameterDirection.ReturnValue)
                {
                    array[num] = commandParameters[i].Value.ToString();
                    num++;
                }
                i++;
            }
            return array;
        }
    }
}
View Code

大侠,这个问题怎么解决?

博学多思 | 园豆:76 (初学一级) | 2015-01-08 11:14

@dudu: 

SqlDataReader rdr = SqlHelper.ExecuteReader(SqlHelper.LocalTrafficConnectionString, CommandType.Text, sbSql.ToString(), parameters)

程序运到这里,SqlHelper.LocalTrafficConnectionString为空@!

博学多思 | 园豆:76 (初学一级) | 2015-01-08 11:41

@博学多思: 

SqlHelper中读取的是LocalTransactionConnectionString连接字符串

public static readonly string LocalTransactionConnectionString = ConfigurationManager.ConnectionStrings["LocalTransactionConnectionString"].ConnectionString;

你在web.config中只配置了LocalTrafficConnectionString连接字符串。

dudu | 园豆:31030 (高人七级) | 2015-01-08 11:46

@dudu: 请问我怎么能让SqlHelper读取LocalTrafficConnectionString连接字符串

博学多思 | 园豆:76 (初学一级) | 2015-01-08 11:48

@博学多思: web.config中LocalTrafficConnectionString的配置不动,再增加一个LocalTransactionConnectionString的配置,是没有配置LocalTransactionConnectionString连接字符串引起了“未将对象引用设置到对象的实例”。

dudu | 园豆:31030 (高人七级) | 2015-01-08 11:53

@dudu: 我做的是团队项目,项目都是在vs上获取的!

博学多思 | 园豆:76 (初学一级) | 2015-01-08 11:52

@dudu: 问题已解决,十分感谢您的热情帮助!!

博学多思 | 园豆:76 (初学一级) | 2015-01-08 11:55

@dudu: @博学多思: 

item.Cells[2].Text = GlobalCode.CommonDiction["WordLicenses"][((TC_TaxiCompanyInfo)item.DataItem).WordLicenses.ToString()].ToString();

GlobalCode.CommonDiction是个字典项集合,我想知道,它里面的值是怎么加进去的?什么时候加的??

asdfgqq | 园豆:8 (初学一级) | 2015-04-03 16:54
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册