C# 使用oledb插入到oracle数据库
嗨,我是一名学生,我们的教授让我们做一个基本的CRUD程序。现在,我已经用C#+mysql做了很多CRUD程序,但是现在我们的教授让我们使用oracle db(忘记它是什么了,比如10g XE之类的),并说我们应该使用oledb。我曾尝试迁移我以前的c#+mysql代码,但显然不起作用。我做了很多搜索,最终找到了这个C# 使用oledb插入到oracle数据库,c#,mysql,database,oracle,visual-studio,C#,Mysql,Database,Oracle,Visual Studio,嗨,我是一名学生,我们的教授让我们做一个基本的CRUD程序。现在,我已经用C#+mysql做了很多CRUD程序,但是现在我们的教授让我们使用oracle db(忘记它是什么了,比如10g XE之类的),并说我们应该使用oledb。我曾尝试迁移我以前的c#+mysql代码,但显然不起作用。我做了很多搜索,最终找到了这个 private void button2_Click(object sender, EventArgs e) { string commandText =
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。您需要使用@prefix传递参数。i、 e.@procedureID首先,您需要创建一个如下所示的序列
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 + "')";
在oracle中,它给了我“ORA-00936缺少表达式”,您需要创建序列来设置一个自动递增的值。看这个寻求帮助@Demzo:如果有,请告诉我helps@TGMCians:一个问题可能有多个asnwers,因此我发布了它,但没有更改原来的asnwers。至少这个论坛允许我发布多个答案。为什么我会被呕吐?至少我正在帮助他克服他的问题,而不是其他人。真的投票否决了我,别担心。我倒过来:)