从DataGridView读取数据并插入到oracle数据库

时间:2017-01-23 16:08:15

标签: c# oracle c#-4.0 datagridview oracle11gr2

我正在尝试将DataGridView中的数据插入到oracle数据库中 对于一个记录一切正常,但当DataGridView中有多行时,我收到以下错误消息:

  

ora-06550第1行第15栏:

     

请参阅pls-00306错误的参数数量或类型   'F_INS_ORDER_DATA'

     

ora-06550第1行第7列:

     

pl / sql语句被忽略

我试过

  1. 检查参数数量,但由于成功插入一条记录,它们没问题

  2. 没有错误类型的参数,但它们没问题,因为一条记录已成功插入

  3. 我检查了参数是否顺序错误但是也可以,因为一条记录成功插入

  4. 代码:

    string connstr = @"Data Source=orcl; User Id=user; password=pwd;";
    
    string insertcmdtxt = @"F_INS_ORDER_DATA";   
    
    using (OracleConnection conn = new OracleConnection(connstr))
    using (OracleCommand cmd = new OracleCommand(insertcmdtxt, conn))
    {
        try
        {
            conn.Open();
    
            cmd.CommandType = CommandType.StoredProcedure;
    
            cmd.CommandText = insertcmdtxt;
    
            cmd.Parameters.Add(":vORDER_ID", OracleDbType.Int32, ParameterDirection.ReturnValue);
    
            foreach (DataGridViewRow Row in DGV_INVOICE.Rows)
            {
                cmd.Parameters.Add(new OracleParameter(":P_CUSTOMER_ID", OracleDbType.Int32)).Value     = TB_CUSTOMER_ID.Text;
                cmd.Parameters.Add(new OracleParameter(":P_ORDER_NOTE", OracleDbType.Varchar2)).Value   = TB_ORDER_NOTE.Text;
    
                cmd.Parameters.Add(new OracleParameter(":P_PRODUCT_ID", OracleDbType.Int32)).Value      = Row.Cells[DGV_INVOICE.Columns["DGV_PRODUCT_ID"].Index].Value;
                cmd.Parameters.Add(new OracleParameter(":P_UNIT_PRICE", OracleDbType.Int32)).Value      = Row.Cells[DGV_INVOICE.Columns["DGV_UNIT_PRICE"].Index].Value;
                cmd.Parameters.Add(new OracleParameter(":P_QUANTITY", OracleDbType.Int32)).Value        = Row.Cells[DGV_INVOICE.Columns["DGV_QUANTITY"].Index].Value;
                cmd.Parameters.Add(new OracleParameter(":P_DISCOUNT", OracleDbType.Int32)).Value        = Row.Cells[DGV_INVOICE.Columns["DGV_DISCOUNT"].Index].Value;
                cmd.Parameters.Add(new OracleParameter(":P_ORDER_STATUS", OracleDbType.Varchar2)).Value = '1';
                cmd.Parameters.Add(new OracleParameter(":P_ITEM_NOTE", OracleDbType.Varchar2)).Value    = Row.Cells[DGV_INVOICE.Columns["DGV_ITEM_NOTE"].Index].Value;
            }
    
            cmd.ExecuteNonQuery();
    
            TB_INVOICE_ID.Text = (cmd.Parameters[":vORDER_ID"].Value).ToString();
        }
        catch (Exception EX)
        {
            MessageBox.Show(EX.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return;
        }
    }
    }'
    

    代码更新

    在测试了以下朋友的建议之后

    现在我没有收到任何错误,但是如果数据库中有多行 我发现只插入一条记录

    string connstr = @"Data Source=orcl; User Id=user; password=pwd;";
                    string insertcmdtxt = @"F_INS_ORDER_DATA";  
    
                    using (OracleConnection conn = new OracleConnection(connstr))
                    using (OracleCommand cmd = new OracleCommand(insertcmdtxt, conn))
                    {
                        try
                        {
                            conn.Open();
    
                            cmd.CommandType = CommandType.StoredProcedure;
    
                            cmd.CommandText = insertcmdtxt;
    
                            foreach (DataGridViewRow Row in DGV_INVOICE.Rows)
                            {
                                cmd.Parameters.Clear();
    
                                cmd.Parameters.Add(":vORDER_ID", OracleDbType.Int32, ParameterDirection.ReturnValue);
    
                                cmd.Parameters.Add(new OracleParameter(":P_CUSTOMER_ID", OracleDbType.Int32)).Value     = TB_CUSTOMER_ID.Text;
                                cmd.Parameters.Add(new OracleParameter(":P_ORDER_NOTE", OracleDbType.Varchar2)).Value   = TB_ORDER_NOTE.Text;
    
                                cmd.Parameters.Add(new OracleParameter(":P_PRODUCT_ID", OracleDbType.Int32)).Value      = Row.Cells[DGV_INVOICE.Columns["DGV_PRODUCT_ID"].Index].Value;
                                cmd.Parameters.Add(new OracleParameter(":P_UNIT_PRICE", OracleDbType.Int32)).Value      = Row.Cells[DGV_INVOICE.Columns["DGV_UNIT_PRICE"].Index].Value;
                                cmd.Parameters.Add(new OracleParameter(":P_QUANTITY", OracleDbType.Int32)).Value        = Row.Cells[DGV_INVOICE.Columns["DGV_QUANTITY"].Index].Value;
                                cmd.Parameters.Add(new OracleParameter(":P_DISCOUNT", OracleDbType.Int32)).Value        = Row.Cells[DGV_INVOICE.Columns["DGV_DISCOUNT"].Index].Value;
                                cmd.Parameters.Add(new OracleParameter(":P_ORDER_STATUS", OracleDbType.Varchar2)).Value = '1';
                                cmd.Parameters.Add(new OracleParameter(":P_ITEM_NOTE", OracleDbType.Varchar2)).Value    = Row.Cells[DGV_INVOICE.Columns["DGV_ITEM_NOTE"].Index].Value;
                            }
    
                            cmd.ExecuteNonQuery();                        
    
                            TB_INVOICE_ID.Text = (cmd.Parameters[":vORDER_ID"].Value).ToString();
                        }
                        catch (Exception EX)
                        {
                            MessageBox.Show(EX.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            return;
                        }
                    }
    

0 个答案:

没有答案