"ConnectionString": "Data Source=.,5433;User Id=sa;Password=P@ssw0rd;Initial
Catalog=AutoLot"
},
"Odbc": {
// Для localdb используйте @"Driver={ODBC Driver 17 for SQL Server};
Server=(localdb)\mssqllocaldb;Database=AutoLot;Trusted_Connection=Yes";
"ConnectionString": "Driver={ODBC Driver 17 for SQL Server};
Server=localhost,5433;
Database=AutoLot;UId=sa;Pwd=P@ssw0rd;"
},
"OleDb": {
// Для localdb используйте @"Provider=SQLNCLI11;
// Data Source=(localdb)\mssqllocaldb;Initial
Catalog=AutoLot;Integrated Security=SSPI"),
"ConnectionString": "Provider=SQLNCLI11;Data Source=.,5433;
User Id=sa;Password=P@ssw0rd;
Initial Catalog=AutoLot;"
}
}
Сообщите MSBuild о необходимости копировать файл JSON в выходной каталог при каждой компиляции. Модифицируйте файл проекта, как показано ниже:
На заметку!
ЭлементCopyToOutputDirectory чувствителен к наличию пробельных символов. Убедитесь, что пробелы вокруг слова Always отсутствуют.Теперь, располагая подходящим файлом appsettings.json
provider и connectionstring с использованием конфигурации .NET Core. Начните с обновления операторов using в верхней части файла 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;
Очистите весь код в 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("***** Fun with Data Provider Factories *****\n");
var (provider, connectionString) = GetProviderFromConfiguration();
DbProviderFactory factory = GetDbProviderFactory(provider);
// Теперь получить объект подключения.
using (DbConnection connection = factory.CreateConnection())
{
if (connection == null)
{
Console.WriteLine($"Unable to create the connection object");
// He удалось создать объект подключения
return;
}
Console.WriteLine($"Your connection object is a: {connection.GetType().Name}");
connection.ConnectionString = connectionString;
connection.Open();
// Создать объект команды.
DbCommand command = factory.CreateCommand();
if (command == null)
{
Console.WriteLine($"Unable to create the command object");
// He удалось создать объект команды
return;
}
Console.WriteLine($"Your command object is a: {command.GetType().Name}");
command.Connection = connection;
command.CommandText =
"Select i.Id, m.Name From Inventory i inner join Makes m on m.Id =
i.MakeId ";
// Вывести данные с помощью объекта чтения данных.
using (DbDataReader dataReader = command.ExecuteReader())
{
Console.WriteLine($"Your data reader object is a: