初始化字符串的格式不符合从索引 0 开始的规范

发布于 2024-10-20 19:19:22 字数 446 浏览 6 评论 0原文

我有一个 ASP.Net MVC 应用程序,它在我的本地开发计算机上运行良好。但部署到IIS7后尝试登录时出现以下错误:

初始化字符串格式不符合规范 从索引 0 开始

大多数发布此错误的人通过以某种方式更改连接字符串来解决该错误。但是,我在本地和部署的应用程序上的连接字符串是相同的。连接字符串是这样的:

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

在我的例子中是什么导致了这个错误?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

冷情妓 2024-10-27 19:19:22

初始化字符串格式不符合规范
从索引 0 开始

Web.config:

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

在 aspx.cs 页面中,代码必须按以下格式编写:

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

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());
通知家属抬走 2024-10-27 19:19:22

当我选中启用 CodeFirst 迁移复选框时,Web 部署工具在配置中创建了错误的行。

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

挽清梦 2024-10-27 19:19:22

就我而言,我不小心在 conn 字符串中写了“password:”而不是“password=”

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

半透明的墙 2024-10-27 19:19:22

检查以确保 Web.config 中连接字符串的凭据正确。我的缺少具有数据库权限的帐户的密码。

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.

回忆那么伤 2024-10-27 19:19:22

我遇到了同样的错误。就我而言,这是配置转换无法正常工作。
当涉及到连接字符串时,配置转换存在问题。
一些参考:

另外,您还可以在 aspx.cs 页面中编写代码,

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

对于那些想了解有关 简洁

希望这有帮助。

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:

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.

冷清清 2024-10-27 19:19:22

如果您一直使用 Visual Studio 发布向导进行部署,并在“设置”中选中了执行代码优先迁移复选框,则新的 ConnectionString 会自动添加到 Server Web.config 文件中,类似于第二个下面一行:

<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" />

首先,注意添加的连接字符串包含“ConnetionString”:我认为它应该是“ConnectionString”!但这不是解决方案。

要避免出现“初始化字符串的格式不符合从索引 0 开始的规范”错误,请在发布向导中执行以下操作:

  1. 设置中,选择配置:发布
  2. 设置中,不要忘记将您的连接字符串粘贴到
    远程连接字符串”字段
  3. 设置中,选中执行代码优先迁移

执行上述操作时,将连接字符串添加到服务器Web.config读取:

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

并且不再出现“初始化字符串的格式不符合从索引0开始的规范”错误。

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.

爱冒险 2024-10-27 19:19:22

当我在包管理器控制台中使用命令:“更新数据库”时,我遇到了同样的问题。

要修复,请确保将启动项目设置为您要更新的项目。

例如,我有Db项目和Web项目,请确保在Db项目上设置启动项目,当运行“Update-Database”时,否则,它将尝试在Web项目内搜索。

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.

青衫负雪 2024-10-27 19:19:22

SQL Server 上的权限设置不正确。我现在已经通过正确设置服务器权限解决了这个问题。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文