过程或函数指定的参数太多

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了过程或函数指定的参数太多相关的知识,希望对你有一定的参考价值。

我正在尝试使用存储过程从c#上传我的上传事件中的.mdb文件。但是那个mdb文件没有在我的sql server的表中加载数据。当我调试整个代码时,我得到了异常。我的存储过程参数是:-(从存储过程我只显示它的起始,因为它是敏感的)

  USE [Demo]
    GO

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO

    ALTER PROCEDURE [dbo].[UploadToDatabase] 
        @UserId bigint,
        @ClientMachineIP nvarchar(15),
        @LoadType nvarchar(25) ='Upload',
        @InstId bigint = null, 
        @bIsIgnoreErrors bit = 'False'
AS
BEGIN
    SET NOCOUNT ON;

    .
            declare @srcConnection varchar(MAX) 
        set @srcConnection = 'Data Source=PAth Of my .mdb file;Provider=Microsoft.Jet.OLEDB.4.0;'


            declare @ssispath varchar(1000)
            declare @cmd varchar(1000)
            set @ssispath = 'Package PathPackage.dtsx'
            set @cmd = @cmd + 'Package.Variables["SourceConnectionString"].Value = @srcConnection' 
            set @cmd = 'dtexec /F "' + @ssispath + '"' 
            print @cmd
            exec master..xp_cmdshell @cmd

之后是将数据加载到我的表中,这些代码我没有在这里显示。

我使用Linq to SQL来访问代码中的sql server,因此对于存储过程我的designer.cs文件代码是

[Function(Name="dbo.UploadToDatabase")]
        public int UploadToDatabase([Parameter(Name="UserId", DbType="BigInt")] System.Nullable<long> userId, [Parameter(Name="ClientMachineIP", DbType="NVarChar(15)")] string clientMachineIP, [Parameter(Name="LoadType", DbType="NVarChar(25)")] string loadType, [Parameter(Name="InstId", DbType="BigInt")] System.Nullable<long> instId, [Parameter(DbType="Bit")] System.Nullable<bool> bIsIgnoreErrors, [Parameter(DbType="Int")] ref System.Nullable<int> rcout)
        {
            IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), userId, clientMachineIP, loadType, instId, bIsIgnoreErrors, rcout);
            rcout = ((System.Nullable<int>)(result.GetParameterValue(5)));
            return ((int)(result.ReturnValue));
        }

由于我有分层架构存储过程调用上传按钮是 -

FROM -Access.cs文件

Public int? RunDTSxProcess(long userID, string clientMachineIP, string loadType, long instID, bool ignoreErrors)
        {
            try
            {
                int? result = 0;
                var run = Demo.UploadToDatabase(userID, clientMachineIP, loadType, instID, ignoreErrors,ref result);
                return result;
            }
            catch (Exception)
            {

                throw;
            }
        }   

Logic.cs

 public int? ExcuteDTSxProcess(long userID, string clientMachineIP, string loadType, long instID, bool ignoreErrors)
    {
        try

        {
            return new DAL.access().RunDTSxProcess( userID,  clientMachineIP,  loadType,  instID,  ignoreErrors);
        }
        catch (Exception)
        {
            throw;
        }
    }

点击按钮: -

protected void btnUpload_Click(object sender, EventArgs e)
        {
            pnlUploadData.Style.Value = "display:none;";
            pnlModify.Style.Value = "display:none;";
            if (fileUpload1.HasFile)
            {
                try
                {


                    string savePath = string.Empty;
                    savePath = ConfigurationManager.AppSettings["UploadDBFilePath"];



                    Server.ScriptTimeout = 100000000;

                    fileUpload1.SaveAs(Server.MapPath(savePath + fileUpload1.FileName));

                    try
                    {
                        //Call Upload procedure
                        **int? result = this.UploadModifyDatabase(savePath +
                              fileUploadEspace.FileName, "Upload");**
                        if(result == 0)
                            lblUploadSuccess.Text = "Database Upload successful";
                        else
                            lblUploadSuccess.Text = "Error while uploading the Database. Please try again.";

                    }
                    catch (Exception)
                    {
                        lblUploadSuccess.Text = "Error in append process. Please try again.";

                    }

                    lblUploadSuccess.Text = "File uploaded successfully.";
 catch (Exception ex)
                {
                    lblUploadSuccess.Text = "ERROR: " + ex.Message.ToString();
                }
            }
            else
            {
                lblUploadSuccess.Text = "You have not specified a file.";
            }

上传修改数据库的方法是: -

   protected int? UploadModifyDatabase(string fileName, string loadType)
            {
                try
                {
                    long userID = 0;
                    if (null != Session["UserID"])
                        userID = long.Parse(Session["UserID"].ToString());
                    long instID = 0;
                    if (null != Session["InstID"])
                        instID = long.Parse(Session["InstID"].ToString());
                    string clientMachineIP = null;

                    bool ignoreErrors = ckhIgnoreErrors.Checked;

                    int? result =  new BLL.logic().ExcuteDTSxProcess(userID, clientMachineIP, loadType, instID, ignoreErrors);
                    return result;

                }
                catch (Exception)
                {

                    throw;
                }
            }

请告诉我代码中出了什么问题。根据一些推荐的帖子,我试图在存储过程中添加一个r计数变量,但问题仍未解决。

答案

您的Procedure UploadToDatabase定义了5个参数,但在designer.cs中您有6个参数,包括rcout。如果将rcout用作输出参数,则可以更改过程以将rcout作为参数或输出参数。

 ALTER PROCEDURE [dbo].[UploadToDatabase] 
        @UserId bigint,
        @ClientMachineIP nvarchar(15),
        @LoadType nvarchar(25) ='Upload',
        @InstId bigint = null, 
        @bIsIgnoreErrors bit = 'False',
        @rcout int OUTPUT
AS
BEGIN
    SET NOCOUNT ON;
    declare @srcConnection varchar(MAX) 
    set @srcConnection = 'Data Source=PAth Of my .mdb file;Provider=Microsoft.Jet.OLEDB.4.0;'
    declare @ssispath varchar(1000)
    declare @cmd varchar(1000)
    set @ssispath = 'Package PathPackage.dtsx'
    set @cmd = @cmd + 'Package.Variables["SourceConnectionString"].Value = @srcConnection' 
    set @cmd = 'dtexec /F "' + @ssispath + '"' 
    print @cmd
    exec master..xp_cmdshell @cmd
    Select @rcout= @@ROWCOUNT

以上是关于过程或函数指定的参数太多的主要内容,如果未能解决你的问题,请参考以下文章

无法从网格更新表(过程或函数 spExcelGridTestUpdateTable 指定了太多参数)

存储过程指定了太多参数

将参数传递给C#中的存储过程

SqlException:过程或函数 sqQuestion_AddNewQuestion 指定的参数过多

Delphi7:快捷键

[python] 之 函数简介