我想知道如何获取存储过程的参数。我是 ASP.Net C# 的新手。这是我的存储过程。
ALTER procedure [dbo].[obtnCtUsuarios]
((@nombre nvarchar (20))
AS
SELECT
id, idEmpresa
FROM
ctUsuarios
WHERE
nombre = @nombre
ORDER BY
idNombre DESC
我在这里遇到错误,因为我不知道如何放置参数。
public List<ctUsuarios> select_ALL_ctUsuarios()
{
List<ctUsuarios> LstctUsuarios = new List<ctUsuarios>();
string storedProcedure = "obtnCtUsuarios";
using (DbConnection con = Conexion.dpf.CreateConnection())
{
con.ConnectionString = Conexion.constr;
using (DbCommand cmd = Conexion.dpf.CreateCommand())
{
cmd.Connection = con;
cmd.CommandText = storedProcedure;
cmd.CommandType = CommandType.StoredProcedure;
con.Open();
using (DbDataReader dr = cmd.ExecuteReader())
{
while (dr.Read())
{
LstctUsuarios.Add(
new ctUsuarios((int)dr["idUsuario"],
(string)dr["id"],
(string)dr["idEmpresa"]));
}
}
}
}
return LstctUsuarios;
}
这是我的连接和我的ExecuteNonQuery
.
public class Conexion
{
public Conexion()
{ }
public static string constr
{
get { return ConfigurationManager.ConnectionStrings["Conn"].ConnectionString; }
}
//Cliente
public static string Provider
{
get { return ConfigurationManager.ConnectionStrings["Conn"].ProviderName; }
}
public static DbProviderFactory dpf
{
get
{
return DbProviderFactories.GetFactory(Provider);
}
}
private static int ejecutaNonQuery(string storedProcedure, List<DbParameter> parametros)
{
int Id = 0;
try
{
using (DbConnection con = Conexion.dpf.CreateConnection())
{
con.ConnectionString = Conexion.constr;
using (DbCommand cmd = Conexion.dpf.CreateCommand())
{
cmd.Connection = con;
cmd.CommandText = storedProcedure;
cmd.CommandType = CommandType.StoredProcedure;
foreach (DbParameter param in parametros)
cmd.Parameters.Add(param);
con.Open();
Id = cmd.ExecuteNonQuery();
}
}
}
catch (Exception)
{
throw;
}
finally
{
//conection.close();
}
return Id;
}
有人可以帮我获取参数吗?谢谢