C#SqlConnection(IIS问题?)" ConnectionString属性尚未初始化"

时间:2016-07-15 07:03:29

标签: c# asp.net iis sqlconnection

我有一个很奇怪的问题。

我有一个C#/ ASP.Net应用程序,它连接到SQL数据库以检索信息。

当我在本地运行时,它完美运行。我能够检索数据,没有问题。

我尝试将其部署到网络上的IIS服务器,然后我开始收到错误消息: The ConnectionString property has not been initialized

这通常意味着变量尚未初始化/没有值,这很奇怪,因为它在本地运行。我重建,重新部署,确保我已经获得了所有文件等。

我决定尝试别的东西 - 我添加了一段代码(File.WriteAllText)。

private DataTable getStoredProcResults(string procName, string[][] parameters)
    {
        InitSql(true);

        DataTable dt = new DataTable();

        File.WriteAllText(@"C:\Temp\CONNECTION.LOG", connectionDetails.ConnectionString); //Added this
        using (SqlConnection sqlConn = new SqlConnection(connectionDetails.ConnectionString))
        {
            sqlConn.Open();
            SqlCommand sqlComm = new SqlCommand(procName, sqlConn);
            foreach (string[] param in parameters)
            {
                sqlComm.Parameters.AddWithValue(param[0], param[1]);
            }

            sqlComm.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da = new SqlDataAdapter();
            da.SelectCommand = sqlComm;



            da.Fill(dt);
            sqlConn.Dispose();
        }


        return dt;

    }

这是奇怪的地方。我能够将connectionDetails.ConnectionString写入文本文件,并且它具有正确的值。 我在本地运行时将IIS Web服务器上的值与文本文件进行了比较,并且连接字符串完全相同。

如果以下行错误说ConnectionString未初始化,如何将连接字符串写入文本文件?

这是我访问该页面时IIS服务器上的完整错误:

The ConnectionString property has not been initialized.

Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.

堆栈追踪:

   [InvalidOperationException: The ConnectionString property has not been initialized.]
   System.Data.SqlClient.SqlConnection.PermissionDemand() +6610944
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory,     TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +6605639
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +233
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +278
   System.Data.SqlClient.SqlConnection.Open() +239
   System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) +292
   System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) +487
   System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) +293
   GenericPortalWebForms.Logic.HourGlassJob.getResultSet(String sqlCommand, Boolean fromHourGlassDB) in c:\Users\cthompso\Source\Workspaces\CustomSolutions\Sema\GenericPortal\Hourglass\Web\Logic\HourGlassJob.cs:297
   GenericPortalWebForms.Logic.HourGlassJob.setJobDetails(Int64 wrID) in c:\Users\cthompso\Source\Workspaces\CustomSolutions\Sema\GenericPortal\Hourglass\Web\Logic\HourGlassJob.cs:75
   GenericPortalWebForms.Logic.HourGlassJob..ctor(DataRow hgJobRow) in c:\Users\cthompso\Source\Workspaces\CustomSolutions\Sema\GenericPortal\Hourglass\Web\Logic\HourGlassJob.cs:40
   GenericPortalWebForms.HourGlass.GetAllHGjobs() in c:\Users\cthompso\Source\Workspaces\CustomSolutions\Sema\GenericPortal\Hourglass\Web\Hourglass.aspx.cs:49
   GenericPortalWebForms.HourGlass.Page_Load(Object sender, EventArgs e) in c:\Users\cthompso\Source\Workspaces\CustomSolutions\Sema\GenericPortal\Hourglass\Web\Hourglass.aspx.cs:26
   System.Web.UI.Control.LoadRecursive() +71
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3178

=============================================== =============

编辑:对不起,我的代码在这里:

当我第一次创建对象时,我从get go初始化它。

private ConnectionStringSettings connectionDetails = ConfigManager.HourglassDb;

来自配置管理器:

public static ConnectionStringSettings HourglassDb
    {
        get { return ConfigurationManager.ConnectionStrings[ConnectionStringName.Split(',')[2]]; }
    }

来自web.config:

<add key="ConnectionStringName" value="name=BLAH1,BLAH2,BLAH3,BLAH4" />
<connectionStrings>
    <add name="BLAH3" connectionString="Data Source=SERVER;Initial Catalog=DATABASE;User ID=USER;Password=PASSWORD;" providerName="System.Data.SqlClient" />
</connectionStrings>

1 个答案:

答案 0 :(得分:0)

您是否尝试在调用connection的{​​{1}}方法之前创建命令对象。所以像这样:

Open()