ADO.NET写入数据库时出现问题

本文关键字:问题 数据库 NET ADO | 更新日期: 2023-09-27 18:21:52

我正试图用下面的代码插入数据库,我已经尝试了我能想到的一切,但当我检查数据库中的表时,没有任何变化,我做错了什么?

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;

ADO.NET写入数据库时出现问题

添加参数后添加此行

adMol.InsertCommand.ExecuteNonQuery();

更新

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()