How to pass a parameter from vb.net

后端 未结 3 1512
心在旅途
心在旅途 2020-12-21 23:30

I have a Program that will auto run each night, run a query, and email results. In my program I am calling a function as part of the query... What i\'d like to is pass the

3条回答
  •  佛祖请我去吃肉
    2020-12-21 23:38

    To call this function in VB.Net code, you should place the function call in a Stored Procedure and call this Procedure from VB.NET, passing the parameters as follows:

     Dim sqlcmd As New SqlClient.SqlCommand()
     sqlcmd.CommandType = CommandType.StoredProcedure
     sqlcmd.CommandText = "PROCEDURE_NAME"
     sqlcmd.Parameters.Add(New SqlClient.SqlParameter("@startdate", DateTime.Now.Date))
     sqlcmd.Parameters.Add(New SqlClient.SqlParameter("@enddate", DateTime.Now.Date.AddDays(1).AddSeconds(-1)));
     Dim obj As Object = sqlcmd.ExecuteScalar()
    

    If you can't create a Stored Procedure, you can do at the query level:

    declare @startdate datetime
    declare @enddate datetime
    
    set @startdate = cast(floor(cast(getdate() as float))as datetime) -- truncate the time part
    set @enddate = dateadd(S, -1, dateadd(d, 1, @startdate)) -- add 1 day, subtract 1 minute from today
    

提交回复
热议问题