使用oledb插入到oracle db

时间:2022-09-27 13:20:19

Hi I'm a student and our professor asked us to do a basic CRUD program. Now, I've done a lot of CRUD programs in C# + mysql, but now our professor asked us to use an oracle db, (forgot what it was. something like 10g XE or something) and said we should use oledb. I've tried to migrate my previous c# + mysql code but apparently it doesn't work. I've done a lot of searching which ultimately led me to this

嗨,我是学生,我们的教授要求我们做一个基本的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();
    }

it's giving me a "ORA-01008: not all variables bound" error when I try to insert data.

当我尝试插入数据时,它给了我一个“ORA-01008:并非所有变量绑定”错误。

P.S. I'm using MS Visual Studio 2013, if that helps.

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

2 个解决方案

#1


0  

First of all you need to create a sequence like below

首先,您需要创建如下所示的序列

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

Then your commend text will be below:

那么您的推荐文本如下:

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

#2


0  

You need to pass the parameters with @ prefix. i.e. @procedureID

您需要使用@前缀传递参数。即@procedureID

#1


0  

First of all you need to create a sequence like below

首先,您需要创建如下所示的序列

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

Then your commend text will be below:

那么您的推荐文本如下:

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

#2


0  

You need to pass the parameters with @ prefix. i.e. @procedureID

您需要使用@前缀传递参数。即@procedureID