如何编写着装预订代码并阻止在注册日期进行预订 [英] How do I write dress booking code and prevent booking in registeresed date

查看:69
本文介绍了如何编写着装预订代码并阻止在注册日期进行预订的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为礼服租赁店编写代码,并阻止在预订日期和后一天预订



我尝试过:



i want to write code for dress rent shop and prevent booking in that booked date and the day after

What I have tried:

try
           {

               OleDbConnection con = new OleDbConnection();
               con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=marriage_dresses.accdb;";
               con.Open();
               OleDbCommand cmd = new OleDbCommand();
               string selectdata = "select dress_name from booking where take_date between @date1 AND @date2 AND dress_name=@name";
               cmd = new OleDbCommand(selectdata,con);
               cmd.Parameters.AddWithValue("@date1",dateTimePicker1.Value.Date);
               cmd.Parameters.AddWithValue("@date2",dateTimePicker2.Value.Date);
               cmd.Parameters.AddWithValue("@name",comboBox1.Text);
               OleDbDataAdapter da = new OleDbDataAdapter(cmd);
               DataTable dt = new DataTable();
               da.Fill(dt);
               con.Close();
               if (dt.Rows.Count != 0)
               {
                   MessageBox.Show("cannot book at this date");

               }
               else
               {
                   try
                   {

                       OleDbConnection con2 = new OleDbConnection();
                       con2.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=marriage_dresses.accdb;";
                       con2.Open();
                       OleDbCommand cmd2 = new OleDbCommand();
                       string insertdata = "insert into booking(dress_name,person_name,take_date,return_date,total_price,payed,rest,entry_name) values (@val1,@val2,@val3,@val4,@val5,@val6,@val7,@val8)";
                       cmd2 = new OleDbCommand(insertdata, con2);
                       cmd2.Parameters.AddWithValue("@val1", comboBox1.Text);
                       cmd2.Parameters.AddWithValue("@val2", textEdit1.Text);
                       cmd2.Parameters.AddWithValue("@val3", dateTimePicker1.Value);
                       cmd2.Parameters.AddWithValue("@val4", dateTimePicker2.Value);
                       cmd2.Parameters.AddWithValue("@val5", textEdit2.Text);
                       cmd2.Parameters.AddWithValue("@val6", textEdit3.Text);
                       double c = Convert.ToDouble(textEdit2.Text) - Convert.ToDouble(textEdit3.Text);
                       cmd2.Parameters.AddWithValue("@val7", c);
                       cmd2.Parameters.AddWithValue("@val8", "");
                       cmd2.ExecuteNonQuery();
                       MessageBox.Show("booking succesful");
                       string query = "select * from booking";
                       OleDbCommand cmd3 = new OleDbCommand(query, con2);
                       OleDbDataAdapter da2 = new OleDbDataAdapter(cmd3);
                       DataTable dt2 = new DataTable();
                       da2.Fill(dt2);
                       gridControl1.DataSource = dt2;
                       textEdit1.Text = "";
                       textEdit2.Text = "";
                       textEdit3.Text = "";


                       con.Close();

                   }
                   catch (Exception)
                   {
                       MessageBox.Show("error");
                   }

推荐答案

这需要一点工作:你不需要使用DataAdapter,你不需要返回服装名称 - 你已经知道了!

相反,考虑返回一个COUNT,并使用命令上的ExecuteScalar方法直接返回一个数字。



但是......你还需要更仔细地考虑你的情况:只是检查并且现有的采取日期不是在所需的雇佣期限的开始或结束之间。开始工作:

That needs a little work: you don't need to use a DataAdapter, and you don't need to return the dress name - you already know that!
Instead, consider returning a COUNT, and use the ExecuteScalar method on the command to return a number directly.

But ... you also need to think about your conditions rather more carefully: just checking and existing "take date" isn't between the start or end of teh required hire period isn't going to work:
Existing hire: 1st to 10th March.
Required hire: 5th to 15th March.

即使礼服需要同时在两个地方,也会立即返回没有行,你会说这没关系......



我建议你坐下来用笔和纸,找出所有可能的排列,看看你是否能够提出适合所有这些排列的条件在你急于代码之前。

Will immediately return no rows and you will say that that is ok, despite the dress needing to be in two places at the same time...

I'd suggest that you sit down with a pen and paper, and work out all the possible permutations and see if you can come up with conditions which fit all of them before you rush into code.


这篇关于如何编写着装预订代码并阻止在注册日期进行预订的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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