从C#调用Oracle插入存储过程

时间:2014-07-07 14:31:18

标签: c# oracle stored-procedures

存储过程:

CREATE OR REPLACE PROCEDURE insertMovie(
   p_m_id IN MOVIE.M_ID%TYPE,
   p_movie_name IN MOVIE.MOVIE_NAME%TYPE,
   p_year IN MOVIE.YEAR%TYPE,
   p_category IN MOVIE.CATEGORY%TYPE)
IS
BEGIN
  INSERT INTO MOVIE ("M_ID", "MOVIE_NAME", "YEAR", "CATEGORY") 
  VALUES (p_m_id, p_movie_name,p_year, p_category);
END;
/

这是我创建的将数据插入movie表的过程。现在我试图用C#来调用它。我尝试了以下代码,但它无法正常工作。

在这方面的一点帮助将不胜感激。谢谢

    private void button1_Click(object sender, EventArgs e)
    {
        dbConnection conObj = new dbConnection();
        OracleConnection theConn = conObj.connFunc();

        String p_m_id, p_movie_name, p_year, p_category;
        p_m_id = movie_id.Text;
        p_movie_name = movie_name.Text;
        p_year = year.Text;
        p_category = category.Text;

        OracleParameter parChoiceIn = new OracleParameter();
        OracleParameter parDataIn = new OracleParameter();
        OracleParameter parOut = new OracleParameter();

        parChoiceIn.ParameterName = "p_m_id";
        parChoiceIn.OracleType = OracleType.Number;
        parChoiceIn.Size = 32;
        parChoiceIn.Direction = System.Data.ParameterDirection.Input;
        parChoiceIn.Value = p_m_id;

        parDataIn.ParameterName = "p_movie_name";
        parDataIn.OracleType = OracleType.VarChar;
        parDataIn.Size = 32;
        parDataIn.Direction = System.Data.ParameterDirection.Input;
        parDataIn.Value = p_movie_name;

        parDataIn.ParameterName = "p_year";
        parDataIn.OracleType = OracleType.Number;
        parDataIn.Size = 32;
        parDataIn.Direction = System.Data.ParameterDirection.Input;
        parDataIn.Value = p_year;

        parDataIn.ParameterName = "p_category";
        parDataIn.OracleType = OracleType.VarChar;
        parDataIn.Size = 32;
        parDataIn.Direction = System.Data.ParameterDirection.Input;
        parDataIn.Value = p_category;

        OracleCommand cmd = theConn.CreateCommand();
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        cmd.CommandText = "insertMovie";
        cmd.Parameters.Add(parChoiceIn);
        cmd.Parameters.Add(parDataIn);
        cmd.Parameters.Add(parOut);

        theConn.Open();

        OracleDataAdapter adapter = new OracleDataAdapter(cmd);
        DataSet myset = new DataSet("theResCur");
        adapter.Fill(myset);
        theGrid.DataSource = myset;
        theGrid.DataSource = myset.Tables[0];

        theConn.Close();
    }
}

1 个答案:

答案 0 :(得分:1)

您正在创建parOut参数,但从不对其进行初始化。

此外,您要重新定义parDataIn参数三次,这样做,OracleCommand只会收到最后定义的参数p_category

我不明白你的代码的最后部分是什么意思。如果你想简单地调用存储过程,也许你应该试试这个:

private void button1_Click(object sender, EventArgs e)
{
    dbConnection conObj = new dbConnection();
    OracleConnection theConn = conObj.connFunc();

    String p_m_id, p_movie_name, p_year, p_category;
    p_m_id = movie_id.Text;
    p_movie_name = movie_name.Text;
    p_year = year.Text;
    p_category = category.Text;

    theConn.Open();

    OracleCommand cmd = theConn.CreateCommand();
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    cmd.CommandText = "insertMovie";
    cmd.Connection = theConn;

    OracleParameter parChoiceIn = new OracleParameter();
    OracleParameter parDataIn = new OracleParameter();

    parChoiceIn.ParameterName = "p_m_id";
    parChoiceIn.OracleType = OracleType.Number;
    parChoiceIn.Size = 32;
    parChoiceIn.Direction = System.Data.ParameterDirection.Input;
    parChoiceIn.Value = p_m_id;
    cmd.Parameters.Add(parChoiceIn);

    parDataIn.ParameterName = "p_movie_name";
    parDataIn.OracleType = OracleType.VarChar;
    parDataIn.Size = 32;
    parDataIn.Direction = System.Data.ParameterDirection.Input;
    parDataIn.Value = p_movie_name;
    cmd.Parameters.Add(parDataIn);

    parDataIn = new OracleParameter();
    parDataIn.ParameterName = "p_year";
    parDataIn.OracleType = OracleType.Number;
    parDataIn.Size = 32;
    parDataIn.Direction = System.Data.ParameterDirection.Input;
    parDataIn.Value = p_year;
    cmd.Parameters.Add(parDataIn);

    parDataIn = new OracleParameter();
    parDataIn.ParameterName = "p_category";
    parDataIn.OracleType = OracleType.VarChar;
    parDataIn.Size = 32;
    parDataIn.Direction = System.Data.ParameterDirection.Input;
    parDataIn.Value = p_category;
    cmd.Parameters.Add(parDataIn);

    cmd.ExecuteNonQuery();

    theConn.Close();
}