R
R
Roman Rakzin2017-02-22 23:42:15
SQL
Roman Rakzin, 2017-02-22 23:42:15

How to connect to MS SQL Server in Asp Net Core and see the data?

I've been copy-pasting code that doesn't work for a few days now.
You need to get data from the server table and assign it to an object, array or list.
Without any Entity Framework. Just a Sql query and writing the table to a variable.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eugene, 2017-02-23
@evgshk

Include the SqlClient package in the project.json file .

{
    "dependencies": {
       ...//здесь идут ваши зависимости
        "System.Data.SqlClient": "4.1.0-*"//это нужно добавить
       ...
    }
  }
}

Save changes to file. Wait for the Project References to be redefined.
Then everything is as usual:
using (SqlConnection connection = new SqlConnection(connectionString))
{
           connection.Open();
           using (SqlCommand command = new SqlCommand("SELECT * FROM dbo.Table", connection))
           {
                  var reader = command.ExecuteReader();
                  while(reader.Read())
                  {
                        var a = reader["Column"];//инициализация значения переменной полем из таблицы БД
                  }
           }
}

Note:
connectionString = "Data Source=ServerName;Initial Catalog=DatabaseName;Persist Security Info=True;User ID=UserName;Password=Password;"))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question