中調用SQL定義的SQL定義已定義的函數在使用C#代碼的SQL定義的函數中查詢數據庫。這是您可以從C#代碼中調用名為“ TCUPOM”的T-SQL標量函數的方法: c#代碼:
原始代碼中的錯誤:Setting the CommandType to StoredProcedure when it should be Text for UDFs.Corrected Code:
create function TCupom (@cupom int) returns float as begin declare @Tcu float; select @Tcu = sum (total) from alteraca2 where pedido = @cupom if (@tcu is null) set @tcu = 0; return @tcu; endThe corrected code below demonstrates how to correctly call the "Tcupom" function from C#:
public void TotalCupom(int杯子) { 弗拉特·塞達(Float Saida); sqldataAdapter da2 = new SQLDATAADAPTER(); if(conex1.state == ConnectionState.clate) { conex1.open(); } sqlcommand totalf = new sqlcommand(“ select dbo.tcupom(@code)”,conex1); SQLParameter Code1 = new SqlParameter(“@code”,sqldbtype.int); code1.value = cupom; Saida = totalf.executescalar(); 返回Saeda; } 說明:
SQLCommand對像是用SQL Query語句構造的,該對象包含UDF完全合格的名稱DBO.TBO.TCUPOM。它的值設置為傳遞給C#方法的CUPOM參數。 executesCalar()方法用於執行UDF並檢索其標量結果。
免責聲明: 提供的所有資源部分來自互聯網,如果有侵犯您的版權或其他權益,請說明詳細緣由並提供版權或權益證明然後發到郵箱:[email protected] 我們會在第一時間內為您處理。
Copyright© 2022 湘ICP备2022001581号-3