首页 新闻 会员 周边

winform连接oracle插入值问题

0
悬赏园豆:20 [已解决问题] 解决于 2017-12-20 16:12

winform项目,连接oracle数据库

在一个页面插入值到数据库中时,出现了问题

提示:ora:00001 违反了主键约束

但是!!!

在PL/SQL中看表内的值时,数据是插入的表中的!

实验了几次,都是同样的,值插入成功,但提示违反主键约束。myORACCommand.ExecuteNonQuery()验证不行。

求各位帮忙!!!

急急急!!!

问题补充:

使用的插入语句是:insert into  tablename values(...);

Booneey的主页 Booneey | 初学一级 | 园豆:177
提问于:2017-12-20 14:53
< >
分享
最佳答案
0

ocmd.ExecuteNonQuery(); if (ocmd.ExecuteNonQuery() > 0) { MessageBox.Show("执行成功!"); }

 

你代码执行了两次。。。。要拿执行结果第一个Execute完直接赋值给一个变量

收获园豆:12
Daniel Cai | 专家六级 |园豆:10424 | 2017-12-20 16:07

解决了,谢谢!

Booneey | 园豆:177 (初学一级) | 2017-12-20 16:11
其他回答(1)
0

oracle的表结构是什么  主键是用的什么,是用的序列吗?

收获园豆:8
87Super | 园豆:107 (初学一级) | 2017-12-20 15:40
-- Create table
create table LIVE
(
  live_id             NVARCHAR2(10) not null,
  reserve_id          NVARCHAR2(10),
  customer_id         NVARCHAR2(10) not null,
  room_id             NVARCHAR2(10) not null,
  live_num            NUMBER,
  live_num_id         NVARCHAR2(10),
  live_time           DATE not null,
  live_day            NUMBER not null,
  staff_id            NVARCHAR2(10) not null,
  deposit_total_money NUMBER not null
)
tablespace SYSTEM
  pctfree 10
  pctused 40
  initrans 1
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
-- Add comments to the table 
comment on table LIVE
  is '入住表';
-- Add comments to the columns 
comment on column LIVE.live_id
  is '入住id';
comment on column LIVE.reserve_id
  is '预定id';
comment on column LIVE.customer_id
  is '顾客id';
comment on column LIVE.room_id
  is '房间id';
comment on column LIVE.live_num
  is '入住人数';
comment on column LIVE.live_num_id
  is '入住人数id';
comment on column LIVE.live_time
  is '入住时间';
comment on column LIVE.live_day
  is '入住天数';
comment on column LIVE.staff_id
  is '员工id';
comment on column LIVE.deposit_total_money
  is '押金总金额';
-- Create/Recreate primary, unique and foreign key constraints 
alter table LIVE
  add constraint PK_LIVE_ID primary key (LIVE_ID)
  using index 
  tablespace SYSTEM
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64K
    next 1M
    minextents 1
    maxextents unlimited
  );
alter table LIVE
  add constraint FK_LCUSTOMER_ID foreign key (CUSTOMER_ID)
  references CUSTOMER (CUSTOMER_ID);
alter table LIVE
  add constraint FK_LRESERVE_ID foreign key (RESERVE_ID)
  references RESERVE (RESERVE_ID);
alter table LIVE
  add constraint FK_LROOM_ID foreign key (ROOM_ID)
  references ROOM (ROOM_ID);
alter table LIVE
  add constraint FK_LSTAFF_ID foreign key (STAFF_ID)
  references STAFF (STAFF_ID);
支持(0) 反对(0) Booneey | 园豆:177 (初学一级) | 2017-12-20 15:47

@秦暮川: 看了一下 你相当于是在表里面没有设定主键的默认值,主键是程序插入的是吧?

支持(0) 反对(0) 87Super | 园豆:107 (初学一级) | 2017-12-20 15:52

@87Super: 对

支持(0) 反对(0) Booneey | 园豆:177 (初学一级) | 2017-12-20 15:56

@秦暮川: 会不会是代码执行了2次?

支持(0) 反对(0) 87Super | 园豆:107 (初学一级) | 2017-12-20 16:03

@87Super: 

  string conn = "Data Source=myorcl; User ID=system;Password=oracle";
            OracleConnection myConnection = new OracleConnection(conn);//建立oracle数据库连接
          
            liveday = textBox7.Text;//入住天数
            if(liveday!=null)
            {
                getdepositnum();//'"++"'   
            }                         
          
            //insert into live values('l002','rs001' ,'c001','r2003',
            //to_number('0'),'0',to_date('2017-5-9','yyyy-MM-dd hh24:mi;ss'),
            //to_number('5'),'s001',to_number('5000')); 1 row inserted
            // (live_id, reserve_id, customer_id, room_id, live_num, live_num_id, live_time, live_day, staff_id, deposit_total_money)
            string allsql = "insert into live values('"+ liveid+ "','"+reserveid+"' ,'"+customerid+"','"+rooid+"',to_number('0'),'0' ,to_date('"+livetime+"','yy-MM-dd  hh24:mi:ss'),to_number('"+liveday+"'),'"+staffid+"',to_number('"+depositnum+"'))";
            OracleCommand ocmd = myConnection.CreateCommand();
            ocmd.CommandText = allsql;
            myConnection.Open();
            ocmd.ExecuteNonQuery();
            if (ocmd.ExecuteNonQuery() > 0)
            {
                MessageBox.Show("执行成功!");
            }

            MessageBox.Show("失败");
            ocmd.Dispose();
            myConnection.Close();
            this.Close();
支持(0) 反对(0) Booneey | 园豆:177 (初学一级) | 2017-12-20 16:05

@87Super: 这是点击事件内的代码

支持(0) 反对(0) Booneey | 园豆:177 (初学一级) | 2017-12-20 16:06

@秦暮川: table_name (列1, 列2,...) VALUES (值1, 值2,....) 这样写 加上列名看看。

支持(0) 反对(0) 87Super | 园豆:107 (初学一级) | 2017-12-20 16:22

@秦暮川: 怎么样了

支持(0) 反对(0) 87Super | 园豆:107 (初学一级) | 2017-12-20 16:40

@87Super: 解决了,myConnection.Open(); ocmd.ExecuteNonQuery(); if (ocmd.ExecuteNonQuery() > 0) 这里执行了两次,应该

要拿执行结果第一个Execute完直接赋值给一个变量

谢谢了!!

支持(0) 反对(0) Booneey | 园豆:177 (初学一级) | 2017-12-20 16:46

@秦暮川: 

失误了,没注意看。

ocmd.ExecuteNonQuery(); if (ocmd.ExecuteNonQuery() > 0) { MessageBox.Show("执行成功!"); }

这里执行了两次SQL。

支持(0) 反对(0) 87Super | 园豆:107 (初学一级) | 2017-12-20 17:24
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册