Accessing SSAS cubes using WCF data services - ssas

I have a requirement by which I need to retrieve data from SSAS cubes and send it over to OData. How can that be done? Any help will be greatly appreciated. Any pointers to sample code will also be greatly appreciated.
Thanks and regards,
Venkatesh. S

you can use ADOMD.Net to execute the query and send the result with wcf. Below is the class that I usually use.
class MDXQueryEngine : IMDXQueryEngine
{
private readonly string _serverName;
private readonly string _initalCatalog;
public MDXQueryEngine(string serverName, string initalCatalog)
{
_serverName = serverName;
_initalCatalog = initalCatalog;
}
public TResult Execute<TResult>(MDXQuery query, Func<IDataReader, TResult> work)
{
var connectionString = string.Format("Data Source={0}; Initial Catalog={1}", _serverName, _initalCatalog);
TResult result;
using (var conn = new AdomdConnection(connectionString))
{
conn.Open();
using(var cmd = conn.CreateCommand())
{
cmd.CommandText = query.Expression;
using (var reader = cmd.ExecuteReader())
{
result = work(reader);
}
}
}
return result;
}
}

You can query SSAS cubes using LINQ and ADO.NET Entity Framework.
You will need SSAS Entity Framework Provider in order to be able to do that:
http://agiledesignllc.com/products
This option makes building WCF Data Services straightforward and trivial.

I assume it is SSAS cubes. Not sure about specific frameworks as such to do this.
But one possible solution:
Wrap the Cube (MDX) with a store procedure. (Access SSAS with MDX from SP)
You can create WCF data service, add an EM and specify stored procedure for EM. You should be able to expose them as oData.

Related

EF Core call stored procedure and get list of entity

I use EF Core and SQL Server local for my ASP.NET Core backend. In SQL Server, I have a long complex parametizered stored procedure with multiple joins.
I call it like this:
[HttpGet("plantpl/{plantId}&{year}&{costAccTypeId}")]
public IActionResult GetPlantPl(long plantId, int year, long costAccTypeId)
{
SqlParameter plantIdParam = new SqlParameter("#PlantId", plantId);
SqlParameter yearParam = new SqlParameter("#Year", year);
SqlParameter costAccTypeIdParam = new SqlParameter("#CostAccTypeId", costAccTypeId);
var plantPLRows = DataContext.PlantPLRows.FromSqlRaw("PlantPl #PlantId, #Year, #CostAccTypeId",
plantIdParam, yearParam, costAccTypeIdParam);
return Json(plantPLRows);
}
In order to use the FromSqlRaw method, I had to create a model class PlantPLRow and the corresponding DbSet PlantPLRows.
Basically, I just want to get some data back from the database and send it to the client. I don't want a new DbSet nor migration for this. But to use the FromSqlRaw I have to use it.
So my question: is this the right way for my situation?
#Sergey, the API call, it's signature/attribute and the controller code are fine and reached, no need to change.
This blog post from #ErikEj gave the hint. I had to add this lines of code to my DataContext class:
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<PlantPLRow>().HasNoKey();
}

Using ASP.NET Core Web API WITHOUT Entity Framework

I need to build a Web API from ASP.NET Core without Entity Framework. It's an existing database that has some custom stored procedures and we do not want to use EF.
I searched this topic and can't find anything about it, is this even possible?
This is possible.
The first problem you will run into is getting the database connection string. You will want to import the configuration to do so. In a controller, it might look like this:
private readonly IConfiguration _configuration;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration)
{
_logger = logger;
_configuration = configuration;
}
Add using System.Data and using System.Data.SqlClient (you'll need NuGet for SqlClient) as well as using Microsoft.Extensions.Configuration. With access to the database, you are writing code "old style", for example:
[HttpGet]
[Route("[controller]/movies")]
public IEnumerable<Movie> GetMovies()
{
List<Movie> movies = new List<Movie>();
string connString = ConfigurationExtensions.GetConnectionString(_configuration, "RazorPagesMovieContext");
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM Movie", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
DataTable dt = ds.Tables[0];
sda.Dispose();
foreach (DataRow dr in dt.Rows)
{
Movie m = new Movie
{
ID = (int)dr["ID"],
Title = dr["Title"].ToString(),
ReleaseDate = (DateTime)dr["ReleaseDate"],
Genre = dr["Genre"].ToString(),
Price = (decimal)dr["Price"],
Rating = dr["Rating"].ToString()
};
movies.Add(m);
}
conn.Close();
return movies.ToArray();
}
The connection string name is in appsettings.json.
"ConnectionStrings": {
"RazorPagesMovieContext": "Server=localhost;Database=Movies;Trusted_Connection=True;MultipleActiveResultSets=true"
}
Yes it is possible. Just implement the API by yourself. Or here is also sample for the identity scaffold, without EF.
https://markjohnson.io/articles/asp-net-core-identity-without-entity-framework/
Just used Dapper as our ORM in a project rather than EF.
https://dapper-tutorial.net/
It is similar to ADO.Net, but it has some additionally features that we leveraged and it was really clean to implement.
I realize this is an old question, but it came up in a search I ran so I figured I'd add to the answers given.
First, if the custom stored procedures are your concern, you can still run them using Entity Framework's .FromSql method (see here for reference: https://www.entityframeworktutorial.net/efcore/working-with-stored-procedure-in-ef-core.aspx)
The relevant info is found at the top of the page:
EF Core provides the following methods to execute a stored procedure:
1. DbSet<TEntity>.FromSql(<sqlcommand>)
2. DbContext.Database.ExecuteSqlCommand(<sqlcommand>)
If you are avoiding Entity Framework for other reasons, it's definitely possible to use any database connection method you want in ASP.NET Core. Just implement your database connection methods using whatever library is relevant to your database and set up your controller to return the data in whatever format you want. Most, if not all, of Microsoft's examples return Entity Framework entities, but you can easily return any data format you want.
As an example, this controller method returns a MemoryStream object after running a query against an MS SQL server (note, in most cases where you want data returned it's my understanding that it should be a "GET" method, not "POST" as is done here, but I needed to send and use information in the HttpPost body)
[HttpPost]
[Route("Query")]
public ActionResult<Stream> Query([FromBody]SqlDto content)
{
return Ok(_msSqlGenericService.Query(content.SqlCommand, content.SqlParameters));
}
Instead of a MemoryStream, you could return a generic DataTable or a List of any custom class you want. Note that you'll also need to determine how you are going to serialize/deserialize your data.

Example of using ADO.NET with Oracle in WCF

Without any Linq or Entity Framework, I would like to see an example of using ADO.NET with Oracle in WCF. I have seen the ABC's and the different contracts, but with say consuming a Restful WCF service sending in 1 to several parameters I would like to see an example of using this type of code:
connection = new OracleConnection(EnvironmentSettings.connectionString);
connection.Open();
command = new OracleCommand("H16.WEB_FACILITY.get_facility_info", connection);
command.CommandType = CommandType.StoredProcedure;
// Input Parameters
command.Parameters.Add("pfcode", OracleDbType.Varchar2, facilityCode, ParameterDirection.Input);
// Output Parameters
command.Parameters.Add("pfacility", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
adapter = new OracleDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
So that I can do CRUD operations will a WCF good practice design. Fault Contracts / Data Contracts etc... I see many examples, but not specific to something that seems so simple. I guess that it why so many people are still doing asmx ... I'm wanting to convert a project I am on and I see tons of asmx web services everywhere and wish for an expert or someone who has done this to point my in the right direction or even better show me how to write that ADO code into WCF ... Thanks in advance.
Slightly confusted as to exactly what you mean, but Linq and EntityFramework have nothing to do with WCF, and the paradigm doesn't change one bit in using them. You could do something as simple as:
[ServiceContract]
public class MyService
{
[OperationContract]
public DataSet LoadData(string facilityCode)
{
command = new OracleCommand("H16.WEB_FACILITY.get_facility_info", connection);
command.CommandType = CommandType.StoredProcedure;
// Input Parameters
command.Parameters.Add("pfcode", OracleDbType.Varchar2, facilityCode, ParameterDirection.Input);
// Output Parameters
command.Parameters.Add("pfacility", OracleDbType.RefCursor).Direction = ParameterDirection.Output;
adapter = new OracleDataAdapter(command);
DataSet ds = new DataSet();
adapter.Fill(ds);
return ds;
}
}
In practice you would probably want to use a [DataContract] class, and return that instead of a DataSet, but the only real change would be reading your results into a real class instead of a DataSet, something like:
[DataContract]
public class MyData
{
[DataMember]
public string Facility { get; set; }
}
Then your service method returns that instead of DataSet:
[OperationContract]
public MyData LoadData(string facilityCode)
{
MyData data;
// read from Oracle into data object...
return data;
}
You can also look at WCF Transaction Flow to control your database transaction scope. It is a nice way to have every WCF service operation be trapped in its own transaction scope, or even control the transaction from the WCF client if needed.
[FaultContract]s are a subject on their own, but you can find some good examples if you google for it. Basically you would set up your own exception type, and then add that to the service like:
[ServiceContract]
[FaultContract(typeof(MyException))]
public class MyService
and that tells WCF to add the serialization info for MyException to the WSDL, so then your operations can throw new MyException(); and that will serialize back to the clients, so they will get your exception.

Reading and Updating Data Using SqlDataAdapter Question

I'm looking for some examples on how to best use SqlDataAdapter to access and update data in my application.
Right now I have something like this:
SqlDataAdapter adapter;
DataSet myData = MyDataAccessClass.GetData("Select * from Students", ref adapter);
// change some data here and save changes
adapter.Update();
All of this occurs in code behind, and I dont really like it at all.
So, I'm trying to find a way to do something like this:
DataSet myData = MyDataAccessClass.GetStudents();
// change some data and save changes
MyDataAccessClass.SaveStudents(myData);
Where SaveStudents method still uses SqlDataAdapter to update db.
Any ideas on how to make this work or some pointers to best practices of doing
something like this are highly appreciated. Thank you.
That seems like a fairly basic Data Access Layer implementation, to me. Generally, I do it something like this:
public class MyDataAccessClass
{
private string ConnString;
public MyDataAccessClass()
{ //Get connection string from configuration file }
public MyDataAccessClass(string connString)
{ ConnString = connString; }
public DataSet GetAllStudents()
{
//your SQL Adapter code here...
}
}
One note that I'd make is that with so many ORM solutions (including just Entity Framework and Linq2Sql) available, you may want to consider using collections of objects instead of data-sets for your Data Representations. Then you can have a method like:
public void CreateUpdateStudent(Student student)
{
//update database
}
That's fairly subjective, I'll admit, but I find it preferable to using straight DataSets.
If you want to get update data using the sql-data-adapter then you could use these
Using System.Data.SqlClient;
SqlConnection con = new SqlConnection("Data Source=abcd-pc;Initial Catalog=user_info;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();
try
{
da.UpdateCommand = new SqlCommand("Update logindemo set password=#pswd where username=#uname",con);
da.UpdateCommand.Parameters.Add("#pswd", SqlDbType.VarChar).Value = txtpass.Text;
da.UpdateCommand.Parameters.Add("#uname", SqlDbType.VarChar).Value = txtusername.Text;
con.Open();
da.UpdateCommand.ExecuteNonQuery();
Label1.Text = "Data Updated";
con.Close();
}
catch
{
Label1.Text = "Unable To Connect";
}
I hope you understand how to update the data easily. It just like the example. You can use these type of example in Inserting into the Data, and Deleting the Data with using specific the command and sql query as it required.

How to intercept and modify SQL query in Linq to SQL

I was wondering if there is any way to intercept and modify the sql generated from linq to Sql before the query is sent off?
Basically, we have a record security layer, that given a query like 'select * from records' it will modify the query to be something like 'select * from records WHERE [somesecurityfilter]'
I am trying to find the best way to intercept and modify the sql before its executed by the linq to sql provider.
Ok, first to directly answer your question (but read on for words of caution ;)), there is a way, albeit a finicky one, to do what you want.
// IQueryable<Customer> L2S query definition, db is DataContext (AdventureWorks)
var cs = from c in db.Customers
select c;
// extract command and append your stuff
DbCommand dbc = db.GetCommand(cs);
dbc.CommandText += " WHERE MiddleName = 'M.'";
// modify command and execute letting data context map it to IEnumerable<T>
var result = db.ExecuteQuery<Customer>(dbc.CommandText, new object[] { });
Now, the caveats.
You have to know which query is generated so you would know how to modify it, this prolongs development.
It falls out of L2S framework and thus creates a possible gaping hole for sustainable development, if anyone modifies a Linq it will hurt.
If your Linq causes parameters (has a where or other extension causing a WHERE section to appear with constants) it complicates things, you'll have to extract and pass those parameters to ExecuteQuery
All in all, possible but very troublesome. That being said you should consider using .Where() extension as Yaakov suggested. If you want to centrally controll security on object level using this approach you can create an extension to handle it for you
static class MySecurityExtensions
{
public static IQueryable<Customer> ApplySecurity(this IQueryable<Customer> source)
{
return source.Where(x => x.MiddleName == "M.");
}
}
//...
// now apply it to any Customer query
var cs = (from c in db.Customers select c).ApplySecurity();
so if you modify ApplySecurity it will automatically be applied to all linq queries on Customer object.
If you want to intercept the SQL generated by L2S and fiddle with that, your best option is to create a wrapper classes for SqlConnection, SqlCommand, DbProviderFactory etc. Give a wrapped instance of SqlConnection to the L2S datacontext constructor overload that takes a db connection. In the wrapped connection you can replace the DbProviderFactory with your own custom DbProviderFactory-derived class that returns wrapped versions of SqlCommand etc.
E.g.:
//sample wrapped SqlConnection:
public class MySqlConnectionWrapper : SqlConnection
{
private SqlConnecction _sqlConn = null;
public MySqlConnectionWrapper(string connectString)
{
_sqlConn = new SqlConnection(connectString);
}
public override void Open()
{
_sqlConn.Open();
}
//TODO: override everything else and pass on to _sqlConn...
protected override DbProviderFactory DbProviderFactory
{
//todo: return wrapped provider factory...
}
}
When using:
using (SomeDataContext dc = new SomeDataContext(new MySqlConnectionWrapper("connect strng"))
{
var q = from x in dc.SomeTable select x;
//...etc...
}
That said, do you really want to go down that road? You'll need to be able to parse the SQL statements and queries generated by L2S in order to modify them properly. If you can instead modify the linq queries to append whatever you want to add to them, that is probably a better alternative.
Remember that Linq queries are composable, so you can add 'extras' in a separate method if you have something that you want to add to many queries.
first thing come to my mind is to modify the query and return the result in Non-LINQ format
//Get linq-query as datatable-schema
public DataTable ToDataTable(System.Data.Linq.DataContext ctx, object query)
{
if (query == null)
{
throw new ArgumentNullException("query");
}
IDbCommand cmd = ctx.GetCommand((IQueryable)query);
System.Data.SqlClient.SqlDataAdapter adapter = new System.Data.SqlClient.SqlDataAdapter();
adapter.SelectCommand = (System.Data.SqlClient.SqlCommand)cmd;
DataTable dt = new DataTable("sd");
try
{
cmd.Connection.Open();
adapter.FillSchema(dt, SchemaType.Source);
adapter.Fill(dt);
}
finally
{
cmd.Connection.Close();
}
return dt;
}
try to add your condition to the selectCommand and see if it helps.
Try setting up a view in the DB that applies the security filter to the records as needed, and then when retrieving records through L2S. This will ensure that the records that you need will not be returned.
Alternatively, add a .Where() to the query before it is submitted that will apply the security filter. This will allow you to apply the filter programmatically (in case it needs to change based on the scenario).