How to get the executing SQL query from a table adapter? [duplicate]

ε祈祈猫儿з 提交于 2019-12-06 02:32:01

You can use this workaround with replace ParameterName with its value but it not good because you need to manage value formatting by your own, and it can be slow
and you will need to get parameters after Insert is executed

code:

var usersTableAdapter = new testDataSetTableAdapters.usersTableAdapter();


            usersTableAdapter.Insert(4, "home", "origin", "email@host.com", "realname", "spec", 1, "username", "usernick", "whereform");
            var cmd = usersTableAdapter.Adapter.InsertCommand;
            var text = cmd.CommandText;
            var sb = new StringBuilder(text);
            foreach (SqlParameter cmdParam in cmd.Parameters)
            {
                if (cmdParam.Value is string)
                    sb.Replace(cmdParam.ParameterName, string.Format("'{0}'", cmdParam.Value));
                else
                    sb.Replace(cmdParam.ParameterName, cmdParam.Value.ToString());
            }
            Console.WriteLine(sb.ToString());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!