【问题标题】:Is this search code with any date format as the following right? or not?此搜索代码是否具有以下任何日期格式?或不?
【发布时间】:2013-03-13 09:39:37
【问题描述】:

此搜索代码是否具有如下任何日期格式?捕捉日期格式“dd-MM-yyyy”。 我的桌子是:

id |   dat    | user | remain
-----------------
1 | 01-03-2013 | x | 1000
----------------
2 | 01-03-2013 | x | 1200
----------------
3 | 02-03-2013 | y | 1100
----------------
4 | 02-03-2013 | y | 1300
----------------
5 | 03-03-2013 | z | 1200
----------------
6 | 03-03-2013 | z | 1400
-------------------------

private void textBox10_Leave(object sender, EventArgs e)
    {
        if ((textBox10.Text == "yyyy/MM/dd") || (textBox10.Text == "dd/MM/yyyy") || (textBox10.Text == "yyyy-MM-dd"))
        {
            textBox10.Text = "dd-MM-yyyy";
            using (SqlConnection cn = new SqlConnection(Class1.x))
            {
                cn.Open();
                string cm = "select id from item_treasury where dat='" + textBox10.Text + "' order by id";
                using (SqlCommand cmd = new SqlCommand(cm, cn))
                {
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read() == true)
                        {
                            if (dr.HasRows == false)
                            {
                                MessageBox.Show("xxxxxxxxxxxxxxxxxxxx");
                                this.Close();
                            }
                            comboBox1.Items.Add(dr[0].ToString());
                        }
                    }
                }
            }
        }
    }

我想更正这段代码。 请帮助我!

【问题讨论】:

  • 不,这是不对的。这很容易出现SQL injection,最糟糕的那种......使用准备好的语句!
  • 这也不会传递日期本身,它会传递格式。你想在这里做什么以日期格式传递? textBox10 也不是一个有用的名称,comboBox1 也不是您应该更改这些名称。
  • 为什么任何输入都不像“dd-MM-yyyy”这样的文本搜索程序没有响应?
  • 您应该使用参数并将日期作为 date 传递给 SQL 服务器,而不是作为一些任意格式的字符串。而且,希望在服务器端,数据存储在类型为datetime(或更现代的变体之一)的列中。将日期存储或处理为字符串是(与日期相关的)错误的最大来源之一。
  • 如果我将 02/03/2013 输入到 textBox10.Text 结果是 (3) 和 (4) 到 comboBox1.Items,如果我输入的文本不是之前的状态,程序会给我一个消息。

标签: c# sql date search


【解决方案1】:

我认为您的文本框中有一个日期,例如 2012 年 12 月 31 日,因此您确实希望将其作为日期,并且您是否使用该日期进行 sql 查询。此时你只是在 TextBox 中寻找格式化字符串。

DateTime date;
if (DateTime.TryParse(textBox10.Text, (... date formats in string array), null, null, out date)
{
    ....
         string cm = "select id from item_treasury where dat='" + date.ToString("dd-MM-yyyy") + "' order by id";
    ....
}

最好将日期保存为数据库中的日期,并使用参数化查询来检索数据。

【讨论】:

  • 感谢您的重播。我想如果我将 02/03/2013 输入到 textBox10.Text 中,结果是 (3) 和 (4) 到 comboBox1.Items 中,如果我输入的文本不是之前的状态,程序会给我一条消息。
  • 新字符串[] {"dd-MM-yyyy", "dd/MM/yyyy" }
【解决方案2】:

谢谢大家, 我最后的正确代码:

DateTime date;
        string[] formats = { "dd/MM/yyyy", "dd-MM-yyyy", "d/MM/yyyy", "d-MM-yyyy", "yyyy/MM/dd", "yyyy-MM-dd", "yyyy/M/dd", "yyyy-M-dd", "dd/M/yyyy", "dd-M-yyyy" }; //////////if (DateTime.TryParse(textBox10.Text, null, System.Globalization.DateTimeStyles.None, out date))
        if (DateTime.TryParseExact(textBox10.Text, formats, System.Globalization.CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
        {
            using (SqlConnection cn = new SqlConnection(Class1.x))
            {
                cn.Open();
                string cm = "select id from item_treasury where dat='" + date.ToString("yyyy-MM-dd") + "' order by id";
                ///////////****
                using (SqlCommand cmd = new SqlCommand(cm, cn))
                {
                    using (SqlDataReader dr = cmd.ExecuteReader())
                    {
                        while (dr.Read() == true)
                        {
                            if (dr.HasRows == false)
                            {
                                MessageBox.Show("xxxxxxxxxxxxxxxxxx");
                                this.Close();
                            }
                            comboBox1.Items.Add(dr[0].ToString());
                        }
                    }
                }
            }
        }

【讨论】:

    猜你喜欢
    • 2011-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多