加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 错误消息:初始化字符串的格式不符合从索引0开始的规范

发布时间:2020-12-15 23:22:50 所属栏目:百科 来源:网络整理
导读:我搜索了但是没有找到我错误的解决方案. 我正在使用VS 2013和SQL Server 2014. 以下是我的连接字符串: using (SqlConnection sqlConnection = new SqlConnection("cnInvestTracker")){} 我的web.config是: connectionStrings add name="cnInvestTracker" c
我搜索了但是没有找到我错误的解决方案.
我正在使用VS 2013和SQL Server 2014.

以下是我的连接字符串:

using (SqlConnection sqlConnection = new SqlConnection("cnInvestTracker"))
{

}

我的web.config是:

<connectionStrings>
    <add name="cnInvestTracker" 
          connectionString="Data Source=localhost;Initial Catalog=InvestTracker;Integrated Security=True"
          providerName="System.Data.SqlClient" />
</connectionStrings>

当代码执行using行时,我收到一条错误消息.错误消息是:

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

是什么导致错误?

解决方法

值“cnInvestTracker”本身不是有效的连接字符串.这是你在这里尝试使用的:

new SqlConnection("cnInvestTracker")

该构造函数不需要连接字符串的名称,它需要the connection string itself:

new SqlConnection(ConfigurationManager.ConnectionStrings["cnInvestTracker"].ConnectionString)

(您可能必须添加对System.Configuration的引用,并且您可能希望添加一些错误检查以确保在尝试引用该名称之前存在该名称的连接字符串.)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读