您正在使用字符文字'',它只能包含一个字符。如果您想使用字符串文字,请改用""。
C# 不支持 DateTime-literals 而不是 VB.NET (#4/30/1998#)。
除此之外,字符串不是DateTime。如果您有一个字符串,您需要先将其解析为DateTime:
string published = "1998,04,30";
DateTime dtPublished = DateTime.ParseExact(published, "yyyy,MM,dd", CultureInfo.InvariantCulture);
mySmallVuln.Published = dtPublished;
或者您可以通过构造函数创建DateTime:
DateTime dtPublished = new DateTime(1998, 04, 30);
或者,由于您的字符串包含年、月和日作为字符串,使用String.Split 和int.Parse:
string[] tokens = published.Split(',');
if (tokens.Length == 3 && tokens.All(t => t.All(Char.IsDigit)))
{
int year = int.Parse(tokens[0]);
int month = int.Parse(tokens[1]);
int day = int.Parse(tokens[2]);
dtPublished = new DateTime(year, month, day);
}