Page 1 of 1

Obtaining a SQL/parameter values without executing it

Posted: Tue 21 Nov 2017 19:39
by motuzko
In LinqConnect, is there a way to obtain a SQL with all the parameter values without executing it?

Re: Obtaining a SQL/parameter values without executing it

Posted: Tue 21 Nov 2017 19:41
by motuzko
2 Scenarios:

1) IQueryable<T> to SQL + parameter values
2) a way to get a SQL for Min/Max(IQueryable<T> => int)

Re: Obtaining a SQL/parameter values without executing it

Posted: Mon 27 Nov 2017 15:30
by Shalex
Unfortunately, there is no way to do that with a current implementation.

Re: Obtaining a SQL/parameter values without executing it

Posted: Tue 28 Nov 2017 17:51
by motuzko
would it be possible to implement this functionality in the future?

Re: Obtaining a SQL/parameter values without executing it

Posted: Thu 07 Dec 2017 19:40
by Shalex
We are processing your request.

Re: Obtaining a SQL/parameter values without executing it

Posted: Fri 20 Apr 2018 10:04
by Shalex
Please try this workaround:

Code: Select all

using System;
using System.IO;
using System.Text;
using MyContext;

namespace MyConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using (var context = new MyDataContext())
            {
                var log = new MyTextWriter();
                context.Log = log;

                string name = "abcd";
                var dept = new DEPT() { DEPTNO = 6, DNAME = name };
                context.DEPTs.InsertOnSubmit(dept);

                try
                {
                    context.SubmitChanges();
                }
                catch (Exception e)
                {
                    if (e.Message != "Getting SQL without execution")
                        throw e;
                }

                Console.WriteLine(log.GetSql());
                Console.ReadKey();
            }
        }
    }

    public class MyTextWriter : TextWriter
    {
        StringBuilder SQL;
        public MyTextWriter()
        {
            SQL = new StringBuilder();
        }
        public override void Write(string value)
        {
            SQL.Append(value);
        }
        public override void WriteLine(string value)
        {
            SQL.AppendLine(value);
        }

        public override void WriteLine()
        {
            throw new Exception("Getting SQL without execution");
        }

        public override Encoding Encoding {
            get { return Encoding.Default; }
        }

        public string GetSql()
        {
            string query = SQL.ToString();
            SQL.Clear();
            return query;
        }
    }
}

Re: Obtaining a SQL/parameter values without executing it

Posted: Mon 23 Apr 2018 13:58
by motuzko
*without executing it*

Re: Obtaining a SQL/parameter values without executing it

Posted: Mon 23 Apr 2018 17:24
by Shalex
The code doesn't send any CUD statements to the server.