1

Can I connect to a SQL Server database without DbContext in .NET Core? I have tried this code, but it doesn't work, I have DEBUG it and I got an empty datatable

public class HomeController : Controller
{
    public IActionResult Index()
    {
        string connString = "Server=localhost; Database=StuDb; User ID=sa;Password=1;Trusted_Connection=True;";
        string query = "select * from Student";

        SqlConnection conn = new SqlConnection(connString);
        conn.Open();

        SqlCommand cmd = new SqlCommand(query, conn);

        DataTable dt = new DataTable();

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);

        conn.Close();
        da.Dispose();

        return View();
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
217LeeSin
  • 123
  • 6
  • Possible duplicate of https://stackoverflow.com/questions/46337728/how-to-use-stored-procedure-in-asp-net-core-with-boilerplate-architecture – aaron Jan 23 '19 at 00:43
  • Are you using the same connection string as with `DbContext`? – Alexander Mar 03 '19 at 14:40

0 Answers0