C# 如何使用日期调试指定的强制转换无效?

C# 如何使用日期调试指定的强制转换无效?,c#,sql,C#,Sql,这是我在数据库控制器中使用的代码。我确信这段代码没有错,但当我从月历选择器中选择日期时,我不断收到错误消息 { SqlDataReader reader; SqlCommand command; Collection<StockItem> items; try { DateTime input;

这是我在数据库控制器中使用的代码。我确信这段代码没有错,但当我从月历选择器中选择日期时,我不断收到错误消息

        {
            SqlDataReader reader;
            SqlCommand command;
            Collection<StockItem> items;
            try
            {
                DateTime input;
                DateTime.TryParse(date, out input);
                command = new SqlCommand("SELECT * FROM StockItem WHERE DATEDIFF(day, stockItemExpiryDate,'" + input.ToString() + "') >= 0 ORDER BY stockItemShelfNumber", cnMain);
                cnMain.Open();
                command.CommandType = CommandType.Text;
                reader = command.ExecuteReader();
                items = new Collection<StockItem>();
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        StockItem item = new StockItem();
                        item.expiryDate = reader.GetDateTime(1).ToShortDateString();
                        item.shelfNumber = reader.GetString(2);
                        item.numberInStock = reader.GetInt32(3) + "";
                        item.productRef = reader.GetInt32(4) + "";
                        SqlConnection connection = newConn();
                        SqlCommand command2 = new SqlCommand("SELECT productPackaging FROM Product WHERE productID =" + item.productRef + ";", connection);
                        connection.Open();
                        command2.CommandType = CommandType.Text;
                        SqlDataReader reader2 = command2.ExecuteReader();
                        String description = "";
                        if (reader2.HasRows)
                        {
                            reader2.Read();
                            description = reader2.GetString(0);
                        }
                        reader2.Close();
                        connection.Close();
                        item.productRef = description;
                        items.Add(item);
                    }
                }
                reader.Close();
                cnMain.Close();
                this.items = items;
                return items;
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message + "\n" + ex.StackTrace);
                cnMain.Close();
                Console.WriteLine(ex.ToString());
            }
            return null;
        }```
{
SqlDataReader;
SqlCommand命令;
收集项目;
尝试
{
日期时间输入;
DateTime.TryParse(日期,输出输入);
command=new-SqlCommand(“从StockItem中选择*,其中DATEDIFF(day,stockItemExpiryDate,”+“input.ToString()+”)>=0 ORDER BY stockItemShelfNumber”,cnMain);
cnMain.Open();
command.CommandType=CommandType.Text;
reader=command.ExecuteReader();
items=新集合();
if(reader.HasRows)
{
while(reader.Read())
{
StockItem=新的StockItem();
item.expiryDate=reader.GetDateTime(1.ToSortDateString();
item.shelfNumber=reader.GetString(2);
item.numberInStock=reader.GetInt32(3)+;
item.productRef=reader.GetInt32(4)+;
SqlConnection=newConn();
SqlCommand command2=新的SqlCommand(“从productID=“+item.productRef+”;”,连接的产品中选择productPackaging);
connection.Open();
command2.CommandType=CommandType.Text;
SqlDataReader reader2=command2.ExecuteReader();
字符串说明=”;
if(reader2.HasRows)
{
reader2.Read();
description=reader2.GetString(0);
}
reader2.Close();
connection.Close();
item.productRef=描述;
项目。添加(项目);
}
}
reader.Close();
cnMain.Close();
这个项目=项目;
退货项目;
}
捕获(例外情况除外)
{
MessageBox.Show(例如Message+“\n”+ex.StackTrace);
cnMain.Close();
Console.WriteLine(例如ToString());
}
返回null;
}```

从错误中,我假设异常是由

item.expiryDate = reader.GetDateTime(1).ToShortDateString();

从逻辑上讲,StockItem表中的字段1(第二列)不是日期/时间。我会检查您的表,如果您确定这是正确的,可以使用
reader.GetValue(1)
,看看您得到了什么。

欢迎使用堆栈溢出。“我确信这段代码没有什么问题”根据我的经验,这几乎总是一个很好的信号,表明代码会有问题。(作为具体的第一个问题,您忽略了
DateTime.TryParse
的结果。如果
date
不是有效的日期/时间值,您是否确实喜欢静默地使用
DateTime.MinValue
呢?),它有各种潜在的文化导向问题。您还没有告诉我们引发异常的位置,这使得很难进一步帮助您。哦,使用
SELECT*
,但是假设列的特定顺序似乎不是一个好主意。更一般地说,你越不确定你的代码的正确性,你就越有可能发现问题——因为当你确定它是正确的,你就很容易在假设代码是正确的情况下浏览代码。当您认为问题出在代码中时,您更可能仔细查看每一行(您可能希望使用调试器调用
reader.GetName(i)
,以查看第699行上使用的
i
的任何值……并检查它是否是您期望的列)。谢谢您的建议。现在重新登录我的sql表,StockItemExpiryDate的顺序与我在代码中设置的顺序不匹配(第699行)。所以错误信息消失了,但现在唯一的问题是它没有显示过期的项目,所以这是下一件要调试的事情谢谢你,没错。我不得不交换表格中的标题以匹配控制器中的代码这相当突出了Jon关于使用SELECT*的评论很脆弱-更改标题会破坏您的代码。您建议重新键入SELECT*部分吗?我不知道你的表,但基本上,使用“选择字段1、字段2、字段3等。从”意味着无论你的表是如何排列的,代码都会按顺序读取它们。