【问题标题】:Error While Executing my Stored Procedure in Windows Form在 Windows 窗体中执行我的存储过程时出错
【发布时间】:2014-04-11 02:23:18
【问题描述】:

我正在尝试将INSERT 我的数据放入两个表ServicesService-line

解释我的存储过程:

  • 它检查表单中的现有 ID
  • 如果未找到,它将在 Services 表中创建一条新记录,并在 Form 中使用信息,然后使用 scope_identity 获取 ID 并为 Service-Line 表使用 ID

    ID 也会返回到 Form 并保持不变。

  • 稍后在Service-Line 中插入第二条记录时,存储过程会检查现有的ID;如果找到,这一次,它会从表单中获取ID 并在Service-Line 中使用它

这是我的存储过程

请多多包涵,因为我正在编写这段代码并进行测试,很多行都被注释掉了

ALTER PROCEDURE [dbo].[InsertServiceServiceLine] (
   --Services Entry

   --FOR IF CONDITION ---CHECK THE DEFAULT VALUE IN THE FORM on SID
   @ExistingSID int,

   --SEELCT PARAMETES DEF VALUES
   @ComboBoxSelectedBike varchar(100),

   -- INPUT PARAMETERES FOR NEW RECORD
   @CID int,
   @Status bit = 1,
   @CurrentMeter int,
   @Labor decimal(20,0),
   @GrandTotal decimal(20,0) = ISNULL,
   --@NextService datetime,

   --Service Line
   @Spare nvarchar(500),
   @Quantity int,
   @Uprice decimal(20,2),
   @Subtotal decimal(20,2)
)
AS
BEGIN

IF (@ExistingSID <= 0)

BEGIN

SET NOCOUNT ON;

DECLARE @BikeID int

SELECT @BikeID = (SELECT BikeID FROM TblBikeNames WHERE BikeName = @ComboBoxSelectedBike)

INSERT INTO [AutoDB_Sample].[dbo].[TblServices]
(CID,BikeID,Status,CurrentMeter,Labor,DateOfService)

VALUES
(@CID,@BikeID,@Status,@CurrentMeter,@Labor,GETDATE())

DECLARE @SID int

SET @SID = SCOPE_IDENTITY()

INSERT INTO [AutoDB_Sample].[dbo].[TblServiceLine]
(SID,Spare,Quantity,Uprice,Subtotal,DateCreated)

VALUES
(@SID,@Spare,@Quantity,@Uprice,@Subtotal,GETDATE())


RETURN @SID

END

ELSE

BEGIN

INSERT INTO [AutoDB_Sample].[dbo].[TblServiceLine]
(SID,Spare,Quantity,Uprice,Subtotal,DateCreated)

VALUES
(@ExistingSID,@Spare,@Quantity,@Uprice,@Subtotal,GETDATE())

END

END

当我在 C# Windows 窗体中使用此存储过程时出现错误

过程或函数的参数太多

这是错误截图

我认为将我的登录信息放在 SP 中会很棒并且可以提高我的应用程序的性能。但是现在卡住了。

这是我的 C# 代码


public void AddItemIntoServices_ServiceLine()
        {

            ConnectionStringSettings consetting = ConfigurationManager.ConnectionStrings["AutoDB"];

            String ConnectionString = consetting.ConnectionString;

            SqlConnection con = new SqlConnection(ConnectionString);

            try
            {
                con.Open(); // open the connection

                // Specify the name of the Stored Procedure you will call

                String SP_Name = "InsertServiceServiceLine";

                // Create the SQL Command object and specify that this is a SP.

                SqlCommand cmd = new SqlCommand(SP_Name, con);
                cmd.CommandType = CommandType.StoredProcedure;

                // Specify values for the input parameters of our Stored Procedure
                // Parameters MUST be named the same as the parameters defined in the Stored Procedure. 


                //~~ If Condition Parameter ****************************************************************************~~//

                int exitstingSID;
                if (int.TryParse(LblSID_Data.Text, out exitstingSID)) ;

                SqlParameter ExistingSID = new SqlParameter("@ExistingSID", exitstingSID);
                ExistingSID.Direction = ParameterDirection.Input;
                ExistingSID.DbType = DbType.Int16;
                cmd.Parameters.Add(ExistingSID);

                //Parameter to select Bike ID from Selected Bike Name

                SqlParameter ParamBikeID = new SqlParameter("@ComboBoxSelectedBike",                       ComboBx_BikeNames.Text);
                ParamBikeID.Direction = ParameterDirection.Input;
                ParamBikeID.DbType = DbType.String;
                cmd.Parameters.Add(ParamBikeID);

                //~~ Customer Info ************************************************************************************~~//

                //CID Convertion
                int P_CID;
                if (int.TryParse(LblCID_Data.Text, out P_CID)) ;
                cmd.Parameters.AddWithValue("@CID", P_CID);

                cmd.Parameters.AddWithValue("@Cname", this.TxtBx_CustomerName.Text);
                cmd.Parameters.AddWithValue("@Vnum", this.TxtBx_VehicleNumber.Text);

                //~~ Service Info ************************************************************************************~~//

                //Labor Convertion
                int Laborint;
                if (int.TryParse(TxtBxLabor.Text, out Laborint)) ;
                SqlParameter ParamLabor = new SqlParameter("@Labor", Laborint);

                ParamLabor.Direction = ParameterDirection.Input;
                ParamLabor.DbType = DbType.Int16;
                cmd.Parameters.Add(ParamLabor);

               //CurrentMeterConversion

                int currentMeterint;
                if (int.TryParse(TxtBx_CurrentMeter.Text, out currentMeterint)) ;

                SqlParameter ParamCurrentMeter = new SqlParameter("@CurrentMeter", currentMeterint);
                ParamCurrentMeter.Direction = ParameterDirection.Input;
                ParamCurrentMeter.DbType = DbType.Int16;
                cmd.Parameters.Add(ParamCurrentMeter);


                //Return Value
                SqlParameter ParamReturn = new SqlParameter("@SID", SqlDbType.Int);
                ParamReturn.Direction = ParameterDirection.Output;
                ParamReturn.DbType = DbType.Int16;
                cmd.Parameters.Add(ParamReturn);


                //~~ Service Info ************************************************************************************~~//

                //Converstions
                Decimal UP, ST;
                if (Decimal.TryParse(TxtBx_UnitPrice.Text, out UP)) ;
                if (Decimal.TryParse(TxtBxTotal.Text, out ST)) ;

                //SpareName
                cmd.Parameters.AddWithValue("@Spare", ComboBx_SparesName.Text);

                //Quantity
                cmd.Parameters.AddWithValue("@Qty", NumericBx_Quantity.Value);

                //Unit Price
                SqlParameter ParamUp = new SqlParameter("@Uprice", UP);
                ParamUp.Direction = ParameterDirection.Input;
                ParamUp.DbType = DbType.Decimal;
                cmd.Parameters.Add(ParamUp);

                //Total
                SqlParameter ParamTot = new SqlParameter("Subtotal", ST);
                ParamTot.Direction = ParameterDirection.Input;
                ParamTot.DbType = DbType.Decimal;
                cmd.Parameters.Add(ParamTot);


                cmd.ExecuteNonQuery();

                String _returnedSID = cmd.Parameters["@SID"].Value.ToString();
                LblSID_Data.Text = _returnedSID;
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            finally
            {
                con.Close();
                clear();
                ToolStripLable_Status.Text = "New Service Record Created";
            }

        }

【问题讨论】:

  • 我认为问题出在调用这个sp的C#代码中。请将该代码添加到您的问题中。
  • 消息清晰,提供更多参数给SP。检查调用该 SP 的代码所在的参数。
  • @史蒂夫。 . .我已经添加了 C# 代码。
  • @ThitLwinOo 。 . SP 在 SSMS 中运行良好,当我从 C# 执行它时出现此错误

标签: c# sql-server winforms function stored-procedures


【解决方案1】:

通过命令集合传递的参数列表应与存储过程的名称、类型和方向完全匹配。

命令集合中包含的参数列表与存储过程所需的参数不匹配,因此出现错误。

乍一看,我可以说:

cmd.Parameters.AddWithValue("@Cname", this.TxtBx_CustomerName.Text);
cmd.Parameters.AddWithValue("@Vnum", this.TxtBx_VehicleNumber.Text);
SqlParameter ParamReturn = new SqlParameter("@SID", SqlDbType.Int);

是添加到命令集合但不在存储过程参数列表中的参数。

相反,我们有存储过程所需的参数@status bit@GrandTotal decimal(20,0) = ISNULL,,但列表中没有。 (顺便说一下,NULL 参数的默认语法是@GrandTotal decimal(20,0) = NULL

最后存储过程命名了一个参数@Quantity,但是你添加了一个名为@Qty的参数。

现在还存在应该匹配的参数类型的问题,否则您最多会冒自动转换的风险,或者会出现说明类型不匹配的错误消息。

您有许多 int 类型的参数,但是您传递了 Int16 类型的参数,而正确的类型是 Int32。并且参数@Labor 的类型也有一个更明显的错误,该错误应该是 sp 的小数,但您将其添加为整数 (16)

【讨论】:

  • 是的,史蒂夫,我更正了它,我的代码按预期工作正常,我将按照您所说的更改 Null 参数。谢谢zz
【解决方案2】:

您在存储过程定义中有 12 个参数(如果我计算正确的话),但是,您的代码可能有更多或更少的参数。代码中定义的参数太多,或者存储过程需要第 13 个参数。我认为错误提到了前者,但我有时会让他们感到困惑。

无论如何,错误总是由于参数数量不匹配,并确保方向(输入/输出)设置正确,并且所有内容都输入正常。

【讨论】:

  • 是的,布赖恩,我很傻。 . .稍后发布此内容后,我更正了我的代码。谢谢zz
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多