日期:2014-05-18  浏览次数:20463 次

往oracle插入数据时的问题
提交textbox中的内容到数据库,下面是提交button的代码
protected   void   Button2_Click(object   sender,   EventArgs   e)
        {
                OracleConnection   conn   =   new   OracleConnection( "Data   Source=caora;User   ID=gufei;Password=gufei; ");
                //在此构造你所需的Sql语句。
                String   strSql   =   "Insert   into   GUFEI.PRODUCTION(PRODUCTNO,   PRODCODE,PUNIT,PRODNAME,PQUANTITY)   values( ' "   +   TextBox1.Text   +   " ', ' "   +   TextBox2.Text   +   " ', ' "   +   TextBox3.Text   +   " ', ' "   +   TextBox4.Text   +   " ', ' "   +   TextBox5.Text   +   " ') ";
                OracleCommand   cmd   =   new   OracleCommand(strSql,   conn);
                cmd.Connection.Open();
                //通过SqlCommand对象执行Sql命令语句即可。      
                cmd.ExecuteNonQuery();
                //cmd.Connection.Close();
点击提交后报错ORA-01400:   无法将   NULL   插入   ( "GUFEI ". "PRODUCTION ". "PRODUCTION_ID ")
PRODUCTION_ID是PK我知道不能为空,但是我textbox中的内容没它,我该怎么做呢

------解决方案--------------------
那你这个 PRODUCTION_ID 的值从哪里来呢?是个序列吗?

还有,楼主,你上一个帖子还没结
------解决方案--------------------
PRODUCTION_ID你要设置为自增字段,要不就PK为空
------解决方案--------------------
String strSql = "Insert into GUFEI.PRODUCTION(PRODUCTION_ID, PRODUCTNO, PRODCODE,PUNIT,PRODNAME,PQUANTITY) values(coalesce(production_id,0)+1, ' " + TextBox1.Text + " ', ' " + TextBox2.Text + " ', ' " + TextBox3.Text + " ', ' " + TextBox4.Text + " ', ' " + TextBox5.Text + " ') ";
------解决方案--------------------
写法错误

String strSql = "Insert into GUFEI.PRODUCTION(PRODUCTION_ID,UNITBASIC_ID, PRODUCTNO, PRODCODE,PUNIT,PRODNAME,PQUANTITY) values(nvl((select max(PRODUCTION_ID) from GUFEI.PRODUCTION), 1), nvl((select max(UNITBASIC_ID) from GUFEI.PRODUCTION), 1), ' " + TextBox1.Text + " ', ' " + TextBox2.Text + " ', ' " + TextBox3.Text + " ', ' " + TextBox4.Text + " ', ' " + TextBox5.Text + " ') ";