首页 > 解决方案 > 通过 CreateUserWizard 连接 SQL 数据库时出错

问题描述

我在 Web 表单中添加了“创建用户向导”并更改了 web.config 文件以更改数据库地址。我运行了这个表单并填写了字段,然后单击了提交按钮。出现此错误页面:“'/' 应用程序中的服务器错误。初始化字符串的格式不符合从索引 0 开始的规范。”。之后我检查了我输入的连接字符串,没关系。我在另一个应用程序上检查了它。这是 web.config 文件:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  
  <connectionStrings>

    <add
      name="MyConnectionString"
      connectionString="Data Source=DESKTOP-RMNK148;Initial Catalog=MySiteDatabase;User ID=MyUserID;Password=MyPassword"
      providerName="System.Data.SqlClient"/>
  </connectionStrings>
  
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
  
  <system.web>
    <membership defaultProvider="MyProvider">
      <providers >
        <clear/>
        <add
          name="MyProvider"
          type="System.Web.Security.SqlMembershipProvider"
          connectionString="MyConnectionString"
          enablePasswordRetrieval="false"
          enablePasswordReset="true" 
          requiresQuestionAndAnswer="true"
          requiresUniqueEmail="false" 
          passwordFormat="Hashed"
          applicationName="/"
         />
      </providers>
    </membership>
    
    
    <compilation debug="true" strict="false" explicit="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
  </system.web>
  
    
  <system.codedom>
    <compilers>
      <compiler language="c#;cs;csharp" extension=".cs"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701"/>
      <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb"
        type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
        warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+"/>
    </compilers>
  </system.codedom>
</configuration>

标签: asp.netcreateuserwizard

解决方案


似乎您的UserID参数在提供的连接字符串中无效。尝试User ID(使用空格)代替:

<add name="MyConnectionString"
     connectionString="Data Source=DESKTOP-RMNK148;Initial Catalog=MySiteDatabase;User ID=MyUserID;Password=MyPassword"
     providerName="System.Data.SqlClient"/>

请注意,如果连接字符串的格式不正确或包含无效字符(如使用分号的数据库密码),则始终会出现“初始化字符串的格式不符合从索引 0 开始的规范”错误。

参考:SQL Server 连接字符串

相关问题:初始化字符串的格式不符合从索引 0 开始的规范


推荐阅读