如何在访问表中插入increment(+1)值 [英] How to insert increment(+1) value in access table

查看:76
本文介绍了如何在访问表中插入increment(+1)值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在该应用程序中创建一个应用程序我设置sales_count + 1如果特定用户输入详细信息,那么在数据库sales_count中应该是1 + 1 = 2比如,这个sales_count我根据今天的日期设置,现在我想要的是如果数据库日期和系统日期匹配然后触发UPDATE查询但是如果没有然后抛出插入查询我写的代码为UPDATE但我无法写我怎么写sales_count + 1在插入查询中如果找到新的日期系统然后插入查询应该在系统中插入sales_count + 1和当前日期



注意:preoblem是我无法写入插入查询是使用当前日期增加特定Unique_no的sales_count,我使用MS-ACCESS作为数据库



我尝试过:



Hi all,

im creating one application in that application i set sales_count+1 if particular user enter detail then in database sales_count should be 1+1=2 like , this sales_count i set base on TODAY date , now i what i want if Database date and system date match then trigger UPDATE query but if not then throw insert query i written code for UPDATE but i unable to write how can i write sales_count+1 in insert query like if new Date system found then insert query should insert sales_count+1 and current date in sytem

Note: preoblem is i unable to write insert query which is increment sales_count for specific Unique_no with current date , im using MS-ACCESS as database

What I have tried:

this.txtinput.MaxLength = 4;

string connectionString = null;
            connectionString = ConfigurationManager.ConnectionStrings["AccessConnectionString"].ConnectionString;
            con.ConnectionString = connectionString;

string access = "select To_Date from Login";
             cmd = new OleDbCommand(access, con);
              con.Open();
              using (OleDbDataReader read = cmd.ExecuteReader())
              {
                  while (read.Read())
                  {
                      string date2 = read["To_Date"].ToString();
                      if (todaydate == date2)
                      {
                          cmd = new OleDbCommand("update Login set Sales_count= IIF(IsNull(Sales_count), 0, Sales_count) + 1, [To_Date]=Date() where [Unique_No]=@Unique_No", con);
                          cmd.Parameters.AddWithValue("@Unique_No", txtinput.Text);
                          con.Open();
                          int n = cmd.ExecuteNonQuery();

                          if (n == 0)
                          {
                              MessageBox.Show("Invalid Unique No.");

                          }
                          else
                          {
                              this.DialogResult = DialogResult.OK;
                          }

                          con.Close();

                       

                      }
                      else
                      {
                           cmd = new OleDbCommand("IF NOT EXISTS(SELECT 1 FROM Login WHERE Unique_No = @Unique_No) INSERT INTO(Sales_count,To_Date)values(?,Date()) ", con);
                          cmd.Parameters.AddWithValue("@Unique_No", txtinput.Text);
                          con.Open();
                          int n = cmd.ExecuteNonQuery();

                          if (n == 0)
                          {
                              MessageBox.Show("Invalid Unique No.");

                          }
                          else
                          {
                              this.DialogResult = DialogResult.OK;
                          }
                          con.Close();


                      }
                  }
              }
               con.Close();

推荐答案

见这里: ms access - 按顺序为表中的分组记录插入排名 - Stack Overflow [ ^ ]


检查我的asnwer问题:如何从select插入另一个表数据查询存在表 [ ^ ]
Check my asnwer to that question: How to insert another table data from select query for exist table[^]


这篇关于如何在访问表中插入increment(+1)值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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