C# 使用控制台从存储过程获取输出
嗨,我正试图从控制台生成输出,但一直收到错误消息 程序或功能“p_日期选择” 需要参数'@DateID',该参数为 未提供 假设获取DateID并显示所有数据,请告诉我我做错了什么 下面是我的存储过程C# 使用控制台从存储过程获取输出,c#,C#,嗨,我正试图从控制台生成输出,但一直收到错误消息 程序或功能“p_日期选择” 需要参数'@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 Syste
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();
}
}
}
那么,您需要为
@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);