过程或函数'procname'需要参数'@id',这是未提供的

时间:2013-06-25 05:08:47

标签: c# sql

我想将数值插入到存储在数组列表中的数据库中。但是当插入我得到了这个错误时,我曾尝试过不同的方式,但它不起作用可以任何人帮助我 在这里输入代码

public void Test_Get_Table_From_Grasshopper_Home(int id, DateTime Date,string fromphone, string ext,string type,DateTime created)
   {
          cw("Retrieving grasshopper page...");

           string filepath = string.Format(@"\home.htm", localprojectpath);
           var calls = new List<Call>(); 
           using (WebZinc webZinc = new WebZinc())
           {

            webZinc.OpenPage(filepath);
            WhiteCliff.WebZinc.Objects.Table table_calls = webZinc.CurrentPage.GetHtmlElements("div", "grid_messages")[1].Tables[0];
            if (table_calls.Rows.Count > 0)
            {
             foreach (TableRow tr in table_calls.Rows)
             {
               if (tr.Cells.Count > 0)
                 {
                   var singlecall = new Call
                      {
                        date = tr.Cells[0].Text, ext = tr.Cells[1].Text, fromphone = tr.Cells[2].Text,type = tr.Cells[3].Text
                       };
                            calls.Add(singlecall); calls.
                        }
                    }
                }
                SqlConnection connection = new SqlConnection(
                        @"Data Source=;Initial Catalog=;User ID=sa;Password=123");
                connection.Open();

                SqlCommand cmd = new SqlCommand();
                cmd.Connection = connection;
                cmd.CommandText = "dbo.nameproc";
                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.AddWithValue("@id", id);
               cmd.Parameters.AddWithValue("@fromphone", fromphone);
               cmd.Parameters.AddWithValue("@date", Date);
               cmd.Parameters.AddWithValue("@ext", ext);
               cmd.Parameters.AddWithValue("@type", type);
               cmd.Parameters.AddWithValue("@created", created);
         cmd.ExecuteNonQuery();
         Console.WriteLine("insert sucessfull done");
     }

            if (calls.Count > 0)
            {
                foreach (var call in calls)
                {
                    cw(string.Format("{0} - {1} - {2}-{3}", call.date, call.ext, call.fromphone, call.type));
                }

            }

        }

    }

}


public class Call
{
    public string id { get; set; }
    public string date { get; set; } 
    public string fromphone { get; set; }
    public string ext { get; set; }
    public string type { get; set; }
    public string created { get; set; }

}

SP:

ALTER procedure [dbo].[nameproc]
      (@id int ,@date datetime,@fromphone varchar(50),@ext varchar(50),@type          varchar(50),@created datetime)
       AS   
       BEGIN
         Insert into GrasshopperLog (id,date,fromphone,ext,type,created)
      Values(@id,@date,@fromphone,@ext,@type,@created)
       END

2 个答案:

答案 0 :(得分:4)

错误是不言自明的。

你的存储过程dbo.homeproc希望你提供一个参数..你没有。

cmd.Parameters.AddWithValue("id", some_variable_holding_an_id);

修改

cmd.Parameters.AddWithValue("@id",SqlDbType.Int).Value=id;

应该是:

cmd.Parameters.AddWithValue("@id",id);

答案 1 :(得分:0)

请尝试按顺序将参数传递给存储过程,可能会有帮助