直接从 C# 调用 SQL 函数 [英] Calling SQL Functions directly from C#

查看:40
本文介绍了直接从 C# 调用 SQL 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对将返回零或一的数据库运行查询(检查特定标准的存在).我接受审查的技术规范指出,我应该创建一个存储过程,该过程将返回一行,其中有一列名为结果"的列将包含 0 或 1 的位值.但是,我不确定存储过程是否是最好的方法,但我有点不确定,所以我想征求您的意见.我能想到的两个选项是:

I have a requirement to run a query against a database that will return either a zero or one (Checking for existance of specific criteria). The Tech specs I've been given for review state that I should be creating a stored procedure, that will return a single row, with a single column called "result" that will contain a bit value of 0 or 1. However, I'm not sure that a stored procedure would be the best approach, but am a little unsure so thought I'd ask for you opinions. The two options I can think of are:

1:创建一个 SQL 标量值函数,该函数执行查询并返回位.然后可以使用TEXT"SqlCommand 对象直接从 .Net 客户端应用程序中调用它,并且它会从ExecuteScalar()"方法返回一个布尔值.

1: Create a SQL scalar-valued function that performs the query and returns a bit. This could then be called directly from within the .Net client application using a "TEXT" SqlCommand object, and it would return a bool from the "ExecuteScalar()" method.

2:按照技术规范中的描述创建存储过程.然后会以正常方式从 .Net 客户端应用程序调用它,并返回一个包含位值的单行单列数据表.

2: Create a stored procedure as described in the tech specs. This would then be called from the .Net Client app in the normal manner, and would return a DataTable with a single row and single column, that contains the bit value.

对我来说,选项一似乎是最好的.然而,我脑子里的东西在说这不是一个好主意.

To me, option one seems the best. However, something in the back of my head is saying this isn't such a good idea.

请您发表您的意见并帮助减轻我的担忧吗?:)

Please could you give your opinions and help relieve my concerns? :)

干杯,伊恩

推荐答案

调用标量值函数是绝对正确的解决方案.

The calling scalar-valued function is absolutely correct solution.

这篇关于直接从 C# 调用 SQL 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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