create table tmp
(
fCode Varchar(10),
fDate smalldatetime
)
insert into tmp
select 'A1','2011-08-10' union all
select 'A2','2011-08-11' union all
select 'A3','2011-08-12' union all
select 'A4','2011-11-07' union all
select 'A5','2011-11-08' union all
select 'A6','2011-11-09'
go
--drop proc SP_TestDatePara
create proc SP_TestDatePara(@Date smalldatetime)
as
select * from tmp where fDate=@Date
/*
EXEC SP_TestDatePara '2011-08-11'
*/
以下VB代码
Option Explicit
Dim Cn As New ADODB.Connection
'此方法不受当前日期格式
Private Sub Command1_Click()
Dim strSQL As String
Dim Rst As New ADODB.Recordset
Dim tmpCm As New ADODB.Command
Dim tmpPara As ADODB.Parameter
tmpCm.ActiveConnection = Cn
tmpCm.CommandType = adCmdStoredProc
tmpCm.CommandText = "SP_TestDatePara"
Set tmpPara = New ADODB.Parameter
tmpPara.Name = "Date"
tmpPara.Type = adDBTimeStamp
tmpPara.Value = Date
tmpPara.Direction = adParamInput
tmpCm.Parameters.Append tmpPara
Set tmpPara = Nothing
Set Rst = tmpCm.Execute()
Set DataGrid1.DataSource = Rst
End Sub
'此方法会受当前日期格式
Private Sub Command2_Click()
Dim strSQL As String
Dim Rst As New ADODB.Recordset
strSQL = "EXEC SP_TestDatePara '" & Date & "'"
Rst.Open strSQL, Cn, adOpenStatic, adLockPessimistic
Set DataGrid1.DataSource = Rst
End Sub
Private Sub Form_Load()
Cn.CursorLocation = adUseClient
Cn.ConnectionString = "Driver=SQL Server;Server=.;UID=sa;PWD=123;Database=pubs;"
Cn.Open
End Sub