错误“对象引用未设置为对象的实例”。用于SqlConnection

时间:2015-06-18 13:17:01

标签: c# sql sqlconnection

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["TropicalConnectionString"].ConnectionString);  

它说

  

对象引用未设置为对象的实例。

为了检查错误,我给了app.config,我想知道是否有可能为初始目录等命名错误?

这是我的app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="TropicalConnectionString" 
             connectionString="Data Source = JIRAN-PC; Initial Catalog = TropicalServer; Integrated Security = True"
             providerName="System.Data.SqlClient" />
        <add name="TropicalServiceWinApp.Properties.Settings.TropicalConnectionString"
             connectionString="Data Source=JIRAN-PC;Initial Catalog=TropicalServer;Integrated Security=True"
             providerName="System.Data.SqlClient" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_ITropicalService" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:5483/TropicalService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_ITropicalService"
                contract="TropicalServiceRef.ITropicalService" name="BasicHttpBinding_ITropicalService" />
        </client>
    </system.serviceModel>
</configuration>

1 个答案:

答案 0 :(得分:0)

试试这个:

  1. 在解决方案资源管理器中选择App.Config文件
  2. 在属性窗口中,选择复制到输出目录以始终复制。
  3. 现在构建应用程序并重试。