我相信在网上的某个地方有一个关于这个话题的讨论,但我丢失了网址,我无法通过谷歌搜索找到它。
我现在可能会尝试的是:
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);
}
但是,我不太确定这是否是正确的方法,或者是否存在更好的方法。
答案 0 :(得分:2)
这适用于我(带有参数和动态结果表的存储过程):
// 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));
}
答案 1 :(得分:1)
我在ActiveRecord代码中实现存储过程时使用的博客是Rodj(http://blog.rodj.org/archive/2008/05/23/activerecord-nhibernate-and-sql-stored-procedures.aspx)的这篇文章。使用他的帖子和评论,我能够使这个工作。我还没有决定这是否是最好的方式。
答案 2 :(得分:1)
你也可以通过以下方式获得IDbConnection:
ActiveRecordMediator.GetSessionFactoryHolder()
.GetSessionFactory(typeof(ActiveRecordBase))
.ConnectionProvider.GetConnection();
答案 3 :(得分:0)
对于ActiveRecord版本1,这适用:
IDbConnection connection = ActiveRecordMediator.GetSessionFactoryHolder()
.GetSessionFactory(typeof(ActiveRecordBase))
.OpenSession().Connection;
答案 4 :(得分:-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");
}