【问题标题】:textBox to DateTime in DB数据库中的文本框到日期时间
【发布时间】:2019-08-13 11:52:07
【问题描述】:

我正在尝试从文本框中获取 DateTime,它已经采用 MySql DateTime 的格式。 DB中的列也是DateTime格式。

但是,当我按下按钮将日期保存在数据库中时,整行都会被清空。

我在 DB 中尝试了不同的格式和数据类型,但没有任何效果

private void button4_Click(object sender, EventArgs e)
{
    MySqlConnection conn = DBUtils.GetDBConnection();
    conn.Open();

    string startzeit = textBoxstartzeit.Text.ToString();
    DateTime start = DateTime.Parse(startzeit); 
    string stopzeit = textBoxstopzeit.Text.ToString();
    DateTime stop = DateTime.Parse(stopzeit);
    string pstartzeit = textBoxstopzeit.Text.ToString();
    DateTime pstart = DateTime.Parse(pstartzeit);
    string pstopzeit = textBoxstopzeit.Text.ToString();
    DateTime pstop = DateTime.Parse(pstopzeit);

    MySqlCommand cmdnew = conn.CreateCommand();
    cmdnew.CommandType = CommandType.Text;
    cmdnew.CommandText = "UPDATE arbeitszeiten SET astart = '" + start + "',  astop = '" + stop + "', pstart = '" + pstart + "', pstop = '" + pstop + "' WHERE id = '" + dataGridView.CurrentCell.Value + "'";
    cmdnew.ExecuteNonQuery();

    conn.Close();
}

private void dataGridView_CellClick(object sender, DataGridViewCellEventArgs e)
{
    try
    {
        MySqlConnection conn = DBUtils.GetDBConnection();
        conn.Open();

        MySqlCommand feedstartzeit = conn.CreateCommand();
        feedstartzeit.CommandText = "SELECT astart FROM arbeitszeiten WHERE id = '" + dataGridView.CurrentCell.Value + "'";
        DateTime start = Convert.ToDateTime(feedstartzeit.ExecuteScalar());
        textBoxstartzeit.Text = start.ToString("yyyy-MM-dd HH:mm:ss");

        MySqlCommand feedstopzeit = conn.CreateCommand();
        feedstopzeit.CommandText = "SELECT astop FROM arbeitszeiten WHERE id = '" + dataGridView.CurrentCell.Value + "'";
        DateTime stop = Convert.ToDateTime(feedstopzeit.ExecuteScalar());
        textBoxstopzeit.Text = stop.ToString("yyyy-MM-dd HH:mm:ss");

        MySqlCommand feedstartpause = conn.CreateCommand();
        feedstartpause.CommandText = "SELECT pstart FROM arbeitszeiten WHERE id = '" + dataGridView.CurrentCell.Value + "'";
        DateTime startpause = Convert.ToDateTime(feedstartpause.ExecuteScalar());
        textBoxstartpause.Text = startpause.ToString("yyyy-MM-dd HH:mm:ss");

        MySqlCommand feedstoppause = conn.CreateCommand();
        feedstoppause.CommandText = "SELECT pstop FROM arbeitszeiten WHERE id = '" + dataGridView.CurrentCell.Value + "'";
        DateTime stoppause = Convert.ToDateTime(feedstoppause.ExecuteScalar());
        textBoxstoppause.Text = stoppause.ToString("yyyy-MM-dd HH:mm:ss");
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Bitte ID auswählen", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Button4 是上传的新数据,dataGridView 部分使用预先格式化的日期时间填充文本框,稍后由 button4 上传

【问题讨论】:

  • 为什么你使用 TextBox 而不是 DateTime 组件?使用DateTimePicker,那么您不需要将字符串转换为日期。
  • 因为我也需要时间
  • 这是什么类型的用户界面?例如,WinForms 中的DateTimePicker 也可以处理时间。
  • 附带说明,我非常强烈地建议您停止执行这样的 SQL - 改用参数化 SQL,否则您将自己向 SQL 开放注入漏洞。
  • “我看不出有必要,因为它是一个私人项目”——我强烈建议你现在就养成好习惯。为什么要养成您已经知道容易受到安全漏洞的习惯?通过使用参数化 SQL,您最终可以得到更简洁的代码和更少的转换问题。

标签: c# sql datetime datagridview


【解决方案1】:

你的日期时间格式应该是你的服务器日期时间格式,如果你想使用日期时间那么你应该使用日期时间选择器,这样你就不需要转换成日期时间了。

【讨论】:

    【解决方案2】:

    好的,Jon Skeet 建议的参数化 sql 请求解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 2020-07-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多