创建ADO.NET SqlConnection时出现异常

时间:2017-12-10 19:10:36

标签: c# ado.net ado

我尝试使用ADO.NET来执行SQL。但是当我创建一个新的SqlConnection时,我得到了这个例外。我创建.config文件,但代码仍然会抛出错误。

我尝试使用ADO.NET将app.config添加到项目中,但这没有帮助。 之后我添加到配置文件configSectionsPropertyGroup但我之前有结果。

项目结构

Project structure

FeatureManager.cs:

using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MssqlManager.Dto;

namespace MssqlManager
{
    public class FeatureManager
    {
        private readonly string _connectionString;

        public FeatureManager(string dataSource, string db)
        {
            _connectionString = $@"Data Source=ServerName; Initial Catalog=DbName; Persist Security Info=True; MultipleActiveResultSets=True; Integrated Security=SSPI; Pooling = true; Max Pool Size = 250; Async = true; Connection Timeout=500";
        }

        public async Task<IEnumerable<FeatureDto>> GetFeatures()
        {
            const string sqlExpression = @"SELECT f.Id, f.Code, a.FeatureState 
                                         FROM Feature as f
                                         INNER JOIN AdminUnitFeatureState as a
                                            ON f.Id = a.FeatureId";
            var features = new List<FeatureDto>();

            using (var connection = new SqlConnection(_connectionString))
            {
                connection.Open();
                SqlCommand command = new SqlCommand(sqlExpression, connection);
                SqlDataReader reader = await command.ExecuteReaderAsync();

                if (reader.HasRows)
                {
                    while (await reader.ReadAsync())
                    {
                        var id = reader.GetValue(0);
                        var code = reader.GetValue(1);
                        var state = reader.GetValue(2);

                        features.Add(new FeatureDto
                        {
                            Id = (Guid)id,
                            Code = (string)code,
                            State = (bool)state
                        });
                    }
                }

                reader.Close();
            }

            return features;
        }
    }
}

异常堆栈:

  

未处理的异常:System.AggregateException:发生了一个或多个错误。 &GT; System.TypeInitializationException:&#39; System.Data.SqlClient.SqlConnection&#39;的类型初始值设定项。抛出异常。
  System.TypeInitializationException:&#39; System.Data.SqlClient.SqlConnectionFactory&#39;的类型初始值设定项。抛出异常。
  System.TypeInitializationException:&#39; System.Data.SqlClient.SqlPerformanceCounters&#39;的类型初始值设定项。抛出异常。
  System.Configuration.ConfigurationErrorsException:配置系统初始化失败   System.Configuration.ConfigurationErrorsException:无法识别的配置节PropertyGroup。 (C:\ Projects \ Personal \ WindowsServerManager \ WindowsServerManager \ TrainingConsoleUI \ bin \ Debug \ TrainingConsoleUI.exe.Config第5行)

     

在System.Configuration.ConfigurationSchemaErrors.ThrowIfErrors(Boolean ignoreLocal)
  在System.Configuration.BaseConfigurationRecord.ThrowIfParseErrors(ConfigurationSchemaErrors schemaErrors)          在System.Configuration.BaseConfigurationRecord.ThrowIfInitErrors()          在System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)          ---内部异常堆栈跟踪结束---          在System.Configuration.ClientConfigurationSystem.EnsureInit(String configKey)          在System.Configuration.ClientConfigurationSystem.PrepareClientConfigSystem(String sectionName)          在System.Configuration.ClientConfigurationSystem.System.Configuration.Internal.IInternalConfigSystem.GetSection(String sectionName)          ...          在System.Threading.Tasks.Task`1.get_Result()          在C:\ Projects \ Personal \ WindowsServerManager \ WindowsServerManager \ TrainingConsoleUI \ Program.cs:第18行的TrainingConsoleUI.Program.Main(String [] args)中

MssqlManager项目中的

App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <PropertyGroup>
        <TargetFramework>net471</TargetFramework>
    </PropertyGroup>
    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.1" />
    </startup>
</configuration>

0 个答案:

没有答案