开发者

How to get work ASP.NET DataSet when connection losts?

I made a basic program that connects and gets content fr开发者_StackOverflow中文版om a table via SQL, it's working normally. What I want is; when connection losts via SQL Server or internet connection, it must continue to list items that it got before connection losts instead of giving "Connection Problem Error".

Code is like this:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim strSQL As String
    Dim conn As New SqlConnection
    conn.ConnectionString = baglan()
    conn.Open()
    strSQL = "SELECT * FROM Telefon_Otomasyon"
    Dim da As New SqlDataAdapter(strSQL, conn)
    Dim dset As New DataSet()
    da.Fill(dset, "Telefon_Otomasyon")
    Dim Hepsi As DataRow
    For Each Hepsi In dset.Tables("Telefon_Otomasyon").Rows
        Response.Write(Hepsi("Arayan") & "<br />")
    Next
End Sub

Thanks!


What you can do is store your dataset in Session: (excuse my VB, i'm very rusty with it)

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Dim strSQL As String
    Dim conn As New SqlConnection
    conn.ConnectionString = baglan()
    conn.Open()
    strSQL = "SELECT * FROM Telefon_Otomasyon"
    Dim da As New SqlDataAdapter(strSQL, conn)
    Dim dset As New DataSet()

    Try
        da.Fill(dset, "Telefon_Otomasyon")
        MyDataset = dset
    Catch ex As SqlException
        dset = MyDataset
    End Try

    Dim Hepsi As DataRow
    For Each Hepsi In dset.Tables("Telefon_Otomasyon").Rows
        Response.Write(Hepsi("Arayan") & "<br />")
    Next
End Sub

Private Property MyDataset() As DataSet
    Get
        return Session("myDataset")
    End Get
    Set(ByVal value as DataSet)
        Session("myDataset") = value
    End Set
End Property

This is also a very basic example, it needs to be tidied up before you can use it in production code, i.e. you need to consider what to do if there is no dataset stored in Session (if it returns null). You may want to be a bit smarter than that and just store a specific table. Note that Session can expire though, so do some reading on it. This should be enough to steer you in the right direction.

Further note: if you want something a little less volatile than Session then you could try using the Cache instead.


I'm not sure I understand the problem here. While I'm not a VB-programmer it seems to me like once the page loads it will run the SQL query and then process that dataset.

If you reload the page and the SQL connection isn't working you will get an error that you'd need to handle somehow (not sure how VB exceptions work but I'm sure you can figure that out).

If you on the other hand mean that you want to get whatever data you can from a query that gets disconnected mid-query - I'd say that is pretty hard and only relevant for huge queries.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜