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

本文关键字:属性 ConnectionString 初始化 SqlConnection IIS 问题 | 更新日期: 2023-09-27 17:57:13

我遇到了一个很奇怪的问题。

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

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

我尝试将其部署到网络上的 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

====

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

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

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

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>

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

在调用connectionOpen()方法之前,您是否尝试过创建命令对象。所以像这样:

SqlCommand sqlComm = new SqlCommand(procName, sqlConn);
sqlConn.Open();