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

问题描述

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

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.

推荐答案

Parameters.Add()



他们需要一些价值吗?

尝试



They need some value don''t they?

Try

Parameters.AddWithValue()



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))


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

10-29 16:42