2011-11-27 63 views
1

我写了一些代码,从一个表中取出一些值,并插入其他表中的值(不仅仅是这些值,还包括这些值(这个值=基于上表))基于另一个表插入数据到表#C#

,我得到这个错误:

System.Data.OleDb.OleDbException (0x80040E10): value wan't given for one or more of the required parameters.`

这里的代码。我不知道我错过了什么。

string selectedItem = comboBox1.SelectedItem.ToString(); 
Codons cdn = new Codons(selectedItem); 
string codon1; 
int index; 

if (this.i != this.counter) 
{ 
    //take from the DataBase the matching codonsCodon1 to codonsFullName 
    codon1 = cdn.GetCodon1(); 

    //take the serialnumber of the last protein 
    string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;" + 
     "Data Source=C:\\Projects_2012\\Project_Noam\\Access\\myProject.accdb"; 
    OleDbConnection conn = new OleDbConnection(connectionString); 
    conn.Open(); 
    string last= "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = "+this.name ; 
    OleDbCommand getSerial = new OleDbCommand(last, conn); 
    OleDbDataReader dr = getSerial.ExecuteReader(); 
    dr.Read(); 
    index = dr.GetInt32(0); 

    //add the amino acid to tblOrderAA 
    using (OleDbConnection connection = new OleDbConnection(connectionString)) 
    { 
    string insertCommand = "INSERT INTO tblOrderAA(orderAASerialPro, orderAACodon1) " 
      + " values (?, ?)"; 
    using (OleDbCommand command = new OleDbCommand(insertCommand, connection)) 
    { 
     connection.Open(); 
     command.Parameters.AddWithValue("orderAASerialPro", index); 
     command.Parameters.AddWithValue("orderAACodon1", codon1); 
     command.ExecuteNonQuery(); 
    } 
    } 
} 

编辑:我把一个消息那行之后:

index = dr.GetInt32(0); 

,看看问题出在哪里了,我之前得到的错误。我没有看到在MessageBox

回答

0

该代码例如,从here

string SqlString = "Insert Into Contacts (FirstName, LastName) Values (?,?)"; 

using (OleDbConnection conn = new OleDbConnection(ConnString)) 
{ 
    using (OleDbCommand cmd = new OleDbCommand(SqlString, conn)) 
    { 
     cmd.CommandType = CommandType.Text; 
     cmd.Parameters.AddWithValue("FirstName", txtFirstName.Text); 
     cmd.Parameters.AddWithValue("LastName", txtLastName.Text); 
     conn.Open(); 
     cmd.ExecuteNonQuery(); 
    } 
} 

尝试做相同的例子。

1

您的SELECT命令有一个语法错误,因为您没有用引号括起来。

更改此:

string last = "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = "+this.name ; 
OleDbCommand getSerial = new OleDbCommand(last, conn); 
OleDbDataReader dr = getSerial.ExecuteReader(); 

string last = "SELECT proInfoSerialNum FROM tblProInfo WHERE proInfoScienceName = ?"; 
OleDbCommand getSerial = new OleDbCommand(last, conn); 
getSerial.Parameters.AddWithValue("?", this.name); 
OleDbDataReader dr = getSerial.ExecuteReader(); 
+0

钢不起作用。 – user1017315

+0

@ user1017315我复制了你的代码,那就是错误发生的地方。代码中的哪一行实际上会产生错误? – LarsTech

+0

**请参阅我的编辑** – user1017315

相关问题