Running stored procedures in your own model using servicestack ormlite

Are there any examples for running a stored procedure on serviceStack MVC using ormlite? mifts? saw this block of code:

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>();
                       }
                    )};

but came up with text that never worked in google groups!

I can also write this:

using(var db = new SwitchWizardDb())
            {
             var results2 = db.dbCmd.ExecuteProcedure()   
            }

but not sure how to do this with the parameters, and in the source code that I was looking at, he said that he was deprecated?

thank

+5
source share
4 answers

Well, I thought it was best to flip my own handler to create this, any thoughts would be very welcome, especially with how I could pass parameters into some kind of function or something like that:

I have a main class for easy access to my connection object:

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();
    }
}

:

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 };
            }

!

+2

, 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 });

github .

+18

Here is an example of starting a stored procedure using ormLite, which may help you:

IList<MyDTO> myList = DbFactory.Run(dbCnx =>
{
 using (var dbCmd = dbCnx.CreateCommand())
 {
      dbCmd.CommandType = CommandType.StoredProcedure;
      dbCmd.CommandText = "mySchema.myStoredProc";
      dbCmd.Parameters.Add(new SqlParameter("@param1", val1));
      dbCmd.Parameters.Add(new SqlParameter("@param2", val2));

      var r = dbCmd.ExecuteReader();
      return r.ConvertToList<MyDTO>();
  }
 });
0
source

To just start a stored procedure without returning data:

 public class ComsManager : Dbase
 {   
    private IDbConnection dbConn;

    public ComsManager()
    {
        dbConn = Dbase.GetConnection();
    }

    public void Housekeeping()
    {
        using(var dbCmd = dbConn.CreateCommand())
            dbConn.Exec(res => { dbCmd.CommandType = CommandType.StoredProcedure; dbCmd.CommandText = "SP_housekeeping"; dbCmd.ExecuteNonQuery(); });
    }
0
source

All Articles