今天要向数据库传递一个日期格式的值,但却发现不知道如何传递日期参数进去
数据库里该字段是以日期格式存储的,而平时写selcet一般都是处理字符串型和数值型的数据,很少涉及日期型(很久没写代码了,都忘记差不多了~)
想了好多办法啊,不过还好没去尝试,因为办法都给人以好长且效率好低的感觉。日期型用字符串是无法传递的了,+号无法重载该类型的值。还想了把值提取出来做判断,这样一算,这个读取次数从n变成了2n了~~~~效率又低了,汗掉~~~~~~~~
还好问了个同学,“#”,多神奇的字符,直接把字符串型当作日期型就摆平了。
strSQL="delete from Attendance where PERSON_ID='"+m_cList.GetItemText(nItem,1)+"' and IO_TIME=#"+strTime+"#";
让我记下这个开心的想法,虽然感觉好像以前数据库书上有念到过~~~~~嗨,知识遗忘厉害。惭愧 2007-4-11:
今天发现在SQLServer2005里面应该不是用'#'还是使用 ' 符号
下面是对Northwind的查询语句
查询1996-07-04到1996-07-09之间的数据
SELECT [OrderID]
,[CustomerID]
,[EmployeeID]
,[OrderDate]
,[RequiredDate]
,[ShippedDate]
,[ShipVia]
,[Freight]
,[ShipName]
,[ShipAddress]
,[ShipCity]
,[ShipRegion]
,[ShipPostalCode]
,[ShipCountry]
FROM [Northwind].[dbo].[Orders]
where OrderDate > '1996-07-04'
and OrderDate < '1996-07-09'
,[CustomerID]
,[EmployeeID]
,[OrderDate]
,[RequiredDate]
,[ShippedDate]
,[ShipVia]
,[Freight]
,[ShipName]
,[ShipAddress]
,[ShipCity]
,[ShipRegion]
,[ShipPostalCode]
,[ShipCountry]
FROM [Northwind].[dbo].[Orders]
where OrderDate > '1996-07-04'
and OrderDate < '1996-07-09'
如果使用#会怎么样呢?
--错误代码 Error Code
1SELECT [OrderID]
2 ,[CustomerID]
3 ,[EmployeeID]
4 ,[OrderDate]
5 ,[RequiredDate]
6 ,[ShippedDate]
7 ,[ShipVia]
8 ,[Freight]
9 ,[ShipName]
10 ,[ShipAddress]
11 ,[ShipCity]
12 ,[ShipRegion]
13 ,[ShipPostalCode]
14 ,[ShipCountry]
15 FROM [Northwind].[dbo].[Orders]
16where OrderDate > #1996-07-04#
17and OrderDate < #1996-07-09#
18
1SELECT [OrderID]
2 ,[CustomerID]
3 ,[EmployeeID]
4 ,[OrderDate]
5 ,[RequiredDate]
6 ,[ShippedDate]
7 ,[ShipVia]
8 ,[Freight]
9 ,[ShipName]
10 ,[ShipAddress]
11 ,[ShipCity]
12 ,[ShipRegion]
13 ,[ShipPostalCode]
14 ,[ShipCountry]
15 FROM [Northwind].[dbo].[Orders]
16where OrderDate > #1996-07-04#
17and OrderDate < #1996-07-09#
18
消息 102,级别 15,状态 1,第 16 行
'#' 附近有语法错误。