在Csharp应用程序中哪一个是好的 [英] In Csharp Application which one is good

查看:77
本文介绍了在Csharp应用程序中哪一个是好的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,



我对存储过程和Sql有疑问

我的问题哪个方法在csharp中写得好应用程序

1)在源代码中直接进行Sql查询或

2)调用存储过程。



可以任何人解释这个主题并给我一个关于Sp Vs Sql在C#中的差异#b / b
ex:假设同样的查询在csharp中运行sql所需的时间和csharp中的SP也需要多长时间..



谢谢

Bhavani

Hi Friends,

I have a doubt regarding in Stored procedure and Sql
My question which way is good to write in csharp application
1) Directly Sql queries in Source code or
2) Calling the Stored Procedure.

Can anyone explain on this topic and give me a diff on Sp Vs Sql in C#
ex:Suppose if same query how much time it will take to run sql in csharp and SP in csharp too..

Thanks
Bhavani

推荐答案

这个没有一个答案 :执行方式几乎相同,因为SP只是一系列指令,其执行方式与将其作为字符串输入并将其作为SqlCommand发送时相同。 SP将被预编译为all,这可能会在SQL服务器端节省一小部分(我的意思是很小)处理时间。但并不多!



这是清晰度和结构之间的平衡:有些人喜欢将简单的SELECT或UPDATE语句放入SP中,但我觉得这会让代码变得更难阅读,因为你不知道SP在没有咨询SSMS的情况下做了什么,并且看起来 - 简单的东西的内联代码更容易维护。



一般来说,我使用的是复杂的SP:错误太容易,或者你不需要知道它是如何工作的,只需得到你想要的结果。例如:

There is no "one answer" to this: either way the execution is pretty much the same, because an SP is just a sequence of instructions that is executed in the same way that it would be if you entered them as a string and sent them over as an SqlCommand. The SP will be precompiled is all, which might save a small (and I do mean small) amount of processing time at the SQL server end. but not much!

It's a balance between clarity and structure: some people like to put even trivial SELECT or UPDATE statements into an SP, but I feel that makes code harder to read as you can't tell what an SP is doing without consulting SSMS and looking - "inline" code for simple stuff is a lot easier to maintain.

Generally, I use an SP where it's complex: where it's too easy to make a mistake, or you don't need to know exactly how it works, just get the results you wanted. For example:
SELECT a.Name, b.EpCount
FROM Serials a
JOIN
  (SELECT SerialId, Count(SerialNo) AS EpCount
   FROM Episode GROUP BY SerialId) AS b
ON a.Id = b.SerialId

是一个名为CountEpisodes的SP,因为它返回电视节目的每个系列以及剧集的数量 - 这是我用的东西一些地方,锻炼有点复杂。所以一次正确,创建一个SP,下次它变得微不足道。

is an SP called CountEpisodes because it returns each series of a TV show together with the number of episodes - it's something I use in a few places, and it's a little complex to work out. So get it right once, create an SP and it becomes trivial next time.


使用SP的一个很大的优点是客户端代码(在这种情况下是c#)可以从数据库结构。



这意味着同一个客户端可以用于不同的数据库实现和不同的数据库,例如Oracle,MS SQL和MySQL。



数据库可以在必要时在内部进行更改,只要存储过程的签名保持不变,客户端就会知道没有区别。



最终的选择是由于需要和未来可能的变化。
One great advantage with using SP is that the client code (in this case in c#) can be isolated from the database structure.

This means that the same client can be used for different database implementations and different databases, such as Oracle, MS SQL and MySQL.

The database can change internally as much as is necessary and the client will know no difference as long as the signature of the stored procedure is kept intact.

The final choice is due to need and possible future changes.


这篇关于在Csharp应用程序中哪一个是好的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆