Sql 如何从vb.net传递参数
我有一个程序,每天晚上自动运行,运行查询,并通过电子邮件发送结果。在我的程序中,我调用一个函数作为查询的一部分。。。我想传递的是程序运行的日期作为参数。(@startdate和@enddate)@startdate将始终是00:00:00的“今天”日期,enddate将始终是23:59:59的“今天”日期。比如说。如果该程序今晚运行,它将通过13年1月31日作为日期。明天,它将通过2/1/13作为日期,下一个日期2/2/13,等等。如果我可以在查询级别这样做,也可以。。。下面是我的代码:Sql 如何从vb.net传递参数,sql,vb.net,parameters,stored-functions,Sql,Vb.net,Parameters,Stored Functions,我有一个程序,每天晚上自动运行,运行查询,并通过电子邮件发送结果。在我的程序中,我调用一个函数作为查询的一部分。。。我想传递的是程序运行的日期作为参数。(@startdate和@enddate)@startdate将始终是00:00:00的“今天”日期,enddate将始终是23:59:59的“今天”日期。比如说。如果该程序今晚运行,它将通过13年1月31日作为日期。明天,它将通过2/1/13作为日期,下一个日期2/2/13,等等。如果我可以在查询级别这样做,也可以。。。下面是我的代码: SEL
SELECT
SUM(QTY) AS Discounts
FROM
dbo.fFinancialDataFull('Date Range Report', @startdate , @enddate, '1', '1', 'ALL', 'ALL', 'ALL', 'ALL', '1', '1', '1', '1', '1') AS fFinancialDataFull_1
WHERE
(ReportCategoryID = 62)) AS unlimitedtbl
要在VB.Net代码中调用此函数,应将函数调用放在存储过程中,然后从VB.Net调用此过程,并按如下方式传递参数:
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()
如果无法创建存储过程,可以在查询级别执行以下操作:
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
试试这个:
Dim StartDate as string = DateTime.Today.ToString("yyyy/MM/dd") & " 00:00:00"
Dim EndDate as string = DateTime.Today.ToString("yyyy/MM/dd") & " 23:59:59"
我不知道你在用什么数据库服务器。如果不起作用,请尝试不同的日期格式,例如
DateTime.Today.ToString(“yyyy-MM-dd”)
//这些是日期变量。。如果你需要他们分开
Dim TodayDt As DateTime = DateTime.Today
Dim Tomorrow As DateTime = DateTime.Today.AddDays(1)
Dim TodayEnd as DateTime
TodayEnd = Tomorrow.AddSeconds(-1)
//这是在SQL Server中执行的SQL命令
SELECT
SUM(QTY) AS Discounts
FROM
dbo.fFinancialDataFull('Date Range Report', startdate , enddate, '1', '1', 'ALL', 'ALL', 'ALL', 'ALL', '1', '1', '1', '1', '1') AS fFinancialDataFull_1
WHERE ReportCategoryID = 62
AND startdate = TodayDt
AND enddate = TodayEnd AS unlimitedtbl
//这是使同一SQL在VB上运行所需编写的函数
Public Function GetValueByDates() As String
Dim TodayDt As DateTime = DateTime.Today
Dim Tomorrow As DateTime = DateTime.Today.AddDays(1)
Dim TodayEnd as DateTime
TodayEnd = Tomorrow.AddSeconds(-1)
Dim ReportCategoryID = 62
Dim sql As String = " SELECT
SUM(QTY) AS Discounts
FROM
dbo.fFinancialDataFull('Date Range Report', startdate , enddate, '1', '1', 'ALL', 'ALL', 'ALL', 'ALL', '1', '1', '1', '1', '1') AS fFinancialDataFull_1
WHERE ReportCategoryID = @ReportCategoryID
AND startdate = @TodayDt
AND enddate = @TodayEnd AS unlimitedtbl"
Using cn As New SqlConnection("Your connection string here"), _
cmd As New SqlCommand(sql, cn)
cmd.Parameters.Add("@TodayDt", SqlDbTypes.DateTime).Value = TodayDt
cmd.Parameters.Add("@TodayEnd", SqlDbTypes.DateTime).Value = TodayEnd
cmd.Parameters.Add("@ReportCategoryID", SqlDbTypes.int).Value = ReportCategoryID
Return cmd.ExecuteScalar().ToString()
End Using
End Function
我没有能力创建存储过程。这是一个一致同步的数据库。我无法控制同步信息。现在是2005年。我现在就试试这个,让你知道。这太完美了。非常感谢。