Pregunta

My stored procedures declaration looks like this:

alter procedure [dbo].[usp_A_MySproc] 
@aID int,
@bID int,
@cID int

My code in application layer:

 using (var transaction = Session.Transaction)
 {
    try
      {
           transaction.Begin();
           Session.CreateSQLQuery("exec usp_A_MySproc ?, ?, ?")
              .SetParameter("aID", 1)
              .SetParameter("bID", 2)
              .SetParameter("cID", 3);
               transaction.Commit();;
      }
 }

Exception: "could not locate named parameter [aID]"

Can you see something wrong?

Thank you!

¿Fue útil?

Solución 2

You need to set the parameters by position:

 using (var transaction = Session.BeginTransaction())
 {

    Session.CreateSQLQuery("exec usp_A_MySproc ?, ?, ?")
          .SetInt32(0, aIDValue)
          .SetInt32(1, bIDValue)
          .SetInt32(2, cIDValue)
          .ExecuteUpdate();

          transaction.Commit();
 }

Otros consejos

For nhibernate

Session.CreateSQLQuery("exec usp_A_MySproc @aID = 1, @bID =2, @cID = 3")

or

IQuery query = Session.CreateSQLQuery("exec usp_A_MySproc @aID =:aID, @bID =:bID , @cID =:cID");
query.SetInt32("aID",1);
query.SetInt32("bID",2);
query.SetInt32("cID",3);
query.ExecuteUpdate();

or to match what you were doing

Session.CreateSQLQuery("exec usp_A_MySproc @aID =:aID, @bID =:bID , @cID =:cID")
    .SetParameter("aID", 1)
    .SetParameter("bID", 2)
    .SetParameter("cID", 3);

For use of SqlCommand modify the code to the following, connectionString stands for your connection string

using (SqlConnection con = new SqlConnection(connectionString))
{    
    SqlCommand cmd = new SqlCommand("usp_A_MySproc ", con);
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add("@aID", SqlDbType.Int32).Value = 1;
    cmd.Parameters.Add("@bID", SqlDbType.Int32).Value = 2;
    cmd.Parameters.Add("@cID", SqlDbType.Int32).Value = 3;

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

or

using (SqlConnection con = new SqlConnection(connectionString))
{    
    SqlCommand cmd = new SqlCommand("usp_A_MySproc ", con);
    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.AddWithValue("@aID", 1) ;
    cmd.Parameters.AddWithValue("@bID", 2) ;
    cmd.Parameters.AddWithValue("@cID", 3) ;

    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

refer to MSDN article on SQLCommand how to properly add parameters

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters(v=vs.110).aspx

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top