gpt4 book ai didi

c# - 如何在 C# 中从 excel 将日期转换为 dd.MM.yyyy 格式

转载 作者:行者123 更新时间:2023-11-30 19:02:48 25 4
gpt4 key购买 nike

在 Excel 工作表中,我的日期格式为 dd.MM.yyyy 16.10.2011 (16 为 dd,10 为 MM)。当我将数据从 excel 导入 SQL Server 2008 时,我得到 MM.dd.yyyy 但仍然是 16.10.2011 (16 作为 MM,10 作为 dd)。那是不正确的。我找到了解决方案:转到 SQL Server 2008 -> 安全 -> 登录 -> {用户属性} -> 并更改用户的默认语言。通过此解决方案,我进入了 SQL Server 2008 dd.MM.yyyy 16.10.2011 (16 作为 dd,10 作为 MM)。有没有其他方法可以在不更改用户语言的情况下将日期转换为 dd.MM.yyyy 格式?

String sConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\JantarV7Export.xlsx;Extended Properties=Excel 8.0";
OleDbConnection olecon = new OleDbConnection(sConnectionString);

olecon.Open();
OleDbCommand cmd = olecon.CreateCommand();
cmd.CommandText = "SELECT person_id, date_of_hours, number_of_hours FROM [Sheet1$]";
OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
OleDbDataReader odr = cmd.ExecuteReader();
conn.Open();
while (odr.Read())
{
SqlCommand cmd1 = conn.CreateCommand();

cmd1.CommandText = "INSERT INTO test_data values (newid(),'" + odr[0] + "',@date_of_hours,'" + odr[2] + "')";
cmd1.Parameters.Add("@date_of_hours", SqlDbType.DateTime).Value =odr[1];
}
cmd1.ExecuteNonQuery();
olecon.Close();
conn.Close();

我认为问题出在我插入 test_data 数据表时。所以我可能应该在插入之前以某种方式转换日期。你有什么建议?谢谢。有没有这样的选项:

cmd1.CommandText = "INSERT INTO test_data values (newid(),'" + odr[0] + 
"',CONVERT(VARCHAR(20),@date_of_hours,104),'" + odr[2] + "')";
cmd1.Parameters.Add("@date_of_hours", SqlDbType.DateTime).Value =odr[1];

(由于插入中的 CONVERT,这不起作用,但是否有一些类似的解决方案)或

CONVERT(VARCHAR(20),@date_of_hours,104) AS [DD:MM:YYYY]

更新在其他形式中,我通过组合框读取数据。它显示 1.10.2011、2.10.2011、.... 到 16.10.2011。

da_test_data = new SqlDataAdapter("SELECT test_data_id, date_of_hours, number_of_hours FROM test_data WHERE _person_id= '" + person_id_person + "' ORDER BY date_of_hours", conn);
dt_test_data = new DataTable();
da_test_data.Fill(dt_test_data);
cb_datum.DataSource = dt_test_data.DefaultView;
cb_datum.ValueMember = "test_data_id";
cb_datum.DisplayMember = "date_of_hours";

项目的组合框:

 private void cb_datum_SelectedIndexChanged(object sender, EventArgs e)
{
DataRowView drvProjekt = cb_datum.SelectedItem as DataRowView;
if (drvProjekt != null)
{
string date1 = drvProjekt["date_of_hours"].ToString();
DateTime date2 = new DateTime();
date2 =Convert.ToDateTime(date1);

//DateTime date = DateTime.ParseExact(date1, "MMddyyyy hh:mm:ss", null);
//DateTime date = DateTime.ParseExact(date1, "dd.MM.yyyy", System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat);

//object obj = DateTime.ParseExact(dddd, "MM/dd/yyyy hh:mm", null);
conn = new SqlConnection(connectionString);
conn.Open();

da_projekt = new SqlDataAdapter("SELECT projekt_id, name_of_projekt, date_of_start, date_of_end FROM projekt WHERE date_of_start < '" + date2 + "' AND date_of_end > '" + date2 + "' ", conn);
dt_projekt = new DataTable();
da_projekt.Fill(dt_projekt);
cb_projekt_id.DataSource = dt_projekt.DefaultView;
cb_projekt_id.ValueMember = "projekt_id";
cb_projekt_id.DisplayMember = "name_of_projekt";
conn.Close();
}
}

当我选择 13.10.2011 时出现错误。日期被识别为 MM.dd.yyyy。我尝试了一切(都在评论中等等)。你找不到解决办法。

最佳答案

正如我在评论中提到的。您的日期值在 SQL 中存储为 Date。因此,当您在 SQL 中存储日期时,它将仅保留日期,而当您检索它时,您将直接获得日期对象。在 SQL 中,它可能显示为 MM.dd.yyyy 没关系。

当您将从 SQL 中读取此数据时,您将获得 DateTime 对象,如果您执行 .ToString("dd.MM.yyyy", CultureInfo.InvariantCulture) 您将获得 dd.MM.yyyy 格式。您可以在 DateTime 对象上使用任何格式,在 MSDN 上阅读更多相关信息.

希望这能回答您的问题。

关于c# - 如何在 C# 中从 excel 将日期转换为 dd.MM.yyyy 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9092506/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com