Click here to Skip to main content
14,383,854 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello.
In razor web pages when youd write Database.Open("dbname"); you open a connection with the database. But is that connection closing automaticlly or does it stay open?

What I have tried:

I am using razor pages and its helpers to create a project site but i do not want to use any external c# classes.
Thank you.
Posted
Updated 1-May-19 15:13pm

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You should use entity framework and inject your database into the code. That way, you're not managing state of your database connections at all.

But, if you do it manually, you should create your connection in a using block. Then your IDisposable on whatever class you use, should disconnect for you. If you don't do that, there's no reason to think your controller class will find objects in your method and clean them up.

If you're using EF, this is worth reading:

Database Connection Hazards with Entity Framework - Brent Ozar Unlimited®[^]

Basically, entity framework goes ahead and tries to manage connections for you, so closing connections is basically fighting it.
   
Comments
simple world 1-May-19 20:18pm
   
EF is independent?
Because as i said i am working only with razor pages not with any mvc or core.
And i dont have any controllers at all.
All my files are purely .cshtml files.
Christian Graus 1-May-19 20:20pm
   
Are you using entity framework? You could be. If not, what are you using for a DB connection?
simple world 1-May-19 20:23pm
   
Have a look at my AddProducts.cshtml page and you will understand.

@{
Layout = "~/_Admin/_SiteLayout.cshtml";
Page.Title = "Προσθήκη Προιόντων";
var db = Database.Open("rentit");


WebImage photo = null;
var newFileName = "";
var imagePath = "";


if (IsPost)
{
//Getting TextBox value in string
string ProdName = Request.Form["prodname"];
string Cost = Request.Form["cost"];
string Description = Request.Form["desc"];
string Details = Request.Form["details"];
string Class = Request.Form["class"];
string Fuel = Request.Form["fuel"];
string Doors = Request.Form["doors"];
string GearBox = Request.Form["gearbox"];



photo = WebImage.GetImageFromRequest();
if (photo != null)
{
newFileName ="_" +
Path.GetFileName(photo.FileName);
imagePath = @"/images\" + newFileName;

photo.Save(@"~\" + imagePath);
}



var sqlinsert = "INSERT INTO Products (ProdName,Cost,Description,Details,Class,Fuel,Doors,GearBox,ImagePath) Values(@0,@1,@2,@3,@4,@5,@6,@7,@8)";
db.Execute(sqlinsert, ProdName.toGreeklish(), Cost, Description, Details, Class, Fuel, Doors, GearBox, newFileName);

Response.Redirect("/_Admin/Products.cshtml");
}

}

And in every page that i need to RUID something in my database i am using the Database.Open(); in every file
Christian Graus 1-May-19 20:26pm
   
Oh, you're doing everything in the CSHTML? That's pretty messy. You have no C# classes? Why would you do it this way?

Yes, if you're creating an old school, 2006 DB connection, it won't close if you don't close it.
simple world 1-May-19 20:30pm
   
Well i just finished my internship so i decided to start learning ASP.NET MVC but because i am a total beginner even in c# i thought i would do something like this just as a test project and then slowly build in it the classes that i need just for education purposes.
But i dont know how to close the db connection and there is almost zero information about razor pages online.
So what do you suggest?
Christian Graus 1-May-19 20:36pm
   
You're going backwards. You're doing it the hard way. Use MVC, learn how to use Razor in sensible ways, and don't learn how to use it to do things no one does :)

If your connection class has a Close method, you would call it :) Ideally, if it's IDisposable, you'd use it in a using statement
simple world 1-May-19 20:38pm
   
Well i guess i still have a lot to learn. Thanks a lot :)
Christian Graus 1-May-19 20:40pm
   
No worries. These frameworks have so many bits, learning some basic razor first is not a terrible idea, but you've moved to a level of complexity where some of your code needs to live in a C# class
simple world 1-May-19 20:42pm
   
Yeah i know.
First thing im gonna do is read a few pdfs to learn some c# first.
I accepted your answer as solution.
Christian Graus 1-May-19 21:00pm
   
Good luck :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100