Format of the initialization string does not confo

2019-01-06 22:11发布

问题:

I have an ASP.Net MVC application which runs fine on my local development machine. But when deployed to IIS7 gives the following error when trying to log in:

Format of the initialization string does not conform to specification starting at index 0

Most people who post this error resolve it by changing their connection string in some way. However my connection string on the local and deployed application are the same. The connection string is like this:

<add name="ApplicationServices" connectionString="Data Source=*server*\*instance*;Initial Catalog=*database*;Integrated Security=True;"
      providerName="System.Data.SqlClient" />

What is causing this error in my case?

回答1:

Format of the initialization string does not conform to specification starting at index 0

Web.config :

<connectionStrings>
    <add name="TestDataConnectionString" connectionString="Data Source=.\SQLExpress;Initial Catalog=TestData;User ID=satest;Password=satest"
     />
  </connectionStrings>

In aspx.cs Page the code must be written in the below format :

SqlConnection con = new
SqlConnection(ConfigurationManager.ConnectionStrings["TestDataConnectionString"].ToString());


回答2:

Web Deployment tool created wrong line in config when I checked Enable CodeFirst Migrations check-box.



回答3:

Check to make sure the credentials are correct for the connection string in Web.config. Mine was missing the password for the account with permissions to the database.



回答4:

I encountered the same error. In my case it was the config transform not working properly. There is an issue with config transforms when it comes to connection strings. Some reference:

  • MSBuild web.config transforms not working by drneel
  • Replaceable token issue with config ConnectionString transforms by Francis

Also one can write the code in the aspx.cs page as

using (IDbConnection dbConnection =
       new SqlConnection(ConfigurationManager.ConnectionStrings["db"].ConnectionString))
       {
           // TODO: Write SQL Stored Procedures or SQL Statements using Dapper
       }

For those who would like to find out more about Dapper.

Hope this helps.



回答5:

In my case I accidentally wrote "password:" instead of "password=" in my conn string



回答6:

If you have been using the Visual Studio Publish Wizard for deployment and checked the Execute Code First Migrations check box in Settings, a new ConnectionString is automatically added to the Server Web.config file, similar t to the 2nd line below:

<add name="LCWeb3Context" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=LCWeb3;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\LCWeb3.mdf" providerName="System.Data.SqlClient" />
<add name="LCWeb3Context_DatabasePublish" connectionString="LCWeb3Context_DatabasePublish.ConnetionString" providerName="System.Data.SqlClient" />

First, notice the added connection string contains "ConnetionString": I think it should be "ConnectionString"! But that's not the solution.

To avoid the "Format of the initialization string does not conform to specification starting at index 0" error, do the following in the Publish Wizard:

  1. In the Settings, select Configuration: Release
  2. In the Settings,don't forget to paste your Connection String in the "Remote Connection String" field
  3. In the Settings, check Execute Code First Migrations

When doing the above, the connection string added to the Server Web.config reads:

<add name="LCWeb3Context_DatabasePublish" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\LCWeb3.mdf;Initial Catalog=LCWeb3;Integrated Security=True" providerName="System.Data.SqlClient" />

and the "Format of the initialization string does not conform to specification starting at index 0" error no longer occurs.



回答7:

I has the same issue, when I use command: "Update-Database" in Package Manager Console.

To Fix, make sure set startup project to the project which you want to do update.

E.g. I got Db project and Web project, make sure set startup project on Db project, when run "Update-Database", otherwise, it will try to search inside Web project.



回答8:

The permissions on the SQL server were not correctly set up. I have now resolved this by properly setting up the server permissions.