如何使用C#将日期时间插入SQL服务器? [英] How can I insert a datetime into SQL server with C#?
问题描述
你好。
我有一个问题是在sql server中插入日期时间
我有一个datetimepicker而我试图采取像这样的日期时间18/03/2017 01:00:00 pm
i使用这个句号:
Validacion .fechachange(DTP_Aniversario.Value),
i使用一个类来改变datetimepicker值的格式,这是类:
public class Validacion
{
public static string fechachange(DateTime fecha)
{
return fecha.ToString(yyyy-MM- dd HH':'mm':'ss);
}
}
但返回
2017/03/18 00:00:00
我需要
2017/03/18 01:00:00 pm
但是当把命令发送到sql server时我有这样的错误将数据类型varchar转换为datetime时出错。只有当它有下午
我该怎么做才能解决它?????
我尝试过:
这个我以前在sql server中插入的代码
private void Btn_Aceptar_Click_1(object sender,EventArgs e)
{
string cmd = string.Format(Execute SpInsertar_Cliente' {0}','{1 }','{2}','{3}',
this.Txt_Id_Cliente,
this.Txt_RasonSocial.Text.Trim(),
//参数3错误
//选项1不用担心
// CONVERT(DATETIME,'& Format(FPEDIDO,yyyy-mm-dd hh:mm:ss)&',102),
//选项2不用担心
// Convert.ToDateTime(DTP_Aniversario.Value,yyyy-mm-dd hh:mm:ss),
//选项3不用担心
/ / DTP_Aniversario.Value.ToString(m,CultureInfo.CreateSpecificCulture(en-us)),
//选项4很烦人但有小时 - > 00:00:00
Validacion.fechachange(DTP_Aniversario.Value),
////////// ////////////////////////////////////////////////// ////////////////////
this.CkB_Bloqueado.Checked,
);
Utilidades.EjecSql(cmd,Frm_Login.Conexion);
Mensajeok(Se ingreso correctamente el registro);
}
不要将其转换为字符串:使用DateTime值直接将其作为参数传递 - 系统将确保由SQL作为DATETIME值处理。
永远不要传递字符串,永远不要连接字符串:始终使用参数化查询并传递真实数据类型。
Hello.
I have a problem to insert the datetime in sql server
I have a datetimepicker and I tried to take the datetime like this "18/03/2017 01:00:00 p.m."
i use this sentenses :
Validacion.fechachange(DTP_Aniversario.Value),
i uses a class to chenge the format of datetimepicker value, this is the class:
public class Validacion { public static string fechachange(DateTime fecha) { return fecha.ToString("yyyy-MM-dd HH':'mm':'ss"); } }
but it return
"2017/03/18 00:00:00"
and i need
"2017/03/18 01:00:00 p.m."
but when send the command to sql server i have a error like this "Error converting data type varchar to datetime." only when it has p.m.
what can i do to fix it?????
What I have tried:
this the code that i used to insert in sql server
private void Btn_Aceptar_Click_1(object sender, EventArgs e) { string cmd = string.Format("Execute SpInsertar_Cliente '{0}','{1}','{2}','{3}'", this.Txt_Id_Cliente, this.Txt_RasonSocial.Text.Trim(), // parameter 3 error // option 1 not worck // CONVERT(DATETIME,'" & Format(FPEDIDO, "yyyy-mm-dd hh:mm:ss") & "',102) , " // option 2 not worck // Convert.ToDateTime(DTP_Aniversario.Value, "yyyy-mm-dd hh:mm:ss"), // option 3 not worck // DTP_Aniversario.Value.ToString("m",CultureInfo.CreateSpecificCulture("en-us")), // option 4 it´s worck but has the hour -> 00:00:00 Validacion.fechachange(DTP_Aniversario.Value), //////////////////////////////////////////////////////////////////////////////// this.CkB_Bloqueado.Checked, ); Utilidades.EjecSql(cmd, Frm_Login.Conexion); Mensajeok("Se ingreso correctamente el registro"); }
Don't convert it to a string: pass it as a parameter directly using the DateTime value - the system will ensure that gets processed by SQL as a DATETIME value.
Never pass strings, never concatenate strings: always use parameterised queries and pass "real" datatypes.
这篇关于如何使用C#将日期时间插入SQL服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!