【问题标题】:Insert to oracle db using oledb使用 oledb 插入到 oracle db
【发布时间】:2014-12-12 19:00:19
【问题描述】:

您好,我是一名学生,我们的教授要求我们做一个基本的 CRUD 程序。现在,我在 C# + mysql 中做了很多 CRUD 程序,但现在我们的教授让我们使用 oracle db,(忘记它是什么。像 10g XE 之类的)并说我们应该使用 oledb。我试图迁移我以前的 c# + mysql 代码,但显然它不起作用。我做了很多搜索,最终导致我这样做

private void button2_Click(object sender, EventArgs e)
    {
        string commandText = "insert into Procedures (procedureID,petID,visitDate,procedureType) values(:procedureID,:petID,:visitDate,:procedureType)";
        con = new OleDbConnection(connectionstring);    
        OleDbCommand cmd = new OleDbCommand(commandText, con);
        con.Open();

        dtpDate.Format = DateTimePickerFormat.Custom;
        dtpDate.CustomFormat = "dd-MMM-yy";

        cmd.Parameters.AddWithValue("procedureID", null);
        //i think this line above is the error. correct me if im wrong.
        //but how do you pass a null value to a foreign key in oracle?
        //in mysql i'd just set my primary key to autoincrement and I would
        //just pass a null value and it would automatically set a unique ID
        //but in oracle i cannot find autoincrement and i cannot set my primary
        //key to nullable. is this the error or did I do something else wrong?
        //is oracle's primary key set to autoincrement by default?

        cmd.Parameters.AddWithValue("petID", tbName.Text);
        cmd.Parameters.AddWithValue("visitDate", dtpDate.Text);
        cmd.Parameters.AddWithValue("procedureType", tbProcedure.Text);
        cmd.ExecuteNonQuery();

        OleDbDataAdapter da = new OleDbDataAdapter("select * from Procedures", con);
        DataSet ds = new DataSet();
        da.Fill(ds, "Procedures");
        dgvTable.DataSource = ds.Tables[0];
        con.Dispose();
    }

当我尝试插入数据时,它给了我一个“ORA-01008: not all variables bound”错误。

附:如果有帮助,我正在使用 MS Visual Studio 2013。

【问题讨论】:

    标签: c# mysql database oracle visual-studio


    【解决方案1】:

    首先你需要创建一个如下所示的序列

    CREATE SEQUENCE procedures_seq
       MINVALUE 1
       START WITH 1
       INCREMENT BY 1
       CACHE 20;
    

    那么你的推荐文字会在下面:

    string commandText = "insert into Procedures (procedureID,petID,visitDate,procedureType) values(procedures_seq.NEXTVAL,'" + tbName.Text + "','" + dtpDate.Text + "','" + tbProcedure.Text + "')";
    

    【讨论】:

    • @Demzo:如果有帮助请告诉我
    • @TGMCians:一个问题可能有多个答案,所以我在没有更改原始答案的情况下发布了它。至少这个论坛允许我发布多个答案。为什么我被否决了?至少我正在帮助他克服他的问题,没有其他人。真的投反对票让我失去了动力。
    • 嗯,别担心。我把它颠倒了:)
    【解决方案2】:

    你需要传递带有@前缀的参数。即@procedureID

    【讨论】:

    • 它给了我“ORA-00936 缺少表达式”
    • 在 oracle 中,您需要创建 Sequence 来设置一个自动递增的值。请参阅此以获取帮助techonthenet.com/oracle/sequences.php
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 2019-05-02
    相关资源
    最近更新 更多