过程或函数需要@Id参数,该参数未提供 [英] The procedure or function needs @Id parameter which was not supplied

查看:74
本文介绍了过程或函数需要@Id参数,该参数未提供的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

我想使用ADO在sql数据库中保存数据。我有一个Base类和一些子类,我在用Base类编写的Save方法中调用存储过程。在程序的第一次和第二次运行中,一切都很好,但之后当我想保存数据时,有一个错误说: Gender_Save方法指定的参数太多了

有时我有另一个错误说:过程或函数Gender_Save需要@Id参数,这是未提供的



sql过程和Save方法中的参数个数是相同的(我认为),并且sql表中的Id字段的IDENTITY是on.so它必须自动添加。过程是Executed并且在SQL中工作正常。似乎在sql中没有问题。顺便说一句,我在数据层中使用DataLogic类来连接数据库。我与sql的连接是因为我可以从表格中加载数据(使用sql加载程序)

我是编程新手,真的需要你的帮助才能解决这个问题:(





_____________________________________

这是我的SQL程序:



  CREATE   PROC  Gender_Save( @Id   INT  @ Title   NVARCHAR  50 ))
AS
BEGIN
SET NOCOUNT ON
IF EXISTS SELECT 1 FROM 性别
WHERE Id = @ Id))
BEGIN
更新性别
SET Title = @Title
WHERE Id = @ Id
END

ELSE
BEGIN
INSERT 性别
SELECT @ Title

< span class =code-keyword> END

PRINT ' Save Dable Successfuly'
END

_______________________________________________________
以下代码是基类:

public class 基础
{
public Base()
{

}
public string 消息{ get ; set ; }

// SAVE方法
public void 保存()
{

DataLogic dl = new DataLogic();

dl.command.CommandType = CommandType.StoredProcedure;
dl.command.CommandText = string .Format( {0} _Save this .GetType()。Name);
foreach (PropertyInfo propertyinfo in this .GetType()。GetProperties())
{


dl.command.Parameters.Add( new SqlParameter( string .Format( @ {0} ,propertyinfo.Name),propertyinfo.Name));
// dl.command.Parameters.Clear();
}
尝试
{
dl.connection.Open();
dl.command.ExecuteNonQuery();
this .Message = 保存ID已完成成功;

dl.connection.Close();
}
catch (SqlException ex)
{
this .Message = ex.Message;
}

}
}





________________________________________________________________________

这里是名为Gender的子类:

  public   class 性别:Base 
{
public 性别()
{

}
public 性别( int id, string title)
{
Id = id;
Title = title;
}
public int ID {获得; set ; }
public string 标题{ get ; set ; }

}



______________________________________________________

最后,当我点击表格上的保存按钮时,会发生以下事件:

  private   void  button1_Click( object  sender,EventArgs e)
{
性别性别= 性别(-1,txtBoxTitle 。文本);
gender.Save();
MessageBox.Show(gender.Message);
this .Close();
}

解决方案

所有你需要做的(可能)是将一个参数添加到Base类Save方法来传输Id值:

 dl.command.Parameters.AddWithValue(  @ID,Id); 

您可能需要将Id从性别类移到基地以避免未定义。


hello,
I want to save data in sql database using ADO. I have a Base class and some child classes, and I''m calling stored procedure in my Save method written in Base class. in the first and second run of the program , everything was fine,but after that when I want to save data, there is an error said: the Gender_Save method has too many arguments specified !
sometimes I''ve got another error says: procedure or function Gender_Save needs @Id parameter which was not supplied !

the number of parameters in sql procedure and Save method are the same(I think), and the IDENTITY of "Id" field in sql table is on.so it must be added automatically.The procedure is Executed and works fine in SQL.it seems there is no problem in sql.By the way,I'' using DataLogic class in Data Layer for connecting to Database. my connection to sql is on cause I can load data from table in my form(using sql Load procedure)
I am new to programming, and really need your help to solve this :(


_____________________________________
here is my SQL Procedure:

CREATE PROC Gender_Save (@Id INT ,@Title NVARCHAR(50))			
AS
BEGIN
		SET NOCOUNT ON
		IF(EXISTS(SELECT 1 FROM Gender
			  WHERE Id=@Id ))
		BEGIN
		     UPDATE Gender
		     SET   Title=@Title
		     WHERE Id=@Id
		END
		
		ELSE
		BEGIN
		       INSERT Gender
		       SELECT @Title
		      
		END
		
		PRINT 'Save Done Successfuly'
END

_______________________________________________________
below code is Base class:

public class Base
   {
       public Base()
       {

       }
       public string Message { get; set; }

       // SAVE method
       public  void Save()
       {

           DataLogic dl= new DataLogic();

           dl.command.CommandType = CommandType.StoredProcedure;
           dl.command.CommandText = string.Format("{0}_Save",this.GetType().Name);
           foreach(PropertyInfo propertyinfo in this.GetType().GetProperties())
           {


           dl.command.Parameters.Add(new SqlParameter(string.Format("@{0}", propertyinfo.Name), propertyinfo.Name));
                   //dl.command.Parameters.Clear();
           }
           try
           {
               dl.connection.Open();
               dl.command.ExecuteNonQuery();
                this.Message = "Save id done successfully";

               dl.connection.Close();
           }
           catch (SqlException ex)
           {
               this.Message = ex.Message;
           }

       }
}



________________________________________________________________________
here is the child class called Gender:

public class Gender: Base
    {
        public Gender()
        {

        }
        public Gender(int id, string title)
        {
            Id = id;
            Title = title;
        }
        public int Id { get; set; }
        public string Title { get; set; }

    }


______________________________________________________
finally, when I click Save button on my form, this event happens:

private void button1_Click(object sender, EventArgs e)
        {
            Gender gender = new Gender (-1,txtBoxTitle.Text);
            gender.Save();
            MessageBox.Show(gender.Message);
            this.Close();
        }

解决方案

All you need to do (probably) is add a Parameter to the Base class Save method to transfer the Id value:

dl.command.Parameters.AddWithValue("@ID", Id);

You will probably have to move the Id from teh Gender class to the Base to avoid it not being defined.


这篇关于过程或函数需要@Id参数,该参数未提供的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆