如何使用带有选择,删除,更新命令的存储过程读取数据 [英] How to Reading data using Store procedure with Select ,delete, Update commands

查看:66
本文介绍了如何使用带有选择,删除,更新命令的存储过程读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里我需要使用存储过程读取数据
我已经创建了一个像这样的程序

Here i need to read data using the Store procedure
i have create a procedure like this

create procedure Raghu(@ID int,@Name nvarchar(60),@Location nvarchar(60) )
AS
Begin
select*from Emp;//it is table name
End


同样,我已经创建了存储过程insert,delete,update
但是我想知道如何在Cs文件的Asp.net编码中使用这些存储过程.
对于选择",应在网格视图中显示
对于insert命令,它可以正常工作,但不适用于Select,Delete和Update

对于选择命令,我有这样的返回代码


like similarly i have created Store Procedures insert,delete,update also
but i want know how to use these store procedures in Asp.net coding in Cs file
for Select it should Display in grid view
For insert command it working but it is not working for the Select , Delete and Update

for select command i have return code like this

SqlConnection sqlConnection1 = new SqlConnection("Data Source=A10;Initial Catalog=raghu;Integrated Security=True");
        SqlCommand cm = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        try
        {
            cm = new SqlCommand("Raghu", sqlConnection1);
            cm.Parameters.Add("@ID", SqlDbType.Int);
            cm.Parameters.Add("@Name", SqlDbType.NVarChar);
            cm.Parameters.Add("@Location", SqlDbType.NVarChar);
            cm.CommandType = CommandType.StoredProcedure;
            da.SelectCommand = cm;
            da.Fill(dt);
            GridView1.DataSource = dt;
        }
        catch (Exception x)
        {
            Response.Write("x.Message");
        }
        finally
        {
            cm.Dispose();

        } 
    }


你能告诉我这段代码哪里出错的吗
它给出了这样的错误

过程或函数"Raghu"需要未提供的参数"@ID".


can you give any idea where error in this code
it is giving error like this

Procedure or Function ''Raghu'' expects parameter ''@ID'', which was not supplied.

推荐答案

Raghu,


根据您的存储过程,输入参数为:@ ID,@ Name,@ Location.

您刚刚完成
Hi Raghu,


As per your Stored Procedure, the Input Parameters are : @ID, @Name, @Location.

You''ve just done
Parameters.Add()



他们需要一些价值吗?

尝试



They need some value don''t they?

Try

Parameters.AddWithValue()




我想知道的第一件事是为什么要为select *语句传递参数?

另外,您要添加参数,但是您要将参数传递到哪里呢?

检查此链接以了解如何调用存储过程.

从Microsoft.NET调用SQL Server存储过程 [ ^ ]


希望对您有所帮助.


First thing i wanted to know is why are you passing parameters for a select * statement?

Also, you are adding the parameters.But where are you passing the values?

check this link on how to call the stored procedures.

Calling SQL Server stored procedures from Microsoft.NET[^]


Hope this helps.


Hm-m-m-m....我看到了您的错误;)
Hm-m-m-m.... I see your the error ;)
cm = new SqlCommand("Raghu", sqlConnection1);
cm.Parameters.Add("@ID", SqlDbType.Int);
cm.Parameters.Add("@Name", SqlDbType.NVarChar);
cm.Parameters.Add("@Location", SqlDbType.NVarChar);
cm.CommandType = CommandType.StoredProcedure;


您添加参数,但是此参数的值在哪里?
像这样的东西:


you add parameters, but where are values for this parameters?
something like this:

cm.Parameters[0] = 1;
cm.Parameters[1] = "myName";
cm.Parameters[2] = "myLocation";


你也在用方法


also you are using method

public SqlParameter Add(string parameterName, SqlDbType sqlDbType);


试试


try

public SqlParameter Add(string parameterName, object value);


反而.这是一个示例:


instead. here is an example:

cm.Parameters.Add("@ID",Convert.ToInt32(1))


这篇关于如何使用带有选择,删除,更新命令的存储过程读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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