sqlkata / querybuilder

SQL query builder, written in c#, helps you build complex queries easily, supports SqlServer, MySql, PostgreSql, Oracle, Sqlite and Firebird
https://sqlkata.com
MIT License
3.09k stars 499 forks source link

Compile query with parameters for ODBC Connection #691

Open mconca-kube opened 1 year ago

mconca-kube commented 1 year ago

I noticed that Dapper requires a different syntax for parameters in query depending on which type of connection you use. I'm using a SQL Server database.

This is the code that works for a SqlConnection (using parameters with a @ sign):

var connectionString = @"Data Source=localhost;Initial Catalog=ComunicazioniGulliver;User ID=sa;Password=kubeswap.01";
var connection = new SqlConnection(connectionString);
connection.Open();

         var parms = new DynamicParameters();
         parms.Add("p0", 0);
         parms.Add("p1", 20);

         var res2 = connection.Query(
    "SELECT [B04TDO0E].* FROM [dbo].[B04TDO0E] ORDER BY (SELECT 0) OFFSET @p0 ROWS FETCH NEXT @p1 ROWS ONLY",
             parms);

This is the query that works with an ODBC connection (using parameters surrounded by question marks).

var connectionString = "DSN=ComunicazioniSqlServer;UID=sa;PWD=kubeswap.01";
var connection = new System.Data.Odbc.OdbcConnection(connectionString);
connection.Open();

var parms = new DynamicParameters();
parms.Add("p0", 0);
parms.Add("p1", 20);

var res2 = connection.Query(
[B04TDO0E].* FROM [dbo].[B04TDO0E] ORDER BY (SELECT 0) OFFSET ?p0? ROWS FETCH NEXT ?p1? ROWS ONLY",
    parms);

SqlKata always compiles queries with the first syntax (@), but how should I manage odbc connections? I would need a way to tell SqlKata to compile parameters using quotation marks. I need to connect to this database using ODBC but my queries do not work this way.