如果 `clsTemp.ProductDate` 是一个字符串类型,你可以使用 `DateTime.Parse()` 或 `DateTime.TryParse()` 方法将其转换为 `DateTime` 类型,然后再使用 `ToString()` 方法将其格式化为年月的字符串。例如:
```csharp
string dateString = dr["生产日期"].ToString();
DateTime date;
bool success = DateTime.TryParse(dateString, out date);
if (success)
{
string yearMonth = date.ToString("yyyy年MM月");
clsTemp.ProductDate = yearMonth;
}
else
{
// 转换失败,处理异常情况
}
```
这个例子中,我们首先将数据库中读取到的生产日期转换为字符串类型,并使用 `DateTime.TryParse()` 方法将其转换为 `DateTime` 类型。如果转换成功,我们使用 `ToString()` 方法将其格式化为 "yyyy年MM月" 的字符串,并赋值给 `clsTemp.ProductDate` 变量。
需要注意的是,如果 `dr["生产日期"]` 返回的是 `DBNull.Value`,则 `dr["生产日期"].ToString()` 会抛出异常。在实际开发中,应该对此进行判断和处理。