[英]datetime conversion failed when converting date or time from character string SQL (c#)
I am trying to get my program to work, it is using parameters in an SQL query to add it to the database.我试图让我的程序工作,它使用 SQL 查询中的参数将其添加到数据库中。 When I run the program it comes up with "datetime conversion failed when converting date or time from character string".
当我运行程序时,它会出现“从字符串转换日期或时间时日期时间转换失败”。 I have tried many types of coding to make it work and all have failed.
我尝试了许多类型的编码来使其工作,但都失败了。 The SQL code itself works.
SQL 代码本身有效。 It's simply the conversion that is breaking.
这只是打破的转换。 I have tried multiple other posts on here and none have worked.
我在这里尝试了多个其他帖子,但没有一个有效。 Here's my code:
这是我的代码:
string query = $"UPDATE[***].[***] SET [***]= '@FName', [***]= '@SName', [***] = '@Modified', [***] = '@Arrival', [***] = '@Departure' where [Id] = '{id}'; ";
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("@FName", record.guestGiven);
parameters.Add("@SName", record.guestSurname);
parameters.Add("@Modified", record.ModifiedDate.ToString());
parameters.Add("@Arrival", Convert.ToString(record.arrivalDate));
parameters.Add("@Departure", Convert.ToString(record.departureDate));
mySuilvision.InsertData(query, parameters);
I also tried to do the following我还尝试执行以下操作
parameters.Add("@Modified", record.ModifiedDate.ToString("yyyy-mm-dd hh:MM:ss));
and all it came up with is the same error and for the last piece of code the VS error was "cannot convert from string to system.IFormatProvider. Any help is appreciated.它提出的只是相同的错误,对于最后一段代码,VS错误是“无法从字符串转换为system.IFormatProvider。感谢您的帮助。
Change your SQL:更改您的 SQL:
string query = $"UPDATE[***].[***] SET [***]= @FName, [***]= @SName, [***] = @Modified, [***] = @Arrival, [***] = @Departure where [Id] = '{id}'; ";
By putting the parameter names in quotes they are treated as string literals, which won't work.通过将参数名称放在引号中,它们被视为字符串文字,这是行不通的。
Also, why are you adding the ID as a literal?另外,为什么要将 ID 添加为文字? You should add that as a parameter too.
您也应该将其添加为参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.