使用servicestack ormlite将存储过程运行到自己的模型中

dav*_*der 7 servicestack

是否有任何使用ormlite在serviceStack MVC上运行存储过程的示例?神话?看到这段代码:

var results = new List<EnergyCompare>
                    {dbFactory.Exec(dbCmd =>
                       {
                          dbCmd.CommandType = CommandType.StoredProcedure;
                          dbCmd.Parameters.Add(new SqlParameter("@id", 1));
                          dbCmd.CommandText = "GetAuthorById";
                          return dbCmd.ExecuteReader().ConvertTo<EnergyCompare>();
                       }
                    )};
Run Code Online (Sandbox Code Playgroud)

但附带的文章从未在谷歌团体工作过!

我也可以这样写:

using(var db = new SwitchWizardDb())
            {
             var results2 = db.dbCmd.ExecuteProcedure()   
            }
Run Code Online (Sandbox Code Playgroud)

但不知道如何用参数完成这个,在我看过的源代码中,它说过时了吗?

谢谢

Zac*_*tes 23

看起来ServiceStack.ORMLite已经更新,以使这更容易:

List<Poco> results = db.SqlList<Poco>("EXEC GetAnalyticsForWeek 1");
List<Poco> results = db.SqlList<Poco>("EXEC GetAnalyticsForWeek @weekNo", new { weekNo = 1 });

List<int> results = db.SqlList<int>("EXEC GetTotalsForWeek 1");
List<int> results = db.SqlList<int>("EXEC GetTotalsForWeek @weekNo", new { weekNo = 1 });
Run Code Online (Sandbox Code Playgroud)

此示例位于github repo的首页.


dav*_*der 3

好吧,我认为最好推出自己的处理程序,因此创建了这个,任何想法都将受到欢迎,特别是如何在某种函数或其他内容中传递参数:

我有一个主类来处理对连接对象的轻松访问:

public class DatabaseNameSp : IDisposable
{
    private readonly SqlConnection _spConn = new SqlConnection(DatabaseNameSp .dbConString);
    public readonly SqlCommand SpCmd;

    public DatabaseNameSp (string procedureName)
    {
       _spConn.Open();

        SpCmd = new SqlCommand
                    {
                        Connection = _spConn,
                        CommandType = CommandType.StoredProcedure,
                        CommandText = procedureName
                    };
    }

    public void Dispose()
    {
         _spConn.Close();
         SpCmd.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

用法:

using (var db = new DatabaseNameSp ("procedurenname"))
            {
                db.SpCmd.Parameters.Add(new SqlParameter("@Id", 1));

                var rdr = db.SpCmd.ExecuteReader(CommandBehavior.CloseConnection);

                var results = new List<CustomDTO>();
                while (rdr.Read())
                {
                    results.Add(new CustomDTO { Name = rdr["name"].ToString(), Id = rdr["id"].ToString() });
                }
                return new CustomDTOResponse { Results = results };
            }
Run Code Online (Sandbox Code Playgroud)

有什么想法吗 !

谢谢