使用 oledb 插入到oracle db
本文关键字:oracle db 插入 oledb 使用 | 更新日期: 2023-09-27 18:34:08
嗨,我是一名学生,我们的教授要求我们做一个基本的 CRUD 程序。现在,我已经用C#+ mysql做了很多CRUD程序,但是现在我们的教授要求我们使用oracle数据库(忘记了它是什么,比如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:并非所有变量都绑定"错误。
附言我正在使用MS Visual Studio 2013,如果有帮助的话。
首先,您需要创建一个如下所示的序列
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 + "')";
您需要传递带有 @ 前缀的参数,即@procedureID