使用C#通过存储过程将数据插入Oracle

时间:2018-02-02 06:29:35

标签: c# asp.net-mvc oracle stored-procedures

我正在尝试使用OracleDataAdapter将数据插入Oracle数据库,我正在使用存储过程。我没有收到任何错误,数据也没有提交到DB。

public DeviceType AddDeviceType(DeviceType deviceType)
        {
            DeviceType objDeviceType = new DeviceType();
            using (cmd = new OracleCommand("SP_DMS_DEVICE_TYPE_INSERT", con))
            {
                try
                {
                    cmd.CommandType = CommandType.StoredProcedure;                    
                    //cmd.Parameters.Add("p_typename", OracleType.VarChar).Direction = ParameterDirection.Input;
                    cmd.Parameters.AddWithValue("p_typename", deviceType.DeviceTypeName).Direction = ParameterDirection.Input;
                    cmd.Parameters.AddWithValue("p_createdby", deviceType.CreatedBy).Direction = ParameterDirection.Input;
                    cmd.Parameters.AddWithValue("p_createdon", deviceType.CreatedOn).Direction = ParameterDirection.Input;
                    cmd.Parameters.AddWithValue("p_updatedby", deviceType.UpdatedBy).Direction = ParameterDirection.Input;
                    cmd.Parameters.AddWithValue("p_updatedon", deviceType.UpdatedOn).Direction = ParameterDirection.Input;

                    con.Open();
                    adap = new OracleDataAdapter();
                    adap.InsertCommand = cmd;                    
                }
                catch (Exception)
                {
                    con.Close();
                }
                return objDeviceType;
            }
        }

存储过程

CREATE OR REPLACE PROCEDURE DEV_INV.SP_DMS_DEVICE_TYPE_INSERT
(
   //--p_id IN DMS_DEVICE_TYPE.ID%TYPE,
   p_typename IN DMS_DEVICE_TYPE.DEVICE_TYPE_NAME%Type,
   p_createdby IN DMS_DEVICE_TYPE.CREATED_BY%Type,
   p_createdon IN DMS_DEVICE_TYPE.CREATED_ON%TYPE,
   p_updatedby IN DMS_DEVICE_TYPE.UPDATED_BY%Type,
   p_updatedon IN DMS_DEVICE_TYPE.UPDATED_ON%Type
)
AS

   BEGIN
     INSERT INTO DMS_DEVICE_TYPE ("ID","DEVICE_TYPE_NAME","CREATED_BY","CREATED_ON","UPDATED_BY","UPDATED_ON")
     VALUES (DMS_DEVICE_TYPE_S.NextVal, p_typename, p_createdby, p_createdon, p_updatedby, p_updatedon);
     COMMIT;
   END SP_DMS_DEVICE_TYPE_INSERT;

2 个答案:

答案 0 :(得分:1)

在您的情况下,您不需要使用OracleDataAdapter。您所要做的就是调用execute方法。

// other codes here

con.Open();
int recordsAffected = cmd.ExecuteNonQuery();

答案 1 :(得分:0)

您可以调用cmd.ExecuteNonQuery()方法来执行存储过程。所以替换这个:

con.Open();
adap = new OracleDataAdapter();
adap.InsertCommand = cmd;  

有了这个

con.Open();
cmd.ExecuteNonQuery();