存储过程指定了太多参数
Posted
技术标签:
【中文标题】存储过程指定了太多参数【英文标题】:stored procedure has too many arguments specified 【发布时间】:2013-08-16 14:36:45 【问题描述】:我花了一整天的时间来解决这个问题(不管是什么问题),我仍然觉得我离解决问题还差得远。我已经检查并重新检查了数据适配器中的参数和表映射设置。加上数据集字段和代码参数等。该过程旨在填充数据集“dsStatus”中具有“StatusID”和“StatusType”字段的表。 是声明参数吗? Visual Studio 是否认为我也应该为声明参数提供值?感谢任何有建议或解决方案的人!
表单代码...
DsStatus1.Clear()
DsStatus1 = kernel.getStatus(intDepartmentID, intJobID, "Job")
数据适配器代码...
Public Function getStatus(ByVal intDepartmentID As Integer, ByVal intJobID As Integer, ByVal strStatusCategory As String) As dsStatus
Dim dsTemp As New dsStatus
SqlDataAdapter1.SelectCommand.Parameters("@DepartmentID").Value = intDepartmentID
SqlDataAdapter1.SelectCommand.Parameters("@JobID").Value = intJobID
SqlDataAdapter1.SelectCommand.Parameters("@StatusCategory").Value = strStatusCategory
Me.SqlDataAdapter1.Fill(dsTemp)
Return dsTemp
End Function
存储过程...
USE [microgenDB]
GO
/****** Object: StoredProcedure [dbo].[get_job_status] Script Date: 08/16/2013 15:20:15 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
/* -------------------- */
ALTER PROCEDURE [dbo].[get_job_status]
@JobID INT,
@DepartmentID INT,
@StatusCategory VARCHAR(50)
AS
BEGIN
DECLARE @MaxJobTaskStartDate DATETIME
DECLARE @MaxJobTaskEndDate DATETIME
--Get the most recent task...
--Get MaxStartDate where EndDate is null
SELECT @MaxJobTaskStartDate = MAX(dbo.tbl_JobTask.JobTaskStartDate)
FROM dbo.tbl_JobTask INNER JOIN
dbo.tbl_JobDepartmentTask ON dbo.tbl_JobTask.JobDepartmentTaskID = dbo.tbl_JobDepartmentTask.JobDepartmentTaskID INNER JOIN
dbo.tbl_Task ON dbo.tbl_JobDepartmentTask.TaskID = dbo.tbl_Task.TaskID INNER JOIN
dbo.tbl_Job ON dbo.tbl_JobTask.JobID = dbo.tbl_Job.JobID INNER JOIN
dbo.tbl_Status ON dbo.tbl_JobDepartmentTask.StatusID = dbo.tbl_Status.StatusID
WHERE (dbo.tbl_JobTask.JobID = @JobID) AND (dbo.tbl_JobTask.JobTaskEndDate IS NULL) AND (dbo.tbl_Status.StatusCategory = @StatusCategory)
GROUP BY dbo.tbl_JobTask.JobID
--Get MaxEndDate (for jobs where no task has a null end date)
SELECT @MaxJobTaskEndDate = MAX(dbo.tbl_JobTask.JobTaskEndDate)
FROM dbo.tbl_JobTask INNER JOIN
dbo.tbl_JobDepartmentTask ON dbo.tbl_JobTask.JobDepartmentTaskID = dbo.tbl_JobDepartmentTask.JobDepartmentTaskID INNER JOIN
dbo.tbl_Task ON dbo.tbl_JobDepartmentTask.TaskID = dbo.tbl_Task.TaskID INNER JOIN
dbo.tbl_Job ON dbo.tbl_JobTask.JobID = dbo.tbl_Job.JobID INNER JOIN
dbo.tbl_Status ON dbo.tbl_JobDepartmentTask.StatusID = dbo.tbl_Status.StatusID
WHERE (dbo.tbl_JobTask.JobID = @JobID) AND (dbo.tbl_Status.StatusCategory = @StatusCategory)
GROUP BY dbo.tbl_JobTask.JobID
--get the max end date coz there's no open tasks
IF (@MaxJobTaskStartDate IS NULL)
BEGIN
--select the highest order task with the max end date
SELECT dbo.tbl_Status.StatusID, dbo.tbl_Status.StatusType
FROM dbo.tbl_Status INNER JOIN
dbo.tbl_JobDepartmentTask ON dbo.tbl_Status.StatusID = dbo.tbl_JobDepartmentTask.StatusID
WHERE (dbo.tbl_JobDepartmentTask.TaskLookupOrder IN
(SELECT MAX(tbl_JobDepartmentTask_1.TaskLookupOrder) AS MaxTaskLookupOrder
FROM dbo.tbl_JobDepartmentTask AS tbl_JobDepartmentTask_1 INNER JOIN
dbo.tbl_JobTask ON tbl_JobDepartmentTask_1.JobDepartmentTaskID = dbo.tbl_JobTask.JobDepartmentTaskID INNER JOIN
dbo.tbl_Status ON tbl_JobDepartmentTask_1.StatusID = dbo.tbl_Status.StatusID
WHERE (dbo.tbl_JobTask.JobID = @JobID) AND (dbo.tbl_JobTask.JobTaskEndDate = @MaxJobTaskEndDate) AND (dbo.tbl_Status.StatusCategory = @StatusCategory))) AND
(dbo.tbl_JobDepartmentTask.DepartmentID = @DepartmentID)
END
--there is an open task so see if it's start date is later than the closed tasks
ELSE
IF @MaxJobTaskStartDate < @MaxJobTaskEndDate
BEGIN
--use the maxenddate as the task status
SELECT dbo.tbl_Status.StatusID, dbo.tbl_Status.StatusType
FROM dbo.tbl_Status INNER JOIN
dbo.tbl_JobDepartmentTask ON dbo.tbl_Status.StatusID = dbo.tbl_JobDepartmentTask.StatusID
WHERE (dbo.tbl_JobDepartmentTask.TaskLookupOrder IN
(SELECT MAX(tbl_JobDepartmentTask_1.TaskLookupOrder) AS MaxTaskLookupOrder
FROM dbo.tbl_JobDepartmentTask AS tbl_JobDepartmentTask_1 INNER JOIN
dbo.tbl_JobTask ON tbl_JobDepartmentTask_1.JobDepartmentTaskID = dbo.tbl_JobTask.JobDepartmentTaskID INNER JOIN
dbo.tbl_Status ON tbl_JobDepartmentTask_1.StatusID = dbo.tbl_Status.StatusID
WHERE (dbo.tbl_JobTask.JobID = @JobID) AND (dbo.tbl_JobTask.JobTaskEndDate = @MaxJobTaskEndDate) AND (dbo.tbl_Status.StatusCategory = @StatusCategory)))AND
(dbo.tbl_JobDepartmentTask.DepartmentID = @DepartmentID)
END
-- use the maxstartdate as the task status
ELSE
BEGIN
SELECT dbo.tbl_Status.StatusID, dbo.tbl_Status.StatusType
FROM dbo.tbl_Status INNER JOIN
dbo.tbl_JobDepartmentTask ON dbo.tbl_Status.StatusID = dbo.tbl_JobDepartmentTask.StatusID
WHERE (dbo.tbl_JobDepartmentTask.TaskLookupOrder IN
(SELECT MAX(tbl_JobDepartmentTask_1.TaskLookupOrder) AS MaxTaskLookupOrder
FROM dbo.tbl_JobDepartmentTask AS tbl_JobDepartmentTask_1 INNER JOIN
dbo.tbl_JobTask ON tbl_JobDepartmentTask_1.JobDepartmentTaskID = dbo.tbl_JobTask.JobDepartmentTaskID INNER JOIN
dbo.tbl_Status ON tbl_JobDepartmentTask_1.StatusID = dbo.tbl_Status.StatusID
WHERE (dbo.tbl_JobTask.JobID = @JobID) AND (dbo.tbl_JobTask.JobTaskStartDate = @MaxJobTaskStartDate) AND (dbo.tbl_Status.StatusCategory = @StatusCategory)))AND
(dbo.tbl_JobDepartmentTask.DepartmentID = @DepartmentID)
END
END
定义 SqlDataAdapter1 的代码(从 Visual Studio 的设计器窗口复制) 它在数据映射窗口中显示存储过程中的三个表。我尝试将所有三个表从源 sp 映射到数据集 dsStatus 中的一个表,并在数据集中创建三个单独的表并将每个表映射到存储过程中的相应表。错误信息仍然存在!
'SqlSelectCommand4
'
Me.SqlSelectCommand4.CommandText = "get_job_status"
Me.SqlSelectCommand4.CommandType = System.Data.CommandType.StoredProcedure
Me.SqlSelectCommand4.Connection = Me.SqlConnection1
Me.SqlSelectCommand4.Parameters.AddRange(New System.Data.SqlClient.SqlParameter() New System.Data.SqlClient.SqlParameter("@RETURN_VALUE", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.ReturnValue, False, CType(0, Byte), CType(0, Byte), "", System.Data.DataRowVersion.Current, Nothing), New System.Data.SqlClient.SqlParameter("@JobID", System.Data.SqlDbType.Int, 4), New System.Data.SqlClient.SqlParameter("@DepartmentID", System.Data.SqlDbType.Int, 4), New System.Data.SqlClient.SqlParameter("@StatusCategory", System.Data.SqlDbType.VarChar, 50))
'
'SqlDataAdapter1
'
Me.SqlDataAdapter1.SelectCommand = Me.SqlSelectCommand4
Me.SqlDataAdapter1.TableMappings.AddRange(New System.Data.Common.DataTableMapping() New System.Data.Common.DataTableMapping("Table1", "get_job_status", New System.Data.Common.DataColumnMapping() New System.Data.Common.DataColumnMapping("StatusID", "StatusID"), New System.Data.Common.DataColumnMapping("StatusType", "StatusType")), New System.Data.Common.DataTableMapping("Table2", "get_job_status", New System.Data.Common.DataColumnMapping() New System.Data.Common.DataColumnMapping("StatusID", "StatusID"), New System.Data.Common.DataColumnMapping("StatusType", "StatusType")), New System.Data.Common.DataTableMapping("Table", "get_job_status", New System.Data.Common.DataColumnMapping() New System.Data.Common.DataColumnMapping("StatusID", "StatusID"), New System.Data.Common.DataColumnMapping("StatusType", "StatusType")))
【问题讨论】:
您确定您的代码指向您认为的完全相同的数据库副本吗?您的连接字符串是否包含AttachDbFileName
之类的内容?
应用程序连接字符串指向开发服务器。所以这是正确的。据我所知,没有对 AttachDbFileName 的引用。干杯。
定义SqlDataAdapter1
的代码在哪里?你确定那段代码也没有加参数吗?
啊,我有两个数据适配器填充同一个数据集。这可能是问题所在。另一个数据适配器也给了我“无法从数据库表中检索模式”。我会看看那个。如果我有任何进展会发布!
不,不是这样 :-(
【参考方案1】:
好的,这是因为一个单独的 xml 文件驻留在运行用户应用程序的本地计算机上。即使到服务器/数据库的连接字符串在应用程序中是正确的,xml 文件连接字符串指向不同的服务器。因为这个,2天+ 2小时。我想你可以猜到我的感受。希望这对其他人有帮助!
【讨论】:
以上是关于存储过程指定了太多参数的主要内容,如果未能解决你的问题,请参考以下文章