Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: Is there a way to setup/define dynamic parameter as in Dapper? #1051

Closed
rafek1241 opened this issue Jul 14, 2022 · 8 comments
Closed
Labels
question Further information is requested

Comments

@rafek1241
Copy link

rafek1241 commented Jul 14, 2022

I've got RAW SQL + expando object with parameters list and in the database field that I'm looking for is of type VARCHAR(200) and my string parameter is parsed by repoDB to NVARCHAR(XXX) which causes slow performance when querying data.

I mitigate this issue by using:

            var @params = new
            {
                Id = id
            };

            TypeMapper.Add<string>(DbType.AnsiString);
            var data = await connection.ExecuteQueryMultipleAsync(query, @params);
            TypeMapper.Remove<string>();

but I'd rather to have something like in dapper:

var parameters = new DynamicParameters();
var customerId = "ALFKI";
parameters.Add("@CustomerId", customerId, DbType.String, ParameterDirection.Input, customerId.Length);
var sql = "select * from customers where CustomerId = @CustomerId";
using (var connection = new SqlConnection(connString))
{
    var customer = connection.QuerySingle<Customer>(sql, parameters);
}

reference: https://www.learndapper.com/parameters

Is it doable in the repodb? Can we select datatype for a query parameters?

@rafek1241 rafek1241 added the question Further information is requested label Jul 14, 2022
@mikependon
Copy link
Owner

AFAIK, RepoDB is leveraging the default behavior of ADO.NET when passing the parameters via parameterized SQL. But allow us to double check this.

There is no other way to override the data type you passed into the parameter, unlesd of course you use the type mapper.

Have you also tried using the QueryField and see if that has change the thing? See here

@rafek1241
Copy link
Author

@mikependon
For that approach what is missing for me is that I can't explicitly decide what DBType will that be

@rafek1241
Copy link
Author

Example from https://repodb.net/operation/executequery#queryfieldquerygroup:

  var param = new QueryGroup(new []
    {
        new QueryField("Id", 10045)
    });

I wish to have:

  var param = new QueryGroup(new []
    {
        new QueryField("Id", DBType.AnsiString,10045)
    });

@rafek1241
Copy link
Author

If we would like to have it ADO.NET-like then at least something like this would be great:

 var results = await _connection.ExecuteQueryMultipleAsync(
                query,
                new { Id = new Microsoft.Data.SqlClient.SqlParameter("Id", SqlDbType.VarChar) }
            );

@mikependon
Copy link
Owner

Yes, we got it. But, there is a logic behind where RepoDB auto detects the type of the column from the DB table and associate the proper type to your command parameter, but AFAIK, by executing via raw SQL it is leveraging the default behavior of ADO. So the NVARCHAR(*) is something like ADO.NET default behavior.

@rafek1241
Copy link
Author

Unfortunately using classes and no raw SQL is not an option in the company that I'm working in. I will create enhancement request for QueryField datatype parameter possibility.

@mikependon
Copy link
Owner

That would work. We will look on this enhancement request afterwards.

@rafek1241
Copy link
Author

No description provided.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants