1

我在一个旧的 .NET 2.0 网站上工作,该网站在日期搜索方面存在问题。

经过几个小时的故障排除后,我得出的结论是,正在发送的搜索参数是一种MM/DD/YYYY格式,并且 SQL Server 正在等待DD/MM/YYYY

我不知道如何解决这个问题。

我尝试使用 ( .ToString("dd/MM/yyyy")) 将其转换为字符串,然后再转换为DateTime. 这不起作用,因为它必须是可空的DateTime

CS1502:最佳重载方法匹配 'TableAdapters.Artist_ListTableAdapter.GetData(int, System.DateTime?, System.DateTime?, int?, int?, bool?, string, string, string, int?, int?, int? , int?, int?, out int?)' 有一些无效参数

现在我对 SQL Server 存储过程不太了解,所以我不知道如何查看该GetData函数......当我右键单击并转到定义时,我得到一个生成的 c# 页面。

有什么帮助吗?

编辑:@BigM 不确定这是否会有所帮助,但是:

变量声明:

public DataTable dtArtist = new DataTable();
public string cp_basePath;
public int pagecount;
int CultureId ;
DateTime? startDate;
DateTime? endDate;
int? disciplineId = null;
int? minimumGradelevel = null;
int? maximumGradelevel = null;
int? languageId = null;
bool? isFeatured = null;
int? educationalInitiativeId = null;
string EducationalInitiativeIds = string.Empty;
string sortColumnName = string.Empty;
string searchCriteria = string.Empty;

从查询字符串中获取:

private void GetQueryStrings()
{
    CultureId = objGeneralLayer.plCultureId;
    if (Request.QueryString["StartDate"] != null)
        startDate = Convert.ToDateTime(Request.QueryString["StartDate"].ToString(), CultureInfo.InvariantCulture);
    if (Request.QueryString["EndDate"] != null)
        endDate = Convert.ToDateTime(Request.QueryString["EndDate"].ToString(), CultureInfo.InvariantCulture);
    if (Request.QueryString["DisciplineId"] != null)
        disciplineId = Convert.ToInt32(Request.QueryString["DisciplineId"].ToString());
    if (Request.QueryString["MinGrade"] != null)
        minimumGradelevel = Convert.ToInt32(Request.QueryString["MinGrade"].ToString());
    if (Request.QueryString["MaxGrade"] != null)
        maximumGradelevel = Convert.ToInt32(Request.QueryString["MaxGrade"].ToString());
    if (Request.QueryString["LanguageId"] != null)
        languageId = Convert.ToInt32(Request.QueryString["LanguageId"].ToString());
    if (Request.QueryString["Featured"] != null)
        isFeatured = Convert.ToBoolean(Request.QueryString["Featured"].ToString());
    if (Request.QueryString["EducatIds"] != null)
        EducationalInitiativeIds = Request.QueryString["EducatIds"].ToString();
    if (Request.QueryString["Criteria"] != null)
        searchCriteria = Request.QueryString["Criteria"].ToString();
}

然后在代码中:

dtArtist = objArtistLayer.getArtistList(CultureId,startDate, endDate, disciplineId, languageId, minimumGradelevel, maximumGradelevel, isFeatured, sortColumnName, searchCriteria, EducationalInitiativeIds, Stakeholder, currPage, pageSize, out rowCount);

然后稍后:

public DataTable getArtistList( int CultureId ,
                                DateTime? StartDate
                                ,DateTime? EndDate
                                ,int? DisciplineId
                                ,int? LanguageId
                                ,int? MinimumGradeLevel
                                ,int? MaximumGradeLevel
                                ,bool? IsFeature
                                ,string ColumnName
                                ,string Criteria
                                ,string EducationalInitiativeIds
                                ,int ? StakeholderId
                                ,int StartPage   
                                ,int PageSize   
                                ,out int RowCount   
                                )
{
    int? count = 0;
    StartDate = Convert.ToDateTime(StartDate);
    DataTable dt = new DataTable();
    TableAdapters.Artist_ListTableAdapter ada = new TableAdapters.Artist_ListTableAdapter();
    dt = ada.GetData(CultureId
                    , StartDate
                    , EndDate
                    , MinimumGradeLevel
                    , MaximumGradeLevel
                    , IsFeature
                    , ColumnName
                    , Criteria
                    , EducationalInitiativeIds
                    , StakeholderId
                    , DisciplineId
                    , LanguageId
                    , StartPage
                    , PageSize
                    , out count
                    );
    RowCount = (int)count;
    return dt;
4

2 回答 2

3

DateTime没有格式 - 它只是一个日期/时间值。将其转换为字符串然后将其转换回日期是没有用的 - 您将获得相同的值。

当您将QueryString参数从字符串转换为日期时,您的问题更有可能出现。这是您发布的代码中唯一将日期表示为字符串的地方。您需要有某种方法来强制执行特定格式,否则Convert.ToDateTime会尝试推断格式,当月份和日期不明确时,这可能是错误的。例如 2/3/2013 是 2 月 3 日还是 3 月 2 日?

如果您的查询字符串变量始终采用格式,mm/dd/yyyy则使用ParseExact将其转换为DateTime

CultureInfo provider = CultureInfo.InvariantCulture;

if (Request.QueryString["StartDate"] != null)
    startDate = DateTime.ParseExact(Request.QueryString["StartDate"], "MM/dd/yyyy", provider);
if (Request.QueryString["EndDate"] != null)
    endDate = DateTime.ParseExact(Request.QueryString["EndDate"], "MM/dd/yyyy", provider);
于 2012-11-09T20:24:43.130 回答
0

你的例外说你的方法的签名应该是:

TableAdapters.Artist_ListTableAdapter.GetData(int, System.DateTime?, System.DateTime?, int?, int?, bool?, string, string, string, int?, int?, int?, int?, int?, out int?)

但你称它为:

ada.GetData(CultureId,
                    ArtistId
                    , StakeholderId
                    , StartDate
                    , EndDate
                    , DisciplineId
                    , MinimumGradeLevel
                    , MaximumGradeLevel
                    , LanguageId
                    , IsFeature
                    , ShowId
                    );

您可以看到参数中存在不匹配 - 在异常中它表示 DateTime 参数应该是 2nd 和 3 rd。但你称它为第四和第五。

于 2012-11-09T20:09:51.257 回答