过程或函数'SelectInformation'需要参数'@Name',这是未提供的 [英] Procedure or function 'SelectInformation' expects parameter '@Name', which was not supplied
问题描述
public DataSet SelectData(object[] o, string str)
{
if (o.Length == 0)
{
try
{
da = new SqlDataAdapter();
ds = new DataSet();
da.SelectCommand.CommandText = str;
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
}
catch (Exception ex)
{
throw ex;
}
}
else
{
try
{
int a = o.Length;
// Create Object to get store procedure parameters
object[] abc = new object[a];
// Check Connection Open or Close
if (con.State == ConnectionState.Closed)
{
con.Open();
}
// Start Code for get sql parameter from Stored Procedure
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = con;
myCommand.CommandText = str;
myCommand.CommandType = CommandType.StoredProcedure;
SqlCommandBuilder.DeriveParameters(myCommand);
for (int i = 0; i < myCommand.Parameters.Count - 1; i++)
{
abc[i] = myCommand.Parameters[i+1].ParameterName.ToString();
}
cmd = new SqlCommand(str,con);
for (int i = 0; i < o.Length; i++)
{
SqlParameter sp = new SqlParameter();
sp.ParameterName = abc[i].ToString();
sp.Value = o[i];
sp.SqlDbType = SqlDbType.VarChar;
cmd.Parameters.Add(sp);
//cmd.Parameters.AddWithValue(abc[i].ToString(), o[i]);
}
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds);
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
return ds;
}
//在上面的功能我传递两个参数首先是对象而另一个是存储过程名称
//此函数用于使用存储过程从任何数据库表中获取数据。
//例如,以下存储过程
// In above function I pass two arguments first is object and other is stored procedure name
// this function is used to fetch data from any database table using stored procedure.
// For example,following stored procedure
ALTER PROCEDURE dbo.SelectInformation
@PName varchar(50)
AS
select * from Information where IName =@PName
RETURN
//当我在上面使用上面的代码时出现以下错误
程序或功能'' SelectInformation''期望参数''@PName'',未提供
//When I using above code at that time i got following error
Procedure or function ''SelectInformation'' expects parameter ''@PName'', which was not supplied
推荐答案
da = new SqlDataAdapter()
ds = new DataSet();
da.SelectCommand.CommandText = str;
da.SelectCommand.Parameters.Add("@PName", "your name or variable");
da.SelectCommand.CommandType = CommandType.StoredProcedure;
da.Fill(ds);
您在调用时错过了添加参数PName通过DataAdopter存储过程。尝试以粗体添加行。希望这可以帮到你。
You missed adding parameter PName while calling the stored proc via DataAdopter. Try adding line in bold. Hope this may help you.
这篇关于过程或函数'SelectInformation'需要参数'@Name',这是未提供的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!