将SQL保存在c#源代码或Stored Procs中有哪些优点/缺点?我一直在和一个朋友讨论这个问题,我们正在做一个开源项目(c# ASP。网论坛)。目前,大多数数据库访问都是通过在c#中构建内联SQL并调用SQL Server DB来完成的。所以我在试着确定,对于这个特定的项目,哪个是最好的。

到目前为止,我有:

in Code的优点:

更容易维护-不需要运行SQL脚本来更新查询 更容易移植到另一个DB -没有pros到移植

存储Procs的优点:

性能 安全


当前回答

I'm firmly on the side of stored procs assuming you don't cheat and use dynamic SQL in the stored proc. First, using stored procs allows the dba to set permissions at the stored proc level and not the table level. This is critical not only to combating SQL injection attacts but towards preventing insiders from directly accessing the database and changing things. This is a way to help prevent fraud. No database that contains personal information (SSNs, Credit card numbers, etc) or that in anyway creates financial transactions should ever be accessed except through strored procedures. If you use any other method you are leaving your database wide open for individuals in the company to create fake financial transactions or steal data that can be used for identity theft.

存储的proc也比从应用程序发送的SQL更容易维护和性能调优。它们还允许dba查看数据库结构更改对数据访问方式的影响。我从未遇到过允许动态访问数据库的优秀dba。

其他回答

SQL注入攻击呈上升趋势。有人很容易找到这些代码并在你的网站上运行注入攻击。您必须始终始终将查询参数化。最好不要在动态SQL查询上运行exec(@x)。在我看来,使用内联SQL并不是一个好主意。

有些人认为,存储过程很麻烦,因为它们是另一组需要与代码分开维护的项。但是它们是可重用的,如果你在查询中发现了一个错误,你可以在不重新编译的情况下修复它们。

我不喜欢存储过程

存储过程更易于维护,因为: 你不需要重新编译你的c#应用每当你想改变一些SQL

无论如何,当数据类型发生变化时,或者您想返回一个额外的列时,您都将重新编译它。总的来说,你可以“透明”地从应用程序下面更改SQL的次数是非常少的

您最终会重用SQL代码。

编程语言,包括c#,都有这个神奇的东西,叫做函数。这意味着您可以从多个地方调用相同的代码块!神奇的!然后你可以把可重用的SQL代码放在其中一个,或者如果你想获得真正的高科技,你可以使用一个库来为你做这件事。我相信它们被称为对象关系映射器,现在很常见。

当您试图构建可维护的应用程序时,代码重复是最糟糕的事情!

同意,这就是为什么storedprocs是个坏东西。将代码重构和分解(分解成更小的部分)为函数要比将SQL分解成函数容易得多。SQL块?

你有4个web服务器和一堆使用相同SQL代码的windows应用程序,现在你意识到SQL代码有一个小问题,所以你宁愿......在一个地方改变程序或将代码推送到所有的web服务器,重新安装所有Windows盒子上的所有桌面应用程序(单击一次可能会有帮助)

为什么你的windows应用程序直接连接到中央数据库?这似乎是一个巨大的安全漏洞,因为它排除了服务器端缓存。他们不应该通过网络服务或者类似于你的网络服务器来连接吗?

所以,推出1个新的sproc,或4个新的web服务器?

在这种情况下,推送一个新的sproc更容易,但根据我的经验,95%的“推送更改”影响的是代码而不是数据库。如果那个月你向服务器推送了20个东西,向数据库推送了1个,那么如果你向服务器推送了21个东西,而向数据库推送了0个东西,你几乎不会损失太多。

更容易检查代码。

你能解释一下吗?我不明白。特别是考虑到scproc可能不在源代码控制中,因此不能通过基于web的SCM浏览器访问等等。

更多的缺点:

Storedprocs存在于数据库中,在外界看来,数据库是一个黑盒。简单的事情,比如想把它们放在源代码控制中,就变成了一场噩梦。

还有一个纯粹努力的问题。如果你想向你的CEO证明为什么只花了他们700万美元来建立一些论坛,那么把所有东西都分解成100万个层可能是有意义的,但为每一件小事创建一个存储过程只是额外的无聊工作,没有任何好处。

Definitely easier to maintain if you put it in a stored procedure. If there's difficult logic involved that will potentially change in the future it is definitely a good idea to put it in the database when you have multiple clients connecting. For example I'm working on an application right now that has an end user web interface and an administrative desktop application, both of which share a database (obviously) and I'm trying to keep as much logic on the database as possible. This is a perfect example of the DRY principle.

较小的日志

存储过程的另一个小优点是没有提到的:当涉及到SQL流量时,基于sp的数据访问产生的流量要少得多。当您监视流量进行分析和分析时,这一点变得非常重要——日志将会更小且可读。

Something that I haven't seen mentioned thus far: the people who know the database best aren't always the people that write the application code. Stored procedures give the database folks a way to interface with programmers that don't really want to learn that much about SQL. Large--and especially legacy--databases aren't the easiest things to completely understand, so programmers might just prefer a simple interface that gives them what they need: let the DBAs figure out how to join the 17 tables to make that happen.

话虽如此,用于编写存储过程的语言(PL/SQL就是一个臭名昭著的例子)是相当残酷的。它们通常不提供您在当今流行的命令式语言、OOP或函数式语言中看到的任何细节。认为COBOL。

因此,请坚持使用仅抽象了关系细节的存储过程,而不是那些包含业务逻辑的存储过程。