I am writing a simple console program for academic purposes. It contains the MSSQL database with one table. Through ADO.NET in it, I do simple CRUD operations. Now I want to do the same, just use .NET Core. The problem occurred with the namespace.

using System.Data.SqlClient; 

There is also a problem with the connection string:

 static string DBaddress = ConfigurationManager.ConnectionStrings["DBConection"].ConnectionString; 

Accordingly, the code that was written earlier in the standard console application

  // Connection handler for all classes; private static bool NonQuery(SqlCommand comm) { int result = -1; using (SqlConnection con = new SqlConnection(DBaddress)) { comm.Connection = con; con.Open(); comm.ExecuteNonQuery(); } return (result == 1)?true:false; } // Read from DB; public static List<Users> ReadDataFromDB() { using (SqlConnection con = new SqlConnection(DBaddress)) { List<Users> users = new List<Users>(); string select = "SELECT * FROM Users"; SqlCommand cmd = new SqlCommand(select, con); try { con.Open(); SqlDataReader reader = cmd.ExecuteReader(); while (reader.Read()) { users.Add(new Users(Convert.ToInt32(reader["Id"]), reader["FirstName"].ToString(), reader["LastName"].ToString())); } } catch { } return users; } } 

now not working.

  • one
    As I understand it, .NET Core is a different build, with code rewritten from scratch, and, of course, there are differences with the classic asp.net. Once you study for academic purposes, I advise you to study .NET Core and fix your code yourself - tym32167
  • In .Net Core2 it seems like Configuration Manager was added. In general, yes, before there was another mechanism for the connection string. In this case, I do not use ASP.NET technology, I have a classic console application. Thanks for the tip, read the tutorial on MSDN. - JDo

1 answer 1

Problem solved by installing Nuget packages

  1. System.Configuration;
  2. System.Data.SqlClient;
  3. System.Data.Common