0
我遇到了.Net C#的問題,它使用類型爲string而不是DateTime的參數構建我的查詢。數據集查詢默認爲字符串,而不是DateTime
數據庫列是類型(SQL)「日期」。
我可以手動將「字符串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
設計器中DataTable中rpt_date列的數據類型是System.DateTime。 –
我能夠在查詢的參數集合中更改參數的數據類型。我將不得不等待,直到我添加另一個查詢來查看它是否恢復,但現在它似乎修復了它。 –