使用OleDbCommand和Access在ExecuteReader()上的异常

时间:2010-03-23 18:09:28

标签: sql vb.net ms-access

我在VB.Net

中收到了以下SQL语句的错误
'Fill in the datagrid with the info needed from the accdb file

    'to make it simple to access the db
    connstring = "Provider=Microsoft.ACE.OLEDB.12.0;Data "
    connstring += "Source=" & Application.StartupPath & "\AuctioneerSystem.accdb"
    'make the new connection
    conn = New System.Data.OleDb.OleDbConnection(connstring)


    'the sql command
    SQLString = "SELECT AllPropertyDetails.PropertyID, Street, Town, County, Acres, Quotas, ResidenceDetails, Status, HighestBid, AskingPrice FROM AllPropertyDetails "
    SQLString += "INNER JOIN Land ON AllPropertyDetails.PropertyID = Land.PropertyID "
    SQLString += "WHERE Deleted = False "
    If PriceRadioButton.Checked = True Then
        SQLString += "ORDER BY AskingPrice ASC"
    ElseIf AcresRadioButton.Checked = True Then
        SQLString += "ORDER BY Acres ASC"
    End If
    'try to open the connection
    conn.Open()
    'if the connection is open
    If ConnectionState.Open.ToString = "Open" Then
        'use the sqlstring and conn to create the command
        cmd = New System.Data.OleDb.OleDbCommand(SQLString, conn)
        'read the db and put it into dr
        dr = cmd.ExecuteReader

        If dr.HasRows Then

            'if there is rows in the db then make sure the list box is clear
            'clear the rows and columns if there is rows in the data grid
            LandDataGridView.Rows.Clear()
            LandDataGridView.Columns.Clear()
            'add the columns
            LandDataGridView.Columns.Add("PropertyNumber", "Property Number")
            LandDataGridView.Columns.Add("Address", "Address")
            LandDataGridView.Columns.Add("Acres", "No. of Acres")
            LandDataGridView.Columns.Add("Quotas", "Quotas")
            LandDataGridView.Columns.Add("Details", "Residence Details")
            LandDataGridView.Columns.Add("Status", "Status")
            LandDataGridView.Columns.Add("HighestBid", "Highest Bid")
            LandDataGridView.Columns.Add("Price", "Asking Price")
            While dr.Read
                'output the fields into the data grid
                LandDataGridView.Rows.Add( _
                dr.Item("PropertyID").ToString _
                , dr.Item("Street").ToString & " " & dr.Item("Town").ToString & ", " & dr.Item("County").ToString _
                , dr.Item("Acres").ToString _
                , dr.Item("Quota").ToString _
                , dr.Item("ResidenceDetails").ToString _
                , dr.Item("Status").ToString _
                , dr.Item("HighestBid").ToString _
                , dr.Item("AskingPrice").ToString)
            End While
        End If
        'close the data reader
        dr.Close()
    End If
    'close the connection
    conn.Close()

任何想法为何不起作用? DB和表名中的字段似乎没问题,但它不起作用:/

表格是

AllPropertyDetails
ProperyID:Number
Street: text
Town: text
County: text
Status: text
HighestBid: Currency
AskingPrice: Currency
Deleted: Boolean


Land
PropertyID: Number
Acres: Number
Quotas: Text 
ResidenceDetails: text

错误是:

System.InvalidOperationException was unhandled
  Message="An error occurred creating the form. See Exception.InnerException for details.  The error is: No value given for one or more required parameters."
  Source="AuctioneerProject"
  StackTrace:
       at AuctioneerProject.My.MyProject.MyForms.Create__Instance__[T](T Instance) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
       at AuctioneerProject.My.MyProject.MyForms.get_LandReport()
       at AuctioneerProject.ReportsMenu.LandButton_Click(Object sender, EventArgs e) in C:\Users\admin\Desktop\Auctioneers\AuctioneerProject\AuctioneerProject\ReportsMenu.vb:line 4
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at AuctioneerProject.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Data.OleDb.OleDbException
       ErrorCode=-2147217904
       Message="No value given for one or more required parameters."
       Source="Microsoft Office Access Database Engine"
       StackTrace:
            at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr)
            at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult)
            at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult)
            at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)
            at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method)
            at System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior)
            at System.Data.OleDb.OleDbCommand.ExecuteReader()
            at AuctioneerProject.LandReport.load_Land() in C:\Users\admin\Desktop\Auctioneers\AuctioneerProject\AuctioneerProject\LandReport.vb:line 37
            at AuctioneerProject.LandReport.PriceRadioButton_CheckedChanged(Object sender, EventArgs e) in C:\Users\admin\Desktop\Auctioneers\AuctioneerProject\AuctioneerProject\LandReport.vb:line 79
            at System.Windows.Forms.RadioButton.OnCheckedChanged(EventArgs e)
            at System.Windows.Forms.RadioButton.set_Checked(Boolean value)
            at AuctioneerProject.LandReport.InitializeComponent() in C:\Users\admin\Desktop\Auctioneers\AuctioneerProject\AuctioneerProject\LandReport.designer.vb:line 40
            at AuctioneerProject.LandReport..ctor() in C:\Users\admin\Desktop\Auctioneers\AuctioneerProject\AuctioneerProject\LandReport.vb:line 5
       InnerException: 

4 个答案:

答案 0 :(得分:3)

可能是这一行:

, dr.Item("Quota").ToString _

应该是

, dr.Item("Quotas").ToString _

因为Quotas(复数)是从SQL语句返回的列名吗?

答案 1 :(得分:1)

可能是您运行查询字符串的方式。从您的“错误消息”:

  

错误是:没有给出一个值   或更多必需参数。

你只显示构建字符串的代码,但错误很可能是你如何使用该字符串调用数据库,但你没有显示。

OP发布表定义后,

编辑Deleted不在任何一个表中:

WHERE Deleted = False

答案 2 :(得分:0)

也许它应该是Deleted = 0而不是Deleted = False

但我同意JonH ..你应该把它包装在一个存储过程中

答案 3 :(得分:0)

你能发布其余的ADO.NET相关代码吗?看来你没有为预期的参数发送参数值。

  

没有给出一个或多个必需参数的值。