Obtaining a SQL/parameter values without executing it
Posted: Tue 21 Nov 2017 19:39
In LinqConnect, is there a way to obtain a SQL with all the parameter values without executing it?
Discussion forums for open issues and questions concerning database tools, data access components and developer tools from Devart
https://forums.devart.com/
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;
}
}
}