您好我正在尝试从控制台生成输出但仍然收到错误消息
程序或功能'p_Date_sel' 期望参数'@DateID',这是 没提供
假设获取DateID并显示所有数据,请告诉我我做错了什么。
以下是我的存储过程
ALTER procedure [dbo].[p_Date_sel]
@DateID int
AS
BEGIN
SELECT DateTypeID , Date , Name, Notes
FROM dbo.Dates
WHERE DateID= @DateID
END
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
namespace ExecuteStoredProcedure
{
class Program
{
private int _dateID;
private int _dateTypeID;
private DateTime _date;
private string _name;
private string _notes;
public Program()
{
_dateID = 0;
}
public Program(int dateID) {
_dateID = dateID;
}
public Program(int datetypeID,DateTime date,string name,string notes){
_dateTypeID = datetypeID;
_date = date;
_name = name;
_notes = notes;
}
public int dateID
{
get {return _dateID;}
}
public int dateTypeID
{
get { return _dateTypeID; }
set { _dateTypeID = value; }
}
public DateTime date
{
get {return _date ;}
set { _date = value;}
}
public string name
{
get { return _name;}
set { _name = value; }
}
public string notes
{
get { return _notes;}
set { _notes = value; }
}
public void LoadData()
{
SqlConnection conn = new SqlConnection("Data Source=mycbj01psql03\\sandbox01;Initial Catalog=DBRMS;Integrated Security=True");
conn.Open();
SqlCommand command = new SqlCommand("p_Date_sel", conn);
command.CommandType = CommandType.StoredProcedure;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
_dateTypeID = Convert.ToInt16(reader["DateTypeID"]);
_date = Convert.ToDateTime(reader["Date"]);
_name = reader["Name"].ToString();
_notes = reader["Notes"].ToString();
}
reader.Close();
conn.Close();
}
}
}
我的主要计划
namespace ExecuteStoredProcedure
{
class TestClass
{
public static void Main(string[] args)
{
Program p = new Program(5);
p.LoadData();
Console.WriteLine("DateID = " + "" + p.dateID);
Console.WriteLine("DateTypeID = " + "" + p.dateTypeID);
Console.WriteLine("Date = " + "" + p.date);
Console.WriteLine("Name = " + "" + p.name);
Console.WriteLine("Notes = " + "" + p.notes);
Console.ReadLine();
}
}
}
答案 0 :(得分:1)
嗯,你需要为@DateID
提供一个值!
您的存储过程需要一个名为@DateID
或类型INT
的参数:
ALTER procedure [dbo].[p_Date_sel]
@DateID int
但在你的电话中,你永远不会提供这样的价值!
您需要做的是创建一个SqlParameter
并将该值传递给存储过程。另外,我强烈建议您为sql客户端代码使用using() {...}
构造。作为最后一条建议:为什么不将DateID
方法设为LoadData()
?你现在拥有它的方式,你必须为你想要检索的每个值创建一个类的实例 - 不是非常有效和有用....
public void LoadData(int dateID)
{
using(SqlConnection conn = new SqlConnection("Data Source=mycbj01psql03\\sandbox01;Initial Catalog=DBRMS;Integrated Security=True"))
{
using(SqlCommand command = new SqlCommand("p_Date_sel", conn))
{
command.CommandType = CommandType.StoredProcedure;
// define the parameter and give it a value!
command.Parameters.Add("@DateID", SqlDbType.Int);
command.Parameters["@DateID"].Value = dateID;
conn.Open();
using(SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
_dateTypeID = Convert.ToInt16(reader["DateTypeID"]);
_date = Convert.ToDateTime(reader["Date"]);
_name = reader["Name"].ToString();
_notes = reader["Notes"].ToString();
}
reader.Close();
}
conn.Close();
}
}
这样,你应该让你的存储过程正常工作。从您的主应用程序中调用它:
Program p = new Program();
p.LoadData(5);