How can I call a sqlserver function from VB.net(or C#) ? Is there some syntax like stored procedure?

前端 未结 5 1092
囚心锁ツ
囚心锁ツ 2020-12-16 05:48
Public Sub cleanTables(ByVal prOKDel As Short)
     Dim sqlParams(1) As SqlParameter
     Dim sqlProcName As String
     sqlProcName = \"db.dbo.sp_mySP\"
     sqlPar         


        
5条回答
  •  抹茶落季
    2020-12-16 06:37

    Sorry, there is no way to run a function directly. Either call it using a sql Text command

    Public Sub RunFunction(ByVal input As Short)
                Using myConnection As New Data.SqlClient.SqlConnection
                    Using myCommand As New Data.SqlClient.SqlCommand("Select dbo.MyFunction(@MyParam)", myConnection)
                        myCommand.CommandType = CommandType.Text
                        myCommand.Parameters.Add(New Data.SqlClient.SqlParameter("@MyParam", input))
                        myCommand.CommandTimeout = 0
                        Try
                            myCommand.ExecuteNonQuery()
                        Catch ex As Exception
    
                        End Try
                    End Using
    
                End Using
            End Sub
    

    Or Wrap a procedure round it...

    Create Procedure RunMyFunction(@MyParam as int)
    Select * FROM dbo.MyFunction(@MyParam)
    Go
    

提交回复
热议问题