How to retrieve the SQL query string of an F# query expression via System.Data.SQLite?

454 Views Asked by At

I'm using the System.Data.SQLite and I'm trying to retrieve the SQL string that is generated by the query expression below. The query executes correctly, but the SQL string is SELECT NULL AS [EMPTY].

It seems that GetCommand().CommandText is not supported, but if so, how else is it possible to access the generated SQL string?

[<Test>]
member this.showSQL() =
    let connectionString = sprintf @"Data Source=%s;UTF8Encoding=True;Version=3" dbFilename
    let connection = new SQLiteConnection(connectionString)
    use dc = new DataContext(connection)

    let channelMap = dc.GetTable<ChannelData>()

    let map = query {
        for row in channelMap do
        where (row.ChannelId = 1)
        select (row.ChannelId, row.Data0, row.State) }

    let cmd = dc.GetCommand(map).CommandText;
    printf "SQL: %s" cmd
1

There are 1 best solutions below

3
On

The SQLiteCommand object has the associated CommandText property working as intended.

    static void Main(string[] args)
    {
        string sql = "select * from foo";
        SQLiteCommand command = new SQLiteCommand(sql, null);

        Console.WriteLine(command.CommandText);
        Console.ReadLine();
    }

Perhaps you can rework your code to utilize it.