c# mysql保存时间,在数据库中只存储日期时间部分C#

I have a test class and an ExecutionDate property which stores only date but when we use [DataType(DataType.Date)] that also stores the time portion in database but I want only date portion.

public class Test

{

[Key]

public int Id { get; set; }

[DataType(DataType.Date)]

public DateTime ExecutionDate { get; set; }

}

Is any way to store only date on time portion in db using Entity Framework? Please help me....

I have added snapshot when use [DataType(DataType.Date)] that stores time portion 00:00 I want remove that

5vmGh.png

解决方案

I think you are trying to specify database column type. You could use data annotations as described in this article.

Here is an example :

[Table("People")]

public class Person

{

public int Id { get; set; }

[Column(TypeName = "varchar")]

public string Name { get; set; }

[Column(TypeName="date")]

public DateTime DOB { get; set; }

}

By default, string is translated to nvarchar, we have changed that here. Also Datetime (this is what you asked I suppose) which by default maps to datatime in sql server, is changed to date which stores only the date portion and not the time portion of a DateTime value.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 C# 窗体程序MySQL 数据库下载文件,可以采用以下步骤: 1. 首先,需要在 MySQL 数据库创建一个 BLOB 字段,并将文件存储在该字段。 2. 在 C# 窗体程序,需要使用 MySQL 数据库连接库和命令库来连接 MySQL 数据库,并执行 SQL 查询语句,以获取包含文件内容的 BLOB 字段。 3. 将 BLOB 数据转换为 byte[] 数组,并将其写入磁盘上的文件。 下面是一个示例代码,演示如何从 MySQL 数据库下载文件: ```c# using System; using System.Data; using System.IO; using System.Windows.Forms; using MySql.Data.MySqlClient; namespace DownloadFileFromMySQL { public partial class Form1 : Form { private MySqlConnection connection; private MySqlCommand command; public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { // 连接 MySQL 数据库 connection = new MySqlConnection("server=localhost;database=test;uid=root;pwd=123456;"); connection.Open(); // 查询包含文件内容的 BLOB 字段 command = new MySqlCommand("SELECT file_content FROM files WHERE file_name=@name", connection); command.Parameters.AddWithValue("@name", "test.txt"); MySqlDataReader reader = command.ExecuteReader(); if (reader.Read()) { // 将 BLOB 数据转换为 byte[] 数组,并将其写入磁盘上的文件 byte[] fileData = (byte[])reader["file_content"]; File.WriteAllBytes("test.txt", fileData); MessageBox.Show("Download completed!"); } else { MessageBox.Show("File not found!"); } reader.Close(); connection.Close(); } } } ``` 以上代码,假设在 MySQL 数据库已经创建了一个名为 `files` 的表,其包含两个字段 `file_name` 和 `file_content`,`file_content` 字段为 BLOB 类型。下载文件时,根据文件名查询对应的 BLOB 数据,并将其写入磁盘上的文件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值