c# 从数据库中取数据 时间段内 数据库中日期是文本类型

2024-12-03 18:12:08
推荐回答(1个)
回答1:

朋友,因为对ACCESS不是非常清楚其数据类型,我暂时用SQL中的方式帮你解决此问题:
先创建表:
/****** Object: Table [dbo].[Chart] Script Date: 09/30/2010 12:39:49 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[Chart](
[id] [int] IDENTITY(1,1) NOT NULL,
[employee] [varchar](50) NULL,
[Date] [varchar](50) NULL,
CONSTRAINT [PK_Chart_1] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
/****** Object: StoredProcedure [dbo].[procGetInfoByDate] Script Date: 09/30/2010 12:39:47 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
create proc [dbo].[procGetInfoByDate]
@beginDate varchar(5),
@endDate varchar(5)
as
begin
select * from Chart where Chart.[Date] between convert(varchar(50),'201002030'+@beginDate) and
convert(varchar(50),'201002030'+@endDate)
end
GO

方法一:直接写SQL语句:
select * from Chart where Chart.[Date] between convert(varchar(50),'201002030'+'1') and
convert(varchar(50),'201002030'+'2')

方法二:用存储过程,这种方法更适合你的需求
create proc procGetInfoByDate
@beginDate varchar(5),
@endDate varchar(5)
as
begin
select * from Chart where Chart.[Date] between convert(varchar(50),'201002030'+@beginDate) and
convert(varchar(50),'201002030'+@endDate)
end

调用存储过程:
procGetInfoByDate 1,2

希望能为你提供借鉴!