Добавить в цитаты Настройки чтения

Страница 389 из 642

    // Для localdb используйте @"Driver={ODBC Driver 17 for SQL Server};

    Server=(localdb)mssqllocaldb;Database=AutoLot;Trusted_Co

      "Co

    Server=localhost,5433;

      Database=AutoLot;UId=sa;Pwd=P@ssw0rd;"

  },

  "OleDb": {

    // Для localdb используйте @"Provider=SQLNCLI11;

    // Data Source=(localdb)mssqllocaldb;Initial

     Catalog=AutoLot;Integrated Security=SSPI"),

     "Co

       User Id=sa;Password=P@ssw0rd;

     Initial Catalog=AutoLot;"

  }

}

Сообщите MSBuild о необходимости копировать файл JSON в выходной каталог при каждой компиляции. Модифицируйте файл проекта, как показано ниже:

<ItemGroup>

  <None Update="appsettings.json">

<b>    &lt;CopyToOutputDirectory&gt;Always&lt;/CopyToOutputDirectory&gt;</b>

  &lt;/None&gt;

&lt;/ItemGroup&gt;

На заметку! Элемент CopyToOutputDirectory чувствителен к наличию пробельных символов. Убедитесь, что пробелы вокруг слова Always отсутствуют.

Теперь, располагая подходящим файлом appsettings.json, вы можете читать значения provider и co

using System;

using System.Data.Common;

using System.Data.Odbc;

#if PC

  using System.Data.OleDb;

#endif

using System.IO;

using Microsoft.Data.SqlClient;

using Microsoft.Extensions.Configuration;

Очистите весь код в Program.cs и добавьте взамен следующий код:

using System;

using System.Data.Common;

using System.Data.Odbc;

#if PC

  using System.Data.OleDb;

#endif

using System.IO;

using Microsoft.Data.SqlClient;

using Microsoft.Extensions.Configuration;

using DataProviderFactory;

Console.WriteLine(&quot;***** Fun with Data Provider Factories *****n&quot;);

var (provider, co

DbProviderFactory factory = GetDbProviderFactory(provider);

// Теперь получить объект подключения.

using (DbCo

{

  if (co

  {

    Console.WriteLine($&quot;Unable to create the co

                  // He удалось создать объект подключения

    return;

  }

  Console.WriteLine($&quot;Your co

  co

  co

  // Создать объект команды.

  DbCommand command = factory.CreateCommand();

  if (command == null)

  {

    Console.WriteLine($&quot;Unable to create the command object&quot;);

                  // He удалось создать объект команды

    return;

  }

    Console.WriteLine($&quot;Your command object is a: {command.GetType().Name}&quot;);

  command.Co

  command.CommandText =

    &quot;Select i.Id, m.Name From Inventory i i

      i.MakeId &quot;;

  // Вывести данные с помощью объекта чтения данных.

  using (DbDataReader dataReader = command.ExecuteReader())

  {

    Console.WriteLine($&quot;Your data reader object is a:

      {dataReader.GetType().Name}&quot;);

    Console.WriteLine(&quot;n***** Current Inventory *****&quot;);

    while (dataReader.Read())

    {

      Console.WriteLine($&quot;-&gt; Car #{dataReader[&quot;Id&quot;]} is a

        {dataReader[&quot;Name&quot;]}.&quot;);

    }