2011-11-27 80 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