1

VB.NET 和 ASMX Web 服务。在插入操作后,我仍然遇到同样的问题,即没有数据。

这是我的插入代码:

Try
    'create a MySqlCommand to represent the query
    If sqlConn.State = ConnectionState.Closed Then
        sqlConn = New MySqlConnection(conStr)
        sqlConn.Open()
    End If

    Dim myCommand As MySqlCommand = New MySqlCommand(strQuery, sqlConn)
    If myCommand.ExecuteNonQuery() <> 0 Then
        Result = True
    End If
Catch ex As Exception
    Throw New ApplicationException("Error-luckie's server 2 " & ex.Message)
Finally
    sqlConn.Close()
    sqlConn.Dispose()
End Try

strQuery 将是一个简单的插入语句。

将数据作为数据集检索的代码如下

Public Function ExecuteQuery(ByVal strQuery As String) As DataSet
    Dim ds As DataSet = New DataSet 'create a DataSet to hold the results

    Try
        'create a MySqlCommand to represent the query
        If sqlConn.State = ConnectionState.Closed Then
            sqlConn = New MySqlConnection(conStr)
            sqlConn.Open()
        End If

        Dim sqlcmd As MySqlCommand = sqlConn.CreateCommand()
        sqlcmd.CommandType = CommandType.Text
        sqlcmd.CommandText = strQuery

        'create a MySqlDataAdapter object
        Dim sqlDa As MySqlDataAdapter = New MySqlDataAdapter
        sqlDa.SelectCommand = sqlcmd

        Try
            'fill the DataSet using the DataAdapter
            sqlDa.Fill(ds, "Results")
        Catch ex As Exception
            Throw New ApplicationException("Error-luckie's server 1 " & ex.Message)
        End Try

    Catch ex As Exception
        Throw New ApplicationException("Error-luckie's server 2 " & ex.Message)
    Finally
        sqlConn.Close()
        sqlConn.Dispose()
    End Try

    Return ds
End Function

I am Inserting data from one thread and retrieving data from another (not the same instance of connection is used).

This works perfect if I retrieve data after a 15 minutes gap.

But I want to get the result at once. Please help.

4

1 回答 1

0

我会改变方法如下

Public Function ExecuteQuery(strQuery As String) As DataSet
    Dim ds As New DataSet()

    Try
        Using con = New MySqlConnection(conStr)  'using statements...
            Using cmd = New MySqlCommand(strQuery, con) 'using statements...
                Using sqlDa = New MySqlDataAdapter(cmd) 'using statements...
                    sqlDa.Fill(ds, "Results")
                End Using
            End Using
        End Using
    Catch ex As Exception
        Throw New ApplicationException("Error-luckie's server 1 " + ex.Message)
    End Try

    Return ds
End Function

Public Function ExecuteNonQuery(strQuery As String) As Boolean
    Try
        Using con = New MySqlConnection(conStr) 'using statements...
            Using cmd = New MySqlCommand(strQuery, con) 'using statements...
                con.Open()
                If cmd.ExecuteNonQuery() > 0 Then ' I have change the condition 
                    Return True
                End If
            End Using

        End Using
    Catch ex As Exception
        Throw New ApplicationException("Error-luckie's server 2 " + ex.Message)
    End Try
    Return False
End Function
于 2013-07-12T04:29:11.863 回答