C# CommandType.Text 与 CommandType.StoredProcedure

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/643880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:31:50  来源:igfitidea点击:

CommandType.Text vs CommandType.StoredProcedure

c#sqldatabaseado.net

提问by Lurker Indeed

Is there any benefit to explicitly using the StoredProcedure CommandType as opposed to just using a Text Command? In other words, is

与仅使用文本命令相比,显式使用 StoredProcedure CommandType 有什么好处吗?换句话说,是

cmd = new SqlCommand("EXEC StoredProc(@p1, @p2)");
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@p1", 1);
cmd.Parameters.Add("@p2", 2);

any worse than

cmd = new SqlCommand("StoredProc");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@p1", 1);
cmd.Parameters.Add("@p2", 2);

EDIT: Fixed bad copy paste job (again). Also, the whole point of the question is for a data access class. I'd much rather be able to pass the stored proc name and parameters in one line as opposed to extra lines for each parameter.

编辑:修复了错误的复制粘贴作业(再次)。此外,问题的重点在于数据访问类。我宁愿能够在一行中传递存储的 proc 名称和参数,而不是为每个参数添加额外的行。

采纳答案by Joel Coehoorn

One difference is how message pumping happens.

一个区别是消息泵是如何发生的。

Where I used to work we had a number of batch processes that ran over night. Many of them simply involved running a stored procedure. We used to schedule these using sql server jobs, but moved away from it to instead call the procedures from a .Net program. This allowed us to keep allour scheduled tasks in one place, even the ones that had nothing to do with Sql Server.

在我以前工作的地方,我们有许多批处理运行整夜。其中许多只涉及运行存储过程。我们过去常常使用 sql server 作业来安排这些,但不再使用它,而是从 .Net 程序调用过程。这允许我们将所有计划任务保存在一个地方,即使是那些与 Sql Server 无关的任务。

It also allowed us to build better logging functionality into the .Net program that calls the procedures, so that the logging from all of the overnight processes was consistent. The stored procedures would use the sql printand raiserrorfunctions, and the .Net program will receive and log those. What we learned was that CommandType.StoredProcedurewould alwaysbuffer these messages into batches of about 50. The .Net code wouldn't see any log events until the procedure finished or flushed the buffer, no matter what options you set on the connection or what you did in your sql. CommandType.Textfixed this for us.

它还允许我们在调用过程的 .Net 程序中构建更好的日志记录功能,以便所有夜间进程的日志记录保持一致。存储过程将使用 sqlprintraiserror函数,而 .Net 程序将接收并记录这些。我们了解到,它总是CommandType.StoredProcedure将这些消息缓冲为大约 50 个的批次。在过程完成或刷新缓冲区之前,.Net 代码不会看到任何日志事件,无论您在连接上设置了什么选项或您在其中做了什么你的sql 为我们解决了这个问题。CommandType.Text

As a side issue, I'd use explicit types with your query parameters. Letting .Net try to infer your parameter types can cause issues in some situations.

作为一个附带问题,我会在您的查询参数中使用显式类型。在某些情况下,让 .Net 尝试推断您的参数类型可能会导致问题。

回答by Jesse Weigert

It's cleaner.

它更干净。

You're calling a stored procedure, why not just use the CommandType.StoredProcedure?

您正在调用存储过程,为什么不直接使用CommandType.StoredProcedure