Click here to Skip to main content
15,891,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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 14:13pm

1 solution

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.
 
Share this answer
 
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?

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900