如何检查字符串是否包含c#中的特定日期 [英] how to check if an string contains a particular date in c#

查看:61
本文介绍了如何检查字符串是否包含c#中的特定日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

以下是我的代码。它有效。

但是我想检查是否有任何日期已经添加到续订日期。那么相同的日期就无法添加。

Hello every one,
Below is my code. It works.
but i want to check if any date is already added to renewal dates.Then same date can not be added.

private void buttonAddToRdates_Click(object sender, EventArgs e)
       {
           string connectionString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Documents and Settings\nic-15\My Documents\ALRS.mdb");
           OleDbConnection con = new OleDbConnection(connectionString);
           con.Open();
           OleDbCommand cmd1 = new OleDbCommand("select renewal_dates from arms_records where thana='" + comboBoxthana.Text + "' and licence_number='" + textBoxLicence_number.Text + "'", con);
           OleDbDataAdapter da = new OleDbDataAdapter(cmd1);
           DataSet ds = new DataSet();
           da.Fill(ds);
           int i = ds.Tables[0].Rows.Count;
           if (i == 1)
           {
               DialogResult result = MessageBox.Show("Previous Renewal Dates:"+cmd1.ExecuteScalar().ToString()+"\n Do you want to add: "+dateTimePickerRenewDate.Value.ToShortDateString()+"  to renewal dates?", "Date will be added to Renewal dates.!!!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
               if (result == DialogResult.Yes)
                {
               //code for yes
                   ds.Clear();
                   string query = "update arms_records set renewal_dates='" + dateTimePickerRenewDate.Value.ToShortDateString() + "'&',' & renewal_dates where thana='" + comboBoxthana.Text + "' and licence_number='" + textBoxLicence_number.Text + "'";
                   OleDbCommand cmd = new OleDbCommand(query, con);
                   cmd.ExecuteNonQuery();
                   con.Close();
                   MessageBox.Show("Date added to Renewal Dates.\nPlease do enter next renewal data before updating record.");
                 }
           }
               else
               {
                   MessageBox.Show("No record found.");
               }
       }

推荐答案

首先,在将日期发送到字符串之前停止将日期转换为字符串访问:事实上,完全停止做这样的事情!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询,并将日期作为DateTime值传递,而不是字符串。这样系统之间就没有本地化问题...



然后,只需将测试从一返回到非零的行数更改为根据您要添加的续订日期检查您检索到的值。
First off, stop converting dates to strings before sending them to Access: in fact, stop doing things like that at all! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead, and pass your date as a DateTime value, not a string. That way there are no localisation problems between systems...

Then, just change your test for number of rows returned from "one" to "non-zero" and check the values you have retrieved against the renewal date you are about to add.


这篇关于如何检查字符串是否包含c#中的特定日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆