【问题标题】:DataSet Query defaulting to string instead of DateTimeDataSet 查询默认为字符串而不是 DateTime
【发布时间】:2016-07-28 04:06:50
【问题描述】:

我在使用 .Net C# 使用字符串类型参数而不是 DateTime 构建查询时遇到问题。

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

我可以手动将“字符串 RptDate”更改为“DateTime RptDate”,它工作得很好,但是一旦我对数据集进行更改,它就会重新生成设计器代码,覆盖我的更改。

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

// 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

【问题讨论】:

    标签: c# sql-server datetime parameters


    【解决方案1】:

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

    【讨论】:

    • 设计器中DataTable中rpt_date列的数据类型为System.DateTime。
    • 我能够在查询的参数集合中更改参数的数据类型。我将不得不等到我添加另一个查询以查看它是否恢复,但现在它似乎已经修复它。
    猜你喜欢
    • 1970-01-01
    • 2015-01-31
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-18
    • 2016-07-15
    • 2023-03-30
    相关资源
    最近更新 更多