开发者

Database Connection (SQL Server) for ASP.NET application

开发者 https://www.devze.com 2023-04-02 00:15 出处:网络
I have an ASP.Net Application that uses a SQL Server database. I am also using ODBC to make the connection (see below). Then I l开发者_JAVA百科oad controls (many of them) with queries.

I have an ASP.Net Application that uses a SQL Server database. I am also using ODBC to make the connection (see below). Then I l开发者_JAVA百科oad controls (many of them) with queries.

Is this the correct way to do this?

Also, I need to do most of these programatically, not at design time.

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the session is started

    Session("ConnString") = "DRIVER={SQL Server};SERVER=myserver;Trusted_Connection=True;DATABASE=mydatabase"
    Session("MyConnection") = New Odbc.OdbcConnection(Session("ConnString"))

End Sub


I don't think that saving connection objects to session is very good practice (see below why)

Can't you just save the connectionstring in session and re-create the sql server connection on page_Load?

Sql connections should normally only live as long as your request lifetime (maximum), prefereably shorter. You should close a sql connection as soon as you don't need it anymore.

It's bad practice to keep one open during the entire session. As this will make your connection pool run out of available connections very fast.

Can you please explan your question a bit better?

0

精彩评论

暂无评论...
验证码 换一张
取 消