DataSet Query默认为字符串而不是DateTime

时间:2016-07-28 04:06:51

标签: c# sql-server datetime parameters

我遇到问题.Net C#使用string类型的参数构建我的查询而不是DateTime。

数据库列是类型(SQL)' date'。

我可以手动更改"字符串RptDate"到" DateTime RptDate"它工作得很好,但是一旦我对数据集进行了更改,它就会重新生成Designer代码,覆盖我的更改。

关于如何强制参数为特定类型的任何想法?

// this is system generated code from the "Dataset.Designer.cs" file
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
        [global::System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")]
        [global::System.ComponentModel.DataObjectMethodAttribute(global::System.ComponentModel.DataObjectMethodType.Select, false)]
        public virtual prDataSet.DetailedRow1DataTable GetDataByUserDate(string RptDate, string UserID) { // this first parameter should be DateTime
            this.Adapter.SelectCommand = this.CommandCollection[1];
            if ((RptDate == null)) {
                throw new global::System.ArgumentNullException("RptDate");
            }
            else {
                // This next line should cast the value as DateTime
                this.Adapter.SelectCommand.Parameters[0].Value = ((string)(RptDate));
            }
            if ((UserID == null)) {
                throw new global::System.ArgumentNullException("UserID");
            }
            else {
                this.Adapter.SelectCommand.Parameters[1].Value = ((string)(UserID));
            }
            prDataSet.DetailedRow1DataTable dataTable = new prDataSet.DetailedRow1DataTable();
            this.Adapter.Fill(dataTable);
            return dataTable;
        }

SQL查询代码:

SELECT 
    id, category, rpt_date, user_id, details, last_modified
FROM         
    DetailedRow
WHERE     
    (rpt_date = @RptDate) AND (user_id LIKE @UserID)

数据库创建代码:

CREATE TABLE [dbo].[DetailedRow]
(
    [id] [int] IDENTITY(100000,1) NOT NULL,
    [category] [int] NOT NULL,
    [rpt_date] [date] NOT NULL,
    [user_id] [nvarchar](6) NOT NULL,
    [details] [ntext] NOT NULL,
    [last_modified] [datetime] NOT NULL,

    CONSTRAINT [PK_DetailedRow] 
       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] TEXTIMAGE_ON [PRIMARY]
GO

ALTER TABLE [dbo].[DetailedRow] 
  ADD CONSTRAINT [DF_DetailedRow_last_modified]  
      DEFAULT (getdate()) FOR [last_modified]
GO

1 个答案:

答案 0 :(得分:1)

我要检查的第一件事是DataSet设计器的GUI版本。只是为了确保DataTable中的rpt_date字段是DateTime