- Tony Meißner
- SnapObjects
- Friday, 25 June 2021 10:05 AM UTC
How can i retrieve Data from Model using the sqlselect attribute and paramvalues?
I already ask a stackoverflow question;
https://stackoverflow.com/questions/68129138
How can I retrieve data from my SQL-Model using the SqlSelect attribute??? For now i do this;
```c#
[SqlParameter(name: "first", dataType: typeof(int))]
[SqlParameter(name: "second", dataType: typeof(string))]
[SqlParameter(name: "third", dataType: typeof(DateTime))]
[FromTable("mytable", Schema = "DBA")]
[SqlSelect("mycustomselect", RawSelect = "someproperty, otherproperty")]
public class MyClass
{
[Key]
[SqlColumn("first")]
public int First { get; set; }
[Key]
[SqlColumn("second")]
public string Second { get; set; }
[Key]
[SqlColumn("third")]
public DateTime Third { get; set; }
...
[SqlColumn("someproperty")]
public int Someproperty { get; set; }
[SqlColumn("otherproperty")]
public int Otherproperty { get; set; }
}
```
in my service I want to load this custom sqlSelect;
```c#
public async Task<MyClass> RetrieveOneAsync(int first, string second, DateTime third, string sqlselect, CancellationToken cancellationToken)
{
Myclass myclass;
var builder = ModelSqlBuilder.GetBuilder<Myclass>(_dataContext)
.GetQueryBuilder(sqlselect);
myclass = await _dataContext.SqlExecutor
.SelectOneAsync<Myclass>(
builder,
new object[]
{
first,
second,
third
},
cancellationToken);
return myclass;
}
```
i also tried it with a ParamValue;
```c#
public async Task<MyClass> RetrieveOneAsync(int first, string second, DateTime third, string sqlselect, CancellationToken cancellationToken)
{
Myclass myclass;
ParamValue firstParam = ParamValue.Input<int>("first", first);
ParamValue secondParam = ParamValue.Input<string>("second", second);
ParamValue thirdParam = ParamValue.Input<DateTime>("third", third);
var builder = ModelSqlBuilder.GetBuilder<Myclass>(_dataContext)
.GetQueryBuilder(sqlselect);
myclass = await _dataContext.SqlExecutor
.SelectOneAsync<Myclass>(
builder,
new object[]
{
firstParam,
secondParam,
thirdParam
},
cancellationToken);
return myclass;
}
```
and my call;
```c#
var myclass = await RetrieveOneAsync(1, "test", DateTime.Now, "mycustomselect", default);
```
Everytime i didnt get the right row. I think there are some problems with my parameters. Maybe the DateTime value cause some problems.
I already read how to get the SQLSelect (this works):
[GetQueryBuilder](https://docs.appeon.com/snapobjects/3.0/api_reference/SnapObjects.Data/ModelSqlBuilder/Method/GetQueryBuilder.html)
[SqlSelectAttribute](https://docs.appeon.com/snapobjects/3.0/api_reference/SnapObjects.Data/ModelAttribute/Class/SqlSelectAttribute/SqlSelectAttribute.html)
but i dont get the right row from the table. Maybe i have to use a different function.
[SelectOneAsync](https://docs.appeon.com/snapobjects/3.0/api_reference/SnapObjects.Data/SqlExecutor/ISqlExecutor/Method/SelectOneAsync.html)
Find Questions by Tag
Helpful?
If a reply or comment is helpful for you, please don’t hesitate to click the Helpful button. This action is further confirmation of their invaluable contribution to the Appeon Community.