马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

怎么实现设计数据库时,当我增加相同商品时该商品的库存相应的加1,

  [复制链接]
查看6762 | 回复9 | 2014-8-30 21:49:59 | 显示全部楼层 |阅读模式
怎么实现设计数据库时,当我增加相同商品时该商品的库存相应的加1,急急急

min | 2014-8-31 09:43:07 | 显示全部楼层
触发器
ibcadmin | 2014-8-31 09:44:44 | 显示全部楼层
触发器      不过你不要急着学触发器   ,  你现在可以在新增的时候 在做一个修改  库存++

点评

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namesp  详情 回复 发表于 2014-8-31 11:30
我用的是UPdate来更改的。  详情 回复 发表于 2014-8-31 11:29
C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
jjjttt | 2014-8-31 09:47:52 | 显示全部楼层
[AppleScript] 纯文本查看 复制代码
create trigger tgr_name
on table_name
with encryption
    instead of update...
as
    T-SQL
Testing_C# | 2014-8-31 11:29:45 | 显示全部楼层
ibcadmin 发表于 2014-8-31 09:44
触发器      不过你不要急着学触发器   ,  你现在可以在新增的时候 在做一个修改  库存++

我用的是UPdate来更改的。
Testing_C# | 2014-8-31 11:30:29 | 显示全部楼层
ibcadmin 发表于 2014-8-31 09:44
触发器      不过你不要急着学触发器   ,  你现在可以在新增的时候 在做一个修改  库存++

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace demo_Builder1
{
    public partial class Add_BookMessage : Form
    {
        SqlConn sqlconn;
        public Add_BookMessage()
        {
            InitializeComponent();
        }

        private void Add_BookMessage_Load(object sender, EventArgs e)
        {
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if(Sql_Select()>0)
                {
                    string sql_select = string.Format("update Erp_MessageManage set Erp_Inventory=Erp_Inventory+1 where Erp_BookGigClass='{0}' and Erp_BookSmallClass='{1}'and Erp_BookComeIn='{2}' and Erp_BookNumber='{3}'", this.textBox1.Text, this.textBox2.Text, this.textBox4.Text, this.textBox5.Text, this.textBox3.Text);
                    sqlconn = new SqlConn();
                    sqlconn.SqlInitail(sql_select);
                    sqlconn.GetSqlConnection.Open();
                    int num = Convert.ToInt32(sqlconn.GetSqlCommand.ExecuteNonQuery());
                    if(num>0)
                    {
                        MessageBox.Show("该类别信息已经存在并且库存已经更新成功");
                    }
                }
                else if (Sql_Insert() > 0)
                {
                    MessageBox.Show("添加成功");
                }
                else
                {
                    MessageBox.Show("添加失败");
                }   
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
            finally
            {
                sqlconn.GetSqlConnection.Close();
            }
            
        }
        private int Sql_Select()
        {
            string sql_select = string.Format("select  count(*) from Erp_MessageManage where Erp_BookGigClass='{0}' and Erp_BookSmallClass='{1}'and Erp_BookComeIn='{2}' and Erp_BookNumber='{3}'", this.textBox1.Text, this.textBox2.Text, this.textBox4.Text, this.textBox5.Text, this.textBox3.Text);
            sqlconn = new SqlConn();
            sqlconn.SqlInitail(sql_select);
            sqlconn.GetSqlConnection.Open();
            int num = Convert.ToInt32(sqlconn.GetSqlCommand.ExecuteScalar());
            return num;
        }

        private int Sql_Insert()
        {
            string sql_insert = string.Format("insert into Erp_MessageManage(Erp_BookGigClass,Erp_BookSmallClass,Erp_BookComeIn,Erp_BookNumber,Erp_BookInventoryNumber)values('{0}','{1}','{2}','{3}','{4}')", this.textBox1.Text, this.textBox2.Text, this.textBox4.Text, this.textBox5.Text, this.textBox3.Text);
            sqlconn = new SqlConn();
            sqlconn.SqlInitail(sql_insert);
            sqlconn.GetSqlConnection.Open();  
            int num = Convert.ToInt32(sqlconn.GetSqlCommand.ExecuteNonQuery());
            return num;


        }
    }
}
PErson | 2014-9-1 09:39:37 | 显示全部楼层
触发器吧!直接用到 insert 之前。如果不会用。就建议用老方法。先判断数据库是否存在。存在。则将对字段值加1

点评

触发器是干啥的 好像没见过啊  详情 回复 发表于 2014-9-1 11:24
Testing_C# | 2014-9-1 11:24:12 | 显示全部楼层
PErson 发表于 2014-9-1 09:39
触发器吧!直接用到 insert 之前。如果不会用。就建议用老方法。先判断数据库是否存在。存在。则将对字段值 ...


触发器是干啥的 好像没见过啊
PErson | 2014-9-1 13:19:31 | 显示全部楼层
Testing_C# 发表于 2014-9-1 11:24
触发器是干啥的 好像没见过啊

数据库的一种东西。就是说。编程一个触发器。触发条件就是你只要一insert 。他就会触发。然后在里面编程写到。触发就检测插入的值是否存在。存在就不插入。就加1.不存在就插入。

点评

哦 明白了 呵呵  详情 回复 发表于 2014-9-1 18:03
Testing_C# | 2014-9-1 18:03:38 | 显示全部楼层
PErson 发表于 2014-9-1 13:19
数据库的一种东西。就是说。编程一个触发器。触发条件就是你只要一insert 。他就会触发。然后在里面编程 ...

哦 明白了 呵呵   
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则