Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更新access表中的datetime_C#_Ms Access_Datetime_Oledbcommand_Oledbparameter - Fatal编程技术网

C# 更新access表中的datetime

C# 更新access表中的datetime,c#,ms-access,datetime,oledbcommand,oledbparameter,C#,Ms Access,Datetime,Oledbcommand,Oledbparameter,我试图将datetime值插入datatable,然后使用oledbdataadapter的update(datatable)方法将其加载到数据库中。。但我一直得到一个“条件表达式中的数据类型不匹配”错误。表中我的访问数据类型为: ID Number Nombre_Proyecto Text Codigo_Ine_Proy Text Cliente text Fecha_Creacion Datetime (short date) 根据access,短日期是mm/dd/yyy,这与我的dat

我试图将datetime值插入datatable,然后使用oledbdataadapter的update(datatable)方法将其加载到数据库中。。但我一直得到一个“条件表达式中的数据类型不匹配”错误。表中我的访问数据类型为:

ID Number
Nombre_Proyecto Text
Codigo_Ine_Proy Text
Cliente text
Fecha_Creacion Datetime (short date) 
根据access,短日期是mm/dd/yyy,这与我的datetime/ToSortDateString方法相符吗?我想至少是这样

任何帮助都将不胜感激。这是我的密码:

将OLEDB命令插入数据适配器:

  sql = "PARAMETERS [@Fecha_Creacion] datetime;INSERT Into [Proyectos] ([ID], [Nombre_Proyecto],[Codigo_Ine_Proy],[Cliente],[Fecha_Creacion]) Values (@ID,@Nombre_Proyecto,@Codigo_Ine_Proy,@Cliente,@Fecha_Creacion)";
  Comando = new OleDbCommand(sql, conn);
  Comando.Parameters.Add("@Nombre_Proyecto", OleDbType.VarWChar, 500, "Nombre_Proyecto");
  Comando.Parameters.Add("@Codigo_Ine_Proy", OleDbType.VarWChar, 500, "Codigo_Ine_Proy");
  Comando.Parameters.Add("@Cliente", OleDbType.VarWChar, 500, "Cliente");
  Comando.Parameters.Add("@Fecha_Creacion", DbType.DateTime);
  Comando.Parameters.Add("@ID", OleDbType.Integer, 10000, "ID");
我在数据表上创建数据行的部分

  DataRow newRow = Tabla_Proyectos_BD_General.NewRow();
  Max_IDs["Proyectos"] += 1;
  newRow["ID"] = Max_IDs["Proyectos"];
  newRow["Nombre_Proyecto"] = textBox2.Text;
  newRow["Codigo_Ine_Proy"] = textBox1.Text;
  newRow["Cliente"] = textBox3.Text;
  string x = System.DateTime.Now.ToShortDateString();
  newRow["Fecha_Creacion"] = x;
  Tabla_Proyectos_BD_General.Rows.Add(newRow);
这是一个字符串,不是日期时间!因此不匹配

newRow[“Fecha_Creacion”]=System.DateTime.Now

您的参数化查询应该可以为您完成

如果你想以shortdatestring格式显示你输入的日期(无论是什么格式,都可以在电脑上进行格式化),请将其作为日期时间,然后根据需要进行格式化

PS如果您想将日期作为字符串传递给数据库,请使用yyyy MM dd或yyyyMMdd格式。除通用和明确的日期格式外,任何其他格式都只是一个等待发生的错误,除非您必须这样做,否则决不会这样做

提示:输出日期时,将其转换为某种格式的字符串是最后一项操作,输入日期时,应首先从字符串转换为日期时间

评论后编辑 最简单的解决办法是

compando.Parameters.Add(“@Fecha_Creacion”,DbType.DateTime,System.DateTime.Now);

newRow["Fecha_Creacion"] = System.DateTime.Now;

您在Access中看到的是“格式化日期”。当通过OleDB进行交互时,您需要使用DateTime,而不是格式化字符串。

我这样做时会出现此异常,但我不知道为什么!!参数“@Fecha_Creacion”的数据类型错误。我已尝试更改参数中的dbtype.DateTime,但在对op中的注释感到困惑后没有成功
newRow["Fecha_Creacion"] = System.DateTime.Now;