Calling SQL Functions directly from C#

后端 未结 6 760
遇见更好的自我
遇见更好的自我 2021-02-08 12:49

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 r

相关标签:
6条回答
  • 2021-02-08 13:01

    Execute the Stored Procedure using the ExecuteScalar() method. You can then cast the result of this to a boolean.

    e.g

       SqlConnection con = new SqlConnection(connectionString);
        SqlCommand com = new SqlCommand("Execute dbo.usp_MyStoredProc", con);
        return (Boolean)com.ExecuteScalar();
    
    0 讨论(0)
  • 2021-02-08 13:03

    This works for me and is based on this answer https://stackoverflow.com/a/3232556/1591831 using a SqlDataAdapter (note that you do not need to use one) and ExecuteScalar (can use ExecuteNonQuery as shown here):

    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;
    
    0 讨论(0)
  • 2021-02-08 13:10

    solving it with a stored procedure is better in the long run because it´s more flexible and adaptable

    0 讨论(0)
  • 2021-02-08 13:12

    I suppose it depends on the logic the corresponding db function (sp/udf) has to execute.

    If for e.g. we are interested in the number of times the particular db function has executed we'd definitely need to do some data manipulation and updates on various tables. Hence we'd have to go for stored procs here. If its a simple retrieval a udf will do.

    0 讨论(0)
  • 2021-02-08 13:16

    The calling scalar-valued function is absolutely correct solution.

    0 讨论(0)
  • 2021-02-08 13:16

    i use this sql scalar function

    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
    

    then this is my c# code

    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();
    

    my C# code skips the function

    0 讨论(0)
提交回复
热议问题