直接从C#调用SQL函数

时间:2010-06-10 09:32:34

标签: sql sql-server stored-procedures user-defined-functions

我需要针对将返回0或1的数据库运行查询 (检查是否存在特定标准)。 我已经获得审查的技术规范声明我应该创建一个存储过程,它将返回一行,其中一个名为“result”的列将包含一个0或1的位值。 但是,我不确定存储过程是否是最好的方法,但我有点不确定,所以以为我会问你的意见。 我能想到的两个选择是:

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

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

对我来说,选项一似乎是最好的。然而,我脑子里的一些话说这不是一个好主意。

请您提出您的意见并帮助减轻我的疑虑? :)

干杯, 伊恩

6 个答案:

答案 0 :(得分:14)

使用 ExecuteScalar()方法执行存储过程。然后,您可以将此结果转换为布尔值。

e.g

   SqlConnection con = new SqlConnection(connectionString);
    SqlCommand com = new SqlCommand("Execute dbo.usp_MyStoredProc", con);
    return (Boolean)com.ExecuteScalar();

答案 1 :(得分:9)

这对我有用,并且基于这个答案https://stackoverflow.com/a/3232556/1591831使用SqlDataAdapter(请注意,您不需要使用)和ExecuteScalar(可以使用如此处所示的ExecuteNonQuery):

bool res = false;
using (SqlConnection conn = new SqlConnection(GetConnectionString()))
{
    using (SqlCommand comm = new SqlCommand("dbo.MyFunction", conn))
    {
        comm.CommandType = CommandType.StoredProcedure;

        SqlParameter p1 = new SqlParameter("@MyParam", SqlDbType.Int);
        // You can call the return value parameter anything, .e.g. "@Result".
        SqlParameter p2 = new SqlParameter("@Result", SqlDbType.Bit);

        p1.Direction = ParameterDirection.Input;
        p2.Direction = ParameterDirection.ReturnValue;

        p1.Value = myParamVal;

        comm.Parameters.Add(p1);
        comm.Parameters.Add(p2);

        conn.Open();
        comm.ExecuteNonQuery();

        if (p2.Value != DBNull.Value)
            res = (bool)p2.Value;
    }
}
return res;

答案 2 :(得分:7)

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

答案 3 :(得分:1)

我认为这取决于相应的db函数(sp / udf)必须执行的逻辑。

如果是例如我们对特定db函数执行的次数感兴趣,我们肯定需要对各种表进行一些数据操作和更新。因此,我们必须在这里进行存储过程。如果它是一个简单的检索,udf会做。

答案 4 :(得分:0)

从长远来看,使用存储过程解决它会更好,因为它更灵活,更具适应性

答案 5 :(得分:0)

我使用这个sql标量函数

CREATE FUNCTION DAYSADDNOWK(@addDate AS DATE, @numDays AS INT)
RETURNS DATETIME
AS
BEGIN
    SET @addDate = DATEADD(d, @numDays, @addDate)
    IF DATENAME(DW, @addDate) = 'sunday'   SET @addDate = DATEADD(d, 1, @addDate)
    IF DATENAME(DW, @addDate) = 'saturday' SET @addDate = DATEADD(d, 2, @addDate)

    RETURN CAST(@addDate AS DATETIME)
END
GO

然后这是我的c#代码

using (SqlCommand cmd3 = new SqlCommand("SELECT dbo.DAYSADDNOWK", ClassV.con))
                    ClassV.con.Open();
                    SqlCommand brecord = ClassV.con.CreateCommand();
                    brecord.CommandText = "INSERT INTO TblBorrowRecords VALUES ('" + DGStudents.CurrentRow.Cells[1].Value.ToString() + "','" + DGStudents.CurrentRow.Cells[2].Value.ToString() + "','" + DGStudents.CurrentRow.Cells[4].Value.ToString() + "','" + DGStudents.CurrentRow.Cells[3].Value.ToString() + "','" + DG.CurrentRow.Cells[4].Value.ToString() + "','" + DG.CurrentRow.Cells[5].Value.ToString() + "','" + DG.CurrentRow.Cells[6].Value.ToString() + "','" +System.DateTime.Now.Date.ToShortDateString() + "' , dbo.DAYSADDNOWK(GETDATE(),5) ,null , '" + ClassV.lname.ToString() + ", " + ClassV.fname.ToString() + " " + ClassV.mname.ToString() + "', null, 'Good',null)";
                    var DAYSADDNOWK = brecord.ExecuteScalar();

我的C#代码跳过了函数