帮助在“搜索"按钮中创建存储过程 [英] help in creating stored procedure in Search Button

查看:53
本文介绍了帮助在“搜索"按钮中创建存储过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为搜索按钮编写基于Storeprocdure的代码.我有一个问题..该代码中的错误是异常:过程或函数GetProducts期望未提供的参数@ProductID ...


Visual Studio C#代码:-

I am writing Storeprocdure based code for search button. i have a problem.. in that code the error is the exception : Procedure or function GetProducts expects Parameter @ProductID Which was not supplied...


Visual Studio C# code:-

string productid = pid.Text;
            string productname = pn.Text;
            SqlConnection conn = new SqlConnection("Data Source=FAROOQPC\\SQLEXPRESS; Database=db_first; Integrated Security=SSPI");
            SqlCommand command = new SqlCommand("GetProducts", conn);
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = productid;
                      
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Products");
            DataTable dt = ds.Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                Password.Text = dr["@ProductName"].ToString();
                //code for remaining text boxes
            }



Sql Server StoredProcedure代码:-



Sql Server StoredProcedure Code:-

set ANSI_NULLS OFF
set QUOTED_IDENTIFIER OFF
GO
ALTER PROCEDURE [dbo].[GetProducts]
(
    @ProductID varchar(50),
    @ProductName varchar(50)
) AS
SELECT ProductID, ProductName From Products where ProductID=@ProductId

推荐答案

尝试使用第三行.
Try using the third line.
string productid = pid.Text;
            string productname = pn.Text;
            SqlConnection conn = new SqlConnection("Data Source=FAROOQPC\\SQLEXPRESS; Database=db_first; Integrated Security=SSPI");
            command.CommandType = CommandType.StoredProcedure;
            command.Parameters.Add("@ProductID", SqlDbType.VarChar).Value = productid;
            command.Parameters.Add("@ProductName", SqlDbType.VarChar).Value = productname;   
            SqlCommand command = new SqlCommand("GetProducts", conn);            
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            DataSet ds = new DataSet();
            adapter.Fill(ds, "Products");
            DataTable dt = ds.Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                Password.Text = dr["@ProductName"].ToString();
                //code for remaining text boxes
            }



希望它能起作用.



Hope it will work.


实际上非常容易. SP中的SQL语句调用必须已经包含参数定义:

It''s actually quite easy. The SQL statement callin you SP has to include the parameter definition already:

SqlCommand command = new SqlCommand("GetProducts @ProductID", conn);



SqlCommand就是真正的名字.为sql命令本身不存在的内容提供参数将使您无所适从.

干杯!

-MRB



The SqlCommand is what will be really called. Supplying a parameter for something that is not present in the sql command itself would get you nowhere.

Cheers!

-MRB


这篇关于帮助在“搜索"按钮中创建存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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