Drizin / InterpolatedSql

Sql Builder using Interpolated Strings
MIT License
106 stars 7 forks source link

Unable to do multiple queries using one DBConnection instance #11

Closed AsuraKev closed 7 months ago

AsuraKev commented 7 months ago

Hi there, First of all I am using dapper :)

I am doing two dynamic pagination queries where one will fetch the total count and another one with pagination result. This is how I am trying it out

image

However I realised that the paginationQuery variable is null and I assume that the totalQuery has been picked up. Is there any way we can run multiple query build on same IDConnection instance?

thanks!

Drizin commented 7 months ago

Can you please provide more details? I didn't get what you're trying to do, or what's the problem you're having.

If you want to run two queries in a single roundtrip you can use Dapper QueryMultiple(). But running two commands over the same connection also should work.

AsuraKev commented 7 months ago

Hey @Drizin, thanks for the reply. Let me clarify my use case.

Based on the docs, to initialise a new query builder it goes like below where a SqlBuilder will be created on the current connection *cn.SqlBuilder($"SELECT FROM Product WHERE CategoryId={categoryId}")**

To extend on this pattern I was trying to do

var connection = new DBConnection();

var query1 = connection.SqlBuilder(""); query1.Where($"blah blah");

var query2 = connection.SqlBuilder(""); query2.Where($"blah blah");

query1.QueryAsync(); query2.QueryAsync();

This is where I noticed that query1 has became null upon inspecting... I was thinking that for a given existing DBConnection, we can create as many SqlBuilder on it. If this usage makes sense...

thanks :)

Drizin commented 7 months ago

That looks odd. How can a variable become null for no reason?

Can you create (and share here) a unit test where I can see the problem?

I tested this and haven't found any issues:

[Test]
public async Task TestNullVariable()
{
    var q1 = cn.QueryBuilder($"SELECT ProductID FROM [Production].[Product]");
    q1.Where($"ProductId=1");
    var q2 = cn.QueryBuilder($"SELECT ProductID FROM [Production].[Product]");
    q2.Where($"ProductId=2");

    var res1 = await q1.QueryAsync<int>();
    var res2 = await q2.QueryAsync<int>();
}
AsuraKev commented 7 months ago

hmm I gave it a shot again, it looks like its working fine now. I must have screwed up something before. thanks for the help and clarification! :)