2008-10-09 15 views
8

Bu konu üzerinde net bir yerde bir tartışma olduğuna inanıyorum ama URL'yi kaybettim ve googling yoluyla bulamıyorum. Şu anda deneyebilirsiniz NeKale ActiveRecord'u kullanarak bir saklı yordamı nasıl yürütüyorsunuz?

olacaktır:

ISessionFactoryHolder factoryHolder = ActiveRecordMediator<EntityClass>.GetSessionFactoryHolder(); 
ISession session = factoryHolder.CreateSession(typeof(EntityClass)); 
try 
{ 
    IDbCommand cmd = session.Connection.CreateCommand(); 
    cmd.CommandText = "spName"; 
    cmd.ExecuteNonQuery(); 
} 
catch(Exception ex) 
{ 

} 
finally 
{ 
    factoryHolder.ReleaseSession(session); 
} 

Ancak, ben oldukça bu bunu yapmanın doğru yolu olup olmadığından emin ya da belki daha iyi bir yolu olup olmadığını değilim.

cevap

1

ActiveRecord kodumda saklı yordamları uygularken kullandığım blog bu yazı Rodj tarafından yapıldı (http://blog.rodj.org/archive/2008/05/23/activerecord-nhibernate-and-sql-stored-procedures.aspx). Onun yazı ve yorumları kullanarak bunu işe almak mümkün oldu. Henüz bunun en iyi yolu olup olmadığına karar vermedim. Bu benim için çalışıyor

ActiveRecordMediator.GetSessionFactoryHolder() 
    .GetSessionFactory(typeof(ActiveRecordBase)) 
    .ConnectionProvider.GetConnection(); 
+4

bu bağlantı bozuk ... –

1

Ayrıca bir IDbConnection alabilirsiniz

IDbConnection connection = ActiveRecordMediator.GetSessionFactoryHolder() 
          .GetSessionFactory(typeof(ActiveRecordBase)) 
          .OpenSession().Connection; 
+0

ActiveRecord SSS'ye bakın: http://using.castleproject.org/display/AR/FAQ –

+0

Bu işe yaramıyor gibi görünüyor, ConnectionProvider'ın mevcut olmadığını söylüyor. – Justin

2

(params ve dinamik sonuç tabloyla saklı yordam):

// get Connection 
System.Data.IDbConnection con = ActiveRecordMediator.GetSessionFactoryHolder() 
                .GetSessionFactory(typeof(Autocomplete)) 
                .ConnectionProvider.GetConnection(); 

// set Command 
System.Data.IDbCommand cmd = con.CreateCommand(); 
cmd.CommandText = "name_of_stored_procedure"; 
cmd.CommandType = System.Data.CommandType.StoredProcedure; 

// set Parameter of Stored Procedure 
System.Data.SqlClient.SqlParameter param = new System.Data.SqlClient.SqlParameter("@parameter_name", System.Data.SqlDbType.NVarChar); 
param.Value = "value_of_parameter"; 
((System.Data.SqlClient.SqlParameterCollection)cmd.Parameters).Add(param); 

// call Stored Procedure (without getting result) 
cmd.ExecuteNonQuery(); 

// ... or read results 
System.Data.SqlClient.SqlDataReader r = (System.Data.SqlClientSqlDataReader)cmd.ExecuteReader(); 
while(r.Read()) { 
    System.Console.WriteLine("result first col: " + r.GetString(0)); 
} 
0

ActiveRecord sürüm 1 için, bu işleri:

-1
public ArrayList DevolverCamposDeObjetoSTP(T Objeto, List<Consulta> Consultas, string StoredProcedureName) 
    { 
     ArrayList results; 
     try 
     { 
      var queryString = @"EXEC " + StoredProcedureName; 
      foreach (var consulta in Consultas) 
      { 
       switch (consulta.tipoCampo) 
       { 
        case Consulta.TipoCampo.dato: 
         queryString = queryString + " " + consulta.Campo + " = " + "'" + consulta.Valor + "'"; 
         break; 
        case Consulta.TipoCampo.numero: 
         queryString = queryString + " " + consulta.Campo + " = " + consulta.Valor; 
         break; 
       } 
       queryString = queryString + ","; 
      } 
      queryString = queryString.Remove(queryString.Count() - 1, 1); 
      var query = new HqlBasedQuery(typeof(T),QueryLanguage.Sql, queryString); 
      results = (ArrayList)ActiveRecordMediator.ExecuteQuery(query); 
     } 
     catch (Exception exception) 
     { 
      throw new Exception(exception.Message); 
     } 
     return results; 
    } 
public class Consulta 
{ 
    public enum TipoCampo 
    { 
     dato, 
     numero 
    } 
    public string Campo { get; set; } 
    public TipoCampo tipoCampo { get; set; } 
    public string Valor { get; set; } 
    public string Indicador { get; set; } 
} 
public void _Pruebastp() 
    { 
     var p = new Recurso().DevolverCamposDeObjetoSTP(
                 new Recurso(), 
                 new List<Consulta> { new Consulta { Campo = "@nombre", tipoCampo = Consulta.TipoCampo.dato, Valor = "chr" }, new Consulta { Campo = "@perfil", tipoCampo = Consulta.TipoCampo.numero, Valor = "1" } }, 
                 "Ejemplo"); 
    } 
İlgili konular