c# - 配置对我的 SQL Server 的访问

标签 c# sql-server

我想将我的 SQL Server 与 C# 链接,但我收到一条错误消息

Warning
The element 'configSections' has invalid child element 'connectionStrings'. List of possible elements expected: 'section, sectionGroup'.

想知道如何解决这个问题,谢谢。

这是我的配置:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <connectionStrings>
      <add name="ConString" connectionString="Data Source=BOY-PC;Initial   Catalog=Test;Integrated Security=True"
           providerName="System.Data.sqlClient"/>
    </connectionStrings>
  </configSections>
</configuration>

最佳答案

不要将连接字符串放入配置部分 - 使用此:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
   <configSections />
   <connectionStrings>
      <add name="ConString" 
           connectionString="Data Source=BOY-PC;Initial Catalog=Test;Integrated Security=True"
           providerName="System.Data.sqlClient"/>
    </connectionStrings>
</configuration>

您的节点<connectionStrings>必须直接位于<configuration>下(不在 <configSections> 内 - 该节点用于定义新的自定义配置部分 - 此处不需要, <connectionStrings> 是系统定义的配置部分)

关于c# - 配置对我的 SQL Server 的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20514475/

相关文章:

c# - 文件系统观察器双条目

c# - 使用 Random 和 OrderBy 是一个好的洗牌算法吗?

c# - 将 XML 反序列化为对象 : XML element with xsi:nil ="true" should have null value (and not empty value) for respective property in object

SQL Server 按从第一个到最后一个的顺序从记录中减去一个值

c# - 查看可空类型是否有值的最简洁方法是什么,如果有,将该值与另一个不可空类型的值进行比较?

c# - 访问静态类的配置/设置 - Asp Core

mysql - 如何在sql中选择超过2列的计数

sql-server - 为什么在 SQL Server 中将空白字符串转换为日期会得到 1900-01-01?

sql - 将具有相同名称的列转换为正确的行

sql - 如何插入表并获取主键值?