Hello,
I need to access some tables info present in the enviorment in a c# extension. Basicly what i need is just to query "Select".
I spent a lot of time searching and i still not found the best way to do so.
I was thinking of expose an API with the desired tables and consume that API in the .net extension. (Tho i am not sure how to consume that in integration studio)
Does anyone know the best approach to that?
Thanks,
Ruben
Hi Ruben,
We have an extension that accesses data from an OutSystems database. We created a small class called SqlHelper:
public class SqlHelper { private static SqlCommand GetSqlCommand() { var dbaProvider = DatabaseAccess.ForRunningApplication(); var trans = dbaProvider.GetRequestTransaction(); return (SqlCommand)(trans.CreateCommand().GetDriverCommand()); } public static string SqlCommandExecute(string sqlCommand) { SqlCommand command = SqlHelper.GetSqlCommand(); command.CommandText = sqlCommand; command.CommandType = CommandType.Text; command.CommandTimeout = 15; return command.ExecuteScalar().ToString(); } }
And we invoke it to query data like this:
var result = SqlHelper.SqlCommandExecute($"SELECT Value FROM DYNAMIC_SETTING WHERE Label = '{dynamicSettingName}'");
I haven't written that code myself, so I'm not sure how it works exactly, but no doubt you can google the details.
Thank you @Kilian Hekhuis That was something i was looking for. Will run some tests with that aswell and see whats the best for my case.
Will mark this as solution because it's a more straight foward way to use the enviorment data.
I hope you can make it work for your scenario. Happy coding!
For your use case, if the "Select" query is something you could otherwise attain by using the built-in tools such as Aggregates or Advanced SQL Queries, instead of adding the query to your C# extension somehow I would try to "pass" the result set of the Aggregate/Advanced SQL Query to the action you're creating in the extension.
In general, this means adding an input parameter to your extension action with the desired result (e.g. Primitive variable, List of Records, Text to receive a JSON, etc.) so that you can access/manipulate them in your extension code more easily without having to worry about the database connection overhead. Basically passing the output of the Aggregate/Advanced SQL Query to one (or many) of the input parameters of your action. For most scenarios, I think this should result in more manageable, maintainable code as well.
Hopefully that helps!
Hi @Francisco Calderón,
thank you for the reply.
In my use case i cannot pass the result of an aggregate because i dont know the filter to apply.
my extension is using the flee expression evaluator, and there i have some functions (like built in functions) and i am building custom functions now. That said for the result of some of the new functions i need to query some tables.
Without being familiar with the exact nature of the requirements, I think you could probably adapt to many scenarios by using different actions from your extension, with something such as:
It might not be the ideal or most comfortable, but at a very granular level this should provide a viable workaround for most scenarios (taken to the extreme, this should work for essentially recreating a C# extension line-by-line, perhaps with some limitations such as LINQ statements that do everything in a single step).
Thank you!I think you provided some value to this, i will grab your idea and run some tests,
Can we use logical table name in the query or we need to use physical name of the table ? also will this depend on the environment like personal or production ?