2011-11-27 48 views
0

我正在处理连接到Microsoft Access数据库的Windows窗体项目,读取文件,执行一些数学计算,然后提供一些基本统计数据。现在我正在自学VB,我知道下面的代码可能更有效率。但是,现在我只是想让它发挥功能。在调用'Fill'之前,select命令属性尚未初始化

程序通过sql过滤需要的数据,并且有几个sql语句。我将每个sql语句的代码分隔成一个子例程,以便在表单加载时以及用户单击按钮更新时调用每个代码。该程序在表单加载时工作正常,但是,当您单击更新按钮时,在子程序Count()中的'odaCalls.Fill'上会出现以下错误:“在调用'Fill'之前,select命令属性尚未初始化。

任何帮助是极大的赞赏。我已搜查谷歌和试图找到的建议有,但还没有找到一个修复。

Option Explicit On 

Public Class Form1 

    'Count() Variables 
    Dim strSQL = "SELECT * FROM tblcallLog" 

    Dim strPath As String = "Provider=Microsoft.ACE.OLEDB.12.0 ;" _ 
     & "Data Source=C:\callLogRev2_be.accdb" 
    Dim odaCalls As New OleDb.OleDbDataAdapter(strSQL, strPath) 
    Dim datCallCount As New DataTable 
    Dim intCount As Integer = 0 

    'LiveCalls() variables 
    Dim strSQLLive As String = "SELECT * FROM tblcallLog WHERE callLive=True" 
    Dim odaCallsLive As New OleDb.OleDbDataAdapter(strSQLLive, strPath) 
    Dim datCallLive As New DataTable 
    Dim intCallLiveCount As Integer = 0 
    Dim decCallLivePct As Decimal 

    'TransferCalls() variables 
    Dim strSQLTransfered As String = _ 
     "SELECT * FROM tblcallLog WHERE callName LIKE '% transfer %' OR callName LIKE 'transfer%'" 
    Dim odaCallsTransfered As New OleDb.OleDbDataAdapter(strSQLTransfered, strPath) 
    Dim datCallTransfered As New DataTable 
    Dim intCallTransfered As Integer = 0 
    Dim decCallTranfered As Decimal 

    'SingleStaffCall() Variables 
    Dim strSQLSingleStaff As String = _ 
     "SELECT * FROM tblcallLog WHERE callName LIKE '% single %' OR callName LIKE 'single%'" 
    Dim odaCallSingleStaff As New OleDb.OleDbDataAdapter(strSQLSingleStaff, strPath) 
    Dim datCallSingleStaff As New DataTable 
    Dim intCallSingleStaff As Integer = 0 
    Dim decCallSingleStaff As Decimal 

    'SingleStaffCallsLive() Variables 
    Dim strSQLSingleStaffLive As String = _ 
     "SELECT * FROM tblcallLog WHERE callName LIKE '% single %' OR callName LIKE 'single%' AND callLive=True" 
    Dim odaCallSingleStaffLive As New OleDb.OleDbDataAdapter(strSQLSingleStaffLive, strPath) 
    Dim datCallSingleStaffLive As New DataTable 
    Dim intCallSingleStaffLive As Integer = 0 
    Dim decCallSingleStaffLive As Decimal 

    'CallToday() Variables 
    Dim strSQLToday As String = _ 
     "SELECT * FROM tblcallLog WHERE startDate = date()" 
    Dim odaCallToday As New OleDb.OleDbDataAdapter(strSQLToday, strPath) 
    Dim datCallToday As New DataTable 
    Dim intCallToday As New Integer 

    'CallTodayLive() Variables 
    Dim strSQLTodayLiveCalls As String = _ 
     "SELECT * FROM tblcallLog WHERE callLive=TRUE AND startDate = date()" 
    Dim odaCallTodayLive As New OleDb.OleDbDataAdapter(strSQLTodayLiveCalls, strPath) 
    Dim datCallTodayLive As New DataTable 
    Dim intCallTodayLive As New Integer 
    Dim decCallTodayLive As New Decimal 

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Count() 
    LiveCalls() 
    TransferCalls() 
    SingleStaffCalls() 
    SingleStaffCallsLive() 
    CallToday() 
    CallTodayLive() 
    End Sub 

    Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click 

    'Checks the database for updates when user pushes the update button on the static data tab. 
    Count() 
    LiveCalls() 
    TransferCalls() 
    SingleStaffCalls() 
    SingleStaffCallsLive() 
    CallToday() 
    CallTodayLive() 
    End Sub 

    Private Sub Count() 
    'database connect and call count 
    odaCalls.Fill(datCallCount) 
    odaCalls.Dispose() 

    intCount = datCallCount.Rows.Count 

    lblTotalCallsData.Text = intCount.ToString 
    lblTotalCallsData.Visible = True 
    End Sub 

    Private Sub LiveCalls() 
    'determine number of live calls 
    odaCallsLive.Fill(datCallLive) 
    odaCallsLive.Dispose() 

    intCallLiveCount = datCallLive.Rows.Count 
    lblcallsLiveData.Text = intCallLiveCount.ToString 
    lblcallsLiveData.Visible = True 

    decCallLivePct = intCallLiveCount/intCount 
    lblPctCallsLiveData.Text = decCallLivePct.ToString("P") 
    lblPctCallsLiveData.Visible = True 
    End Sub 

    Private Sub TransferCalls() 
    'determine the number of transfer calls 

    odaCallsTransfered.Fill(datCallTransfered) 
    odaCallsTransfered.Dispose() 

    intCallTransfered = datCallTransfered.Rows.Count 
    lblTotalTransferCallsData.Text = intCallTransfered.ToString 
    lblTotalTransferCallsData.Visible = True 

    decCallTranfered = intCallTransfered/intCount 
    lblPctTransferCallsData.Text = decCallTranfered.ToString("P") 
    lblPctTransferCallsData.Visible = True 
    End Sub 

    Private Sub SingleStaffCalls() 
    'determine the number of single staff calls and percentage of total 

    odaCallSingleStaff.Fill(datCallSingleStaff) 
    odaCallSingleStaff.Dispose() 

    intCallSingleStaff = datCallSingleStaff.Rows.Count 
    lblTotalSingleStaffCallData.Text = intCallSingleStaff.ToString 
    lblTotalSingleStaffCallData.Visible = True 
    decCallSingleStaff = intCallSingleStaff/intCount 
    lblPctSingleStaffCallsData.Text = decCallSingleStaff.ToString("P") 
    End Sub 

    Private Sub SingleStaffCallsLive() 
    'determine the percentage of single staff calls taken live 

    odaCallSingleStaffLive.Fill(datCallSingleStaffLive) 
    odaCallSingleStaffLive.Dispose() 

    intCallSingleStaffLive = datCallSingleStaffLive.Rows.Count 
    decCallSingleStaffLive = intCallSingleStaffLive/intCount 

    lblPctSingleStaffCallsLiveData.Visible = True 
    lblPctSingleStaffCallsLiveData.Text = decCallSingleStaffLive.ToString("P") 
    End Sub 

    Private Sub CallToday() 
    'determine values for todays date 
    odaCallToday.Fill(datCallToday) 
    odaCallToday.Dispose() 

    intCallToday = datCallToday.Rows.Count 
    lblTotalCallsTodayData.Text = intCallToday 
    lblTotalCallsTodayData.Visible = True 
    End Sub 

    Private Sub CallTodayLive() 
    'determine the number of live calls today 
    odaCallTodayLive.Fill(datCallTodayLive) 
    odaCallTodayLive.Dispose() 

    intCallTodayLive = datCallTodayLive.Rows.Count 
    lblCallsTodayLiveData.Text = intCallTodayLive.ToString 
    lblCallsTodayLiveData.Visible = True 

    'Statement to error correct so the database doesn't force the program to divide by zero 
    Try 
     decCallTodayLive = intCallTodayLive/intCallToday 
    Catch Exception As DivideByZeroException 
     lblPctCallsTodayLiveData.Text = "0.00%" 
     lblPctCallsTodayLiveData.Visible = True 
    Catch Exception As OverflowException 
     decCallTodayLive = 0 
    End Try 

    lblPctCallsTodayLiveData.Text = decCallTodayLive.ToString("P") 
    lblPctCallsTodayLiveData.Visible = True 
    End Sub 
End Class 
+2

这是很多代码发布您的错误。你可以尝试把它修剪到相关的部分吗? – LarsTech

回答

1

的问题是,您要填写后立即处置的dataadapters。

这就是为什么它可以在表单加载的情况下运行,但不能在这之后运行。最好的做法是创建和删除y使用它们的方法中的数据适配器,而不是在表单级别的spec上创建它们。

+1

+1使用'using'语句而不是调用Dispose也是一个好主意。这只能在OP按您的建议进行时才能完成。 –

+0

谢谢大家的建议。 – Brad

+0

不客气。请记住,如果答案已解决或帮助您解决问题,请点击答案旁边的复选框,让未来的问题访问者知道该答案适合您。 –

相关问题