简体   繁体   English

将C#datetime发送到SQL Server-错误“从字符串转换日期和/或时间时转换失败”

[英]Send C# datetime to SQL Server - error “Conversion failed when converting date and/or time from character string”

I am trying to submit a form that contains a few datetimepickers as well as other controls. 我正在尝试提交包含一些datetimepickers以及其他控件的表单。 other controls have no problem but when I take the datetimepicker date value and put it in a parameterised insert statement, this error happens: 其他控件没有问题,但是当我将datetimepicker日期值放入参数化的insert语句中时,会发生此错误:

Conversion failed when converting date and/or time from character string 从字符串转换日期和/或时间时转换失败

I have tried the following methods: 我尝试了以下方法:

  1. custom datetime format: 自定义日期时间格式:

     dateTimePicker1.Format = DateTimePickerFormat.Custom; dateTimePicker1.CustomFormat = "dd/MM/yyyy";' 
  2. convert it to string and then into date time using datetime.parseexact, datetime.parse and Convert.ToDateTime. 使用datetime.parseexact,datetime.parse和Convert.ToDateTime将其转换为字符串,然后转换为日期时间。

  3. tried this type of code as well: 也尝试过这种类型的代码:

     DateTime date2 = Convert.ToDateTime(dateTimePicker2.Text); 

Full code 完整代码

con.Open();

string s = "insert into [NutritionClinicDatabase].[dbo].[PatientInformation] values(@NIC, @ClinicNo, @PatientCategory, @Date, @Name, @Gender, @DOB, @DateOfAdmission, @DateOfDischarge, @DurationOfVisit, @Age, @City, @Contact, @Height, @Weight, @UsualWweight, @ReferredUnit, @WC, @FatMass, @LeanBodyMass, @HandGripStrength, @Intake, @Illness, @Illness2, @Illness3, @Illness4, @Specify, @Other)";


cmd = new SqlCommand(s, con);
String strDateFormat = "yyyy-MM-dd";
cmd.Parameters.AddWithValue("@NIC", NIC);
cmd.Parameters.AddWithValue("@ClinicNo", ClinicNo);
cmd.Parameters.AddWithValue("@PatientCategory", PatientCategory);

if (PatientCategory == "Clinic Patient")
{
    cmd.Parameters.AddWithValue("@Date", SqlDbType.Date).Value = dateTimePicker1.Value.Date;
}
else if ((PatientCategory == "In-Ward Patient") || (PatientCategory == "ICU Patient"))
{
    cmd.Parameters.AddWithValue("@Date", DBNull.Value);
}

cmd.Parameters.AddWithValue("@Name", Name1);
cmd.Parameters.AddWithValue("@Gender", Gender);

//if (dateTimePicker2.CustomFormat == " ")
//    cmd.Parameters.AddWithValue("@DOB", DBNull.Value);
//else
cmd.Parameters.AddWithValue("@DOB", SqlDbType.Date).Value = dateTimePicker2.Value.Date;
// cmd.Parameters.AddWithValue("@DOB", SqlDbType.Date).Value = dateTimePicker2.Value.Date;

if ((PatientCategory == "In-Ward Patient") || (PatientCategory == "ICU Patient"))
{
    //if (dateTimePicker3.CustomFormat == " ")
    //    cmd.Parameters.AddWithValue("@DateOfAdmission", DBNull.Value);
    //else
    cmd.Parameters.AddWithValue("@DateOfAdmission", SqlDbType.Date).Value = dateTimePicker3.Value.Date;
    // cmd.Parameters.AddWithValue("@DOB", SqlDbType.Date).Value = dateTimePicker2.Value.Date;
    //if (dateTimePicker4.CustomFormat == " ")
    //    cmd.Parameters.AddWithValue("@DateOfDischarge", DBNull.Value);
    //else
    cmd.Parameters.AddWithValue("@DateOfDischarge", SqlDbType.Date).Value = dateTimePicker4.Value.Date;
    //cmd.Parameters.AddWithValue("@DateOfDischarge", DateTime.Parse(dateTimePicker4.Value.Date.ToString()));
    // cmd.Parameters.AddWithValue("@DOB", SqlDbType.Date).Value = dateTimePicker2.Value.Date;

    cmd.Parameters.AddWithValue("@DurationOfVisit", Duration);
}
else if (PatientCategory == "Clinic Patient")
{
    cmd.Parameters.AddWithValue("@DateOfAdmission", DBNull.Value);
    cmd.Parameters.AddWithValue("@DateOfDischarge", DBNull.Value);
    cmd.Parameters.AddWithValue("@DurationOfVisit", "Not Specified");
}

cmd.Parameters.AddWithValue("@Age", Age);
cmd.Parameters.AddWithValue("@City", City);
cmd.Parameters.AddWithValue("@Contact", Contact);
cmd.Parameters.AddWithValue("@Height", Height1);
cmd.Parameters.AddWithValue("@Weight", Weight);
cmd.Parameters.AddWithValue("@UsualWweight", UsualWeight);
//cmd.Parameters.AddWithValue("@CurrentWeight", CurrentWeight);
cmd.Parameters.AddWithValue("@ReferredUnit", ReferredUnit);
cmd.Parameters.AddWithValue("@WC", WC);
cmd.Parameters.AddWithValue("@FatMass", FatMass);
cmd.Parameters.AddWithValue("@LeanBodyMass", LeanBodyMass);
cmd.Parameters.AddWithValue("@HandGripStrength", HandGripStrength);
cmd.Parameters.AddWithValue("@Intake", Intake);
cmd.Parameters.AddWithValue("@Illness", Illness);
cmd.Parameters.AddWithValue("@Illness2", Illness2);
cmd.Parameters.AddWithValue("@Illness3", Illness3);
cmd.Parameters.AddWithValue("@Illness4", Illness4);
cmd.Parameters.AddWithValue("@Specify", Specify);
cmd.Parameters.AddWithValue("@Other", Other);

cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();

con.Close();

MessageBox.Show("Patient Registered");

You are not using AddWithValue correctly. 您没有正确使用AddWithValue The AddWithValue method takes a parameter name and an object that is the parameter value, not the type. AddWithValue方法采用参数名称和对象,该对象是参数值,而不是类型。 I suggest you avoid AddWithValue entirely and instead use the Add method, such as: 我建议您完全避免使用AddWithValue ,而应使用Add方法,例如:

cmd.Parameters.Add("@Date", SqlDbType.Date).Value = dateTimePicker1.Value.Date;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从字符串 SQL (c#) 转换日期或时间时,日期时间转换失败 - datetime conversion failed when converting date or time from character string SQL (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#中的字符串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#“从字符串转换日期/时间时转换失败”但查询在服务器上运行时效果很好 - C# "Conversion failed when converting date/time from character string" But query works well when run on server 从字符串错误4转换日期和/或时间时转换失败 - Conversion failed when converting date and/or time from character string error 4 错误“从字符串转换日期和/或时间时转换失败” - Error “Conversion failed when converting date and/or time from character string” 错误:从字符串转换日期和/或时间时转换失败 - Error: Conversion failed when converting date and/or time from character string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM