修改代码,使datbase服务器不会崩溃 [英] Modify code so the datbase server will not crash

查看:141
本文介绍了修改代码,使datbase服务器不会崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何修改代码,以便在尝试访问数据库服务器时如果数据库服务器不可用,该代码不会崩溃?如果发生这种情况,那么必须有一种适当的方法来处理该错误.我不知道将Try..Catch过程放在哪里.

How do you modify code so that it will not crash if the database server is unavailable when you attempt to access it? If this happens then there must be an appropriate way to handle the error. I do not know where to put the Try..Catch procedure.



Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim db As New DBWrapper
        Dim l As ArrayList



        If Not IsPostBack Then
            Dim o As New Order
            o = Session("CurrentOrder")
            'Code so the program will not crash if the database server is unavailable when trying to access it.
            ' Try
            'Do something
            'Catch ex As Exception
            'Handle exception by displaying a message box
            'msgbox("Error happened because the database is not available" & ex.tostring())
            'Finally
            'dbwrapper.End
            ' End Try
            l = db.GetCustomersForLookup
            lstCustomer.DataSource = l

            lstCustomer.DataValueField = "Value"
            lstCustomer.DataTextField = "Description"
            lstCustomer.DataBind()


            If o Is Nothing Then

                calOrderDate.SelectedDate = Date.Today

            Else
                calOrderDate.SelectedDate = o.OrderDate


            End If

            SetFormToInitialState()
        End If


    End Sub

推荐答案

使用错误处理,例如try ... catch

只需执行此操作
Use error handling such as try...catch

Simply just do this
try
 <some code="" to="" use="" with="" database="">
catch (e as exception)
 <some code="" to="" do="" if="" above="" for="" example="">
 msgbox("Error happened at "+e.toString())
end try</some></some>


您可以将try-catch包裹在可能会出错.

在这里,在您的代码中,尝试连接到DB并获取数据的行是发生错误的可能性很高的地方之一.
将try-catch包裹在以下行中:
You wrap try-catch around the code that has potential of getting an error.

Here, in your code the line that tries to connect to DB and get back data is one of those places where probability of having an error is high.
Wrap your try-catch around the following line:
l = db.GetCustomersForLookup


这篇关于修改代码,使datbase服务器不会崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆