2012-01-31 83 views
1

我試圖用下面的代碼插入數據庫,我試過了我能想到的所有內容,但是當我檢查數據庫中的表時沒有任何更改,我做錯了什麼?寫入數據庫的ADO.NET問題

P.S如下圖所示,數據庫的名稱是Stoichiometry.mdb,表的名稱是Molecules。

感謝

con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Stoichiometry.mdb"); 
    adMol = new OleDbDataAdapter(); 
    adMol.SelectCommand = new OleDbCommand("SELECT * FROM Molecules", con); 

    dsHistory = new DataSet(); 
    con.Open(); 
    adMol.Fill(dsHistory, "Molecules"); 
    con.Close(); 

    DataRow data = dsHistory.Tables["Molecules"].NewRow(); 
    data["Formula"] = formula_; 
    data["MolecularWeight"] = totalWeight_; 
    adMol.InsertCommand = new OleDbCommand("INSERT INTO Molecules (Formula," 
        + " MolecularWeight) " + "VALUES(@Formula, @MolecularWeight)", con); 


    adMol.InsertCommand.Parameters.Add("@Formula", OleDbType.VarChar, 250, "Formula"); 
    adMol.InsertCommand.Parameters["@Formula"].SourceVersion = DataRowVersion.Current; 
    adMol.InsertCommand.Parameters.Add("@MolecularWeight", OleDbType.Numeric, 20, "MolecularWeight"); 
    adMol.InsertCommand.Parameters["@MolecularWeight"].SourceVersion = DataRowVersion.Current; 

回答

3

加入這一行添加參數

adMol.InsertCommand.ExecuteNonQuery(); 

UPDATE

adMol.InsertCommand = new OleDbCommand("INSERT INTO Molecules (Formula," + " MolecularWeight) " + "VALUES(@Formula, @MolecularWeight)", con); 
adMol.InsertCommand.Parameters.Add("@Formula", OleDbType.VarChar, 250, "Formula"); 
adMol.InsertCommand.Parameters["@Formula"].SourceVersion = DataRowVersion.Current; 
adMol.InsertCommand.Parameters.Add("@MolecularWeight", OleDbType.Numeric, 20, "MolecularWeight"); 
adMol.InsertCommand.Parameters["@MolecularWeight"].SourceVersion = DataRowVersion.Current; 
adMol.InsertCommand.ExecuteNonQuery() 
+0

謝謝了!這工作。 – 2012-01-31 05:54:01