c# - 如何使用 C# 代码在 MySql 数据库中插入 DateTime

标签 c# mysql

2/1/2009 5:04:15 AM

我从 MySQL 数据库获取它,并想将它从我获取它的其他地方提供到表中。

但它在 C# 中不起作用,但使用 gui 它可以。

我如何使用 C# 代码将此日期提供给 MySQL 数据库。

最佳答案

如果您使用 MySqlCommand 类,您应该能够使用具有以下结构的 DateTime:

using (var command = connection.CreateCommand())
{
    command.CommandText = "INSERT INTO table (dateTimeColumn) VALUES (@1)";

    command.Parameters.AddWithValue("@1", DateTime.Now);

    command.ExecuteNonQuery();
}

这应确保正确存储日期/时间。

关于c# - 如何使用 C# 代码在 MySql 数据库中插入 DateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4153799/

相关文章:

php - 在 mysql/php 中选择列名

mysql - 从表格的第一行添加列值并显示另一列

php - 生成错误 INSERT 语句的 Doctrine

c# - 将 .sql 文件传递​​给 SQL Server 执行

c# - MigraDoc:如何将前面的空格添加到字符串?

c# - 如何确定新创建的对象是否具有用户设置的任何属性值

php - MySQL 语法错误 : ORDER BY clause is not in GROUP BY

php - 用于评论和回复 PHP 应用程序的递归函数

c# - 超链接 NavigateUrl 的正确语法

c# - 编写脚本程序