我是新的接口數據庫的應用程序,並試圖從數據庫中我指定的參數應過濾掉結果的幾個領域。我一直收到一個沒有參數或參數提供。任何人都可以對此有所瞭解嗎?謝謝。通過存儲過程傳遞參數
下面是存儲過程:
ALTER PROC dbo.PassParamUserID
AS
set nocount on
DECLARE @UserID int;
SELECT f_Name, l_Name
FROM tb_User
WHERE tb_User.ID = @UserID;
這裏是我的代碼
class StoredProcedureDemo
{
static void Main()
{
StoredProcedureDemo spd = new StoredProcedureDemo();
//run a simple stored procedure that takes a parameter
spd.RunStoredProcParams();
}
public void RunStoredProcParams()
{
SqlConnection conn = null;
SqlDataReader rdr = null;
string ID = "2";
Console.WriteLine("\n the customer full name is:");
try
{
//create a new connection object
conn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=c:\\Program Files\\Microsoft SQL Server\\MSSQL10.SQLEXPRESS\\MSSQL\\DATA\\UserDB.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True; Integrated Security=SSPI");
conn.Open();
//create command objects identifying the stored procedure
SqlCommand cmd = new SqlCommand("PassParamUserID", conn);
//Set the command object so it know to execute the stored procedure
cmd.CommandType = CommandType.StoredProcedure;
//ADD PARAMETERS TO COMMAND WHICH WILL BE PASSED TO STORED PROCEDURE
cmd.Parameters.Add(new SqlParameter("@UserID", 2));
//execute the command
rdr = cmd.ExecuteReader();
//iterate through results, printing each to console
while (rdr.Read())
{
Console.WriteLine("First Name: {0,25} Last Name: {0,20}", rdr["f_Name"], rdr["l_Name"]);
}
}
謝謝。這工作。這是否意味着我在創建S_Proc時使用了DECLARE @UserID,然後在它進入Alter時將其更改爲@UserID? – jpavlov 2011-01-11 18:20:01