这让我发疯,我希望有人可以帮助我。我正在使用Visual Studio Express 2012并在VB中创建Windows窗体应用程序以访问MS Access 2010数据库中的数据。这是使用ODBC Microsoft Access驱动程序完成的。我最初将所有SELECT,UPDATE和INSERT语句构建为字符串。我回去并将它们更改为基于参数的命令来处理特殊字符并消除数据库插入威胁。除一个查询外,一切正常。
以下是给我提问的代码:
Dim cmdSQLQuery As Odbc.OdbcCommand
cmdSQLQuery = SCCResources.dbConnSCCResources.CreateCommand
cmdSQLQuery.CommandText = "SELECT tblRepairRequests.ID as [Repair Request ID #], tblRepairRequests.RequestType as [Request Type], tblRepairRequests.PositionLocation as [Position #], tblRepairRequests.DateTimeSubmitted as [Date/Time Submitted], tblRepairRequests.DateTimeClosed as [Date/Time Closed], tblRepairRequests.Status, tblRepairRequests.SubmitterFirstName as [Submitter's First Name], tblRepairRequests.SubmitterLastName as [Submitter's Last Name], tblRepairRequests.SubmitterIDNumber as [Submitter's ID #], tblRepairRequests.SupervisorFirstName as [Supervisor First Name], tblRepairRequests.SupervisorLastName as [Supervisor Last Name], tblRepairRequests.SupervisorIDNumber as [Supervisor ID #], tblRepairRequests.ProblemDescription as [Problem Description], tblRepairRequests.DuplicateParent, tblRepairRequests.DuplicateChild, tblRepairRequests.Reopened FROM tblRepairRequests WHERE tblRepairRequests.ID = ?; "
cmdSQLQuery.Parameters.Clear()
cmdSQLQuery.Parameters.AddWithValue("ID", ID.Text)
cmdSQLQuery.CommandType = CommandType.Text
Dim daSearchResults As New System.Data.Odbc.OdbcDataAdapter(cmdSQLQuery)
Dim dsSearchResults As New System.Data.DataSet
Dim dvSearchResults As New System.Data.DataView
daSearchResults.Fill(dsSearchResults)
dvSearchResults = dsSearchResults.Tables(0).DefaultView
With dgvSearchResults
.DataSource = dvSearchResults
.Columns("Status").Visible = True
.Columns("DuplicateParent").Visible = False
.Columns("DuplicateChild").Visible = False
.Columns("Reopened").Visible = False
.Columns("Repair Request ID #").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Request Type").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Status").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Submitter's First Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Submitter's Last Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Submitter's ID #").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Date/Time Submitted").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Date/Time Submitted").DefaultCellStyle.Format = "MM/dd/yyyy HH:mm:ss"
.Columns("Supervisor First Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Supervisor Last Name").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Supervisor ID #").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Position #").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Problem Description").Width = 300
.Columns("Date/Time Closed").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
.Columns("Date/Time Closed").DefaultCellStyle.Format = "MM/dd/yyyy HH:mm:ss"
.ReadOnly = True
.Visible = True
End With
在 daSearchResults.Fill(dsSearchResults)行,我收到以下错误:错误[07002] [Microsoft] [ODBC Microsoft Access Driver]参数太少。预计1。 我知道命名参数不完全支持,换句话说,能够以任何顺序添加。我正在使用它们以便于阅读,我正在以正确的顺序添加它们。我在命令上的监视显示了那里的参数。如果我使用以下字符串查询,它可以正常工作:
strSQLQuery = "SELECT DISTINCT tblRepairRequests.ID as [Repair Request ID #], tblRepairRequests.RequestType as [Request Type], tblRepairRequests.PositionLocation as [Position #], tblRepairRequests.DateTimeSubmitted as [Date/Time Submitted], tblRepairRequests.DateTimeClosed as [Date/Time Closed], tblRepairRequests.Status, tblRepairRequests.SubmitterFirstName as [Submitter's First Name], tblRepairRequests.SubmitterLastName as [Submitter's Last Name], tblRepairRequests.SubmitterIDNumber as [Submitter's ID #], tblRepairRequests.SupervisorFirstName as [Supervisor First Name], tblRepairRequests.SupervisorLastName as [Supervisor Last Name], tblRepairRequests.SupervisorIDNumber as [Supervisor ID #], tblRepairRequests.ProblemDescription as [Problem Description], tblRepairRequests.DuplicateParent, tblRepairRequests.DuplicateChild, tblRepairRequests.Reopened FROM tblRepairRequests WHERE tblRepairRequests.ID = 3;"
我不知道出了什么问题,特别是因为这个类似的代码在同一个数据库的同一个程序中的另一个表单上运行:
If SCCResources.CheckDBConnStateOpen(1) Then
Dim cmdSubmitRepairRequest As Odbc.OdbcCommand
cmdSubmitRepairRequest = SCCResources.dbConnSCCResources.CreateCommand
With cmdSubmitRepairRequest
.CommandText = "INSERT INTO tblRepairRequests([RequestType], [SubmitterFirstName], [SubmitterLastName], [SubmitterIDNumber], [DateTimeSubmitted], [SupervisorFirstName], [SupervisorLastName], [SupervisorIDNumber], [PositionLocation], [ProblemDescription], [Status], [DuplicateParent], [DuplicateChild], [Reopened]) VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"
.Parameters.Clear()
.Parameters.AddWithValue("RequestType", cbRequestType.SelectedValue)
.Parameters.AddWithValue("SubmitterFirstName", dtReportingUserList.Rows(cbPersonReporting.SelectedIndex).Item("FirstName"))
.Parameters.AddWithValue("SubmitterLastName", dtReportingUserList.Rows(cbPersonReporting.SelectedIndex).Item("LastName"))
.Parameters.AddWithValue("SubmitterIDNumber", dtReportingUserList.Rows(cbPersonReporting.SelectedIndex).Item("SheriffID"))
.Parameters.AddWithValue("DateTimeSubmitted", Today.ToString("MM/dd/yyyy") & " " & TimeOfDay.ToString("HH:mm:ss"))
.Parameters.AddWithValue("SupervisorFirstName", dtSupervisorContactedList.Rows(cbSupervisorContacted.SelectedIndex).Item("FirstName"))
.Parameters.AddWithValue("SupervisorLastName", dtSupervisorContactedList.Rows(cbSupervisorContacted.SelectedIndex).Item("LastName"))
.Parameters.AddWithValue("SupervisorIDNumber", dtSupervisorContactedList.Rows(cbSupervisorContacted.SelectedIndex).Item("SheriffID"))
If cbPositionLocation.SelectedValue = "" Then
.Parameters.AddWithValue("PositionLocation", "")
Else
.Parameters.AddWithValue("PositionLocation", cbPositionLocation.SelectedValue)
End If
.Parameters.AddWithValue("ProblemDescription", tbProblemDescription.Text)
.Parameters.AddWithValue("Status", "Open")
.Parameters.AddWithValue("DuplicateParent", False)
.Parameters.AddWithValue("DuplicateChild", False)
.Parameters.AddWithValue("Reopened", False)
.CommandType = CommandType.Text
.ExecuteNonQuery()
.CommandText = "SELECT @@Identity"
MessageBox.Show("The repair request has been submitted. The request number is: " & cmdSubmitRepairRequest.ExecuteScalar(), "Repair Request Submitted", MessageBoxButtons.OK, MessageBoxIcon.Information)
End With
End If
我尝试为参数使用不同的名称而不做任何更改。我似乎没有做任何事情(没有使用基于字符串的查询)似乎工作。非常令人沮丧,特别是因为类似的代码在我的程任何帮助表示赞赏。感谢。