简体   繁体   English

从字符串 SQL (c#) 转换日期或时间时,日期时间转换失败

[英]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.

相关问题 将C#datetime发送到SQL Server-错误“从字符串转换日期和/或时间时转换失败” - Send C# datetime to SQL Server - error “Conversion failed when converting date and/or time from character string” 从C#中的字符串SQL转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string SQL in c# 从字符串转换日期和/或时间时,DateTime转换失败 - DateTime conversion failed when converting date and/or time from character string 从字符串DateTime转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string DateTime 从C#中的字符串转换日期时间时转换失败 - Conversion failed when converting datetime from character string in C# 使用c#从字符串转换日期和/或时间从字符串转换时失败,以便检查SQL Server数据库中的表 - Conversion failed when converting date and/or time from character string, using c# in order to check a table in SQL Server database 从字符串转换日期和/或时间时,C#转换失败 - C# Conversion failed when converting date and/or time from character string 从字符串C#转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string C# 从C#中的字符串转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string in C# 从字符串转换日期时,SQL / C#转换失败 - SQL / C# Conversion failed when converting date from character string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM