在C#中更新Access 2010数据库

时间:2014-11-13 14:04:18

标签: c# parameters ms-access-2010

我为此疯狂, 我正在尝试使用c#

保存Access 2010 .accdb数据库中表的更改

我有这个方法:

using (OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + frmMain.dbPath + "; Persist Security Info=False;"))
{
    conn.Open();

    using (OleDbTransaction tx = conn.BeginTransaction())
    {
        using (OleDbCommand cmd = new OleDbCommand(
                "UPDATE Ricette SET " +
                    "Ricette.ResinaUreicaTotale = @ResinaUreicaTotale, " +
                    "Ricette.ResinaUreicaParziale = @ResinaUreicaParziale, " +
                    "Ricette.ResinaMelaminicaTotale = @ResinaMelaminicaTotale, " +
                    "Ricette.ResinaMelaminicaParziale = @ResinaMelaminicaParziale, " +
                    "Ricette.AcquaTotale = @AcquaTotale, " +
                    "Ricette.AcquaParziale = @AcquaParziale, " +
                    "Ricette.FarinettaTotale = @FarinettaTotale, " +
                    "Ricette.FarinaCoccoTotale = @FarinaCoccoTotale, " +
                    "Ricette.ComponenteATotale = @ComponenteATotale, " +
                    "Ricette.TempoMix = @TempoMix " +
                "WHERE Ricette.[Nome] = [@Nome];", conn))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Transaction = tx;
            using (OleDbDataAdapter ad = new OleDbDataAdapter(cmd))
            {
                try
                {
                    DataRow row = ds.Tables[0].Rows.Find(cmbIncollaggio.SelectedItem.ToString());
                    string[] ric = new string[1];

                    switch (cmbIncollaggio.SelectedItem.ToString())
                    {
                        case "IF20":
                            cmd.Parameters.Add("@Nome", OleDbType.VarChar, 100, "Nome").Value = "IF20";

                            cmd.Parameters.Add("@ResinaUreicaTotale", OleDbType.VarChar, 100, "ResinaUreicaTotale").Value = RUTEdit.Text;
                            frmMain.seriale.DataWrite("00WD", RUTEdit.Text, 2100);
                            ric = frmMain.seriale.DataRead("00RD", 2100, 1);
                            RUTLabel.Text = Convert.ToInt32(ric[0]).ToString();

                            cmd.Parameters.Add("@ResinaUreicaParziale", OleDbType.VarChar, 100, "ResinaUreicaParziale").Value = RUPEdit.Text;
                            frmMain.seriale.DataWrite("00WD", RUPEdit.Text, 2101);
                            ric = frmMain.seriale.DataRead("00RD", 2101, 1);
                            RUPLabel.Text = Convert.ToInt32(ric[0]).ToString();

                            cmd.Parameters.Add("@ResinaMelaminicaTotale", OleDbType.VarChar, 100, "ResinaMelaminicaTotale").Value = RMTEdit.Text;
                            frmMain.seriale.DataWrite("00WD", RMTEdit.Text, 2102);
                            ric = frmMain.seriale.DataRead("00RD", 2102, 1);
                            RMTLabel.Text = Convert.ToInt32(ric[0]).ToString();

                            cmd.Parameters.Add("@ResinaMelaminicaParziale", OleDbType.VarChar, 100, "ResinaMelaminicaParziale").Value = RMPEdit.Text;
                            frmMain.seriale.DataWrite("00WD", RMPEdit.Text, 2103);
                            ric = frmMain.seriale.DataRead("00RD", 2103, 1);
                            RMPLabel.Text = Convert.ToInt32(ric[0]).ToString();

                            cmd.Parameters.Add("@AcquaTotale", OleDbType.VarChar, 100, "AcquaTotale").Value = ATEdit.Text;
                            frmMain.seriale.DataWrite("00WD", ATEdit.Text, 2104);
                            ric = frmMain.seriale.DataRead("00RD", 2104, 1);
                            ATLabel.Text = Convert.ToInt32(ric[0]).ToString();

                            cmd.Parameters.Add("@AcquaParziale", OleDbType.VarChar, 100, "AcquaParziale").Value = APEdit.Text;
                            frmMain.seriale.DataWrite("00WD", APEdit.Text, 2105);
                            ric = frmMain.seriale.DataRead("00RD", 2105, 1);
                            APLabel.Text = Convert.ToInt32(ric[0]).ToString();

                            cmd.Parameters.Add("@FarinettaTotale", OleDbType.VarChar, 100, "FarinettaTotale").Value = FTEdit.Text;
                            frmMain.seriale.DataWrite("00WD", FTEdit.Text, 2106);
                            ric = frmMain.seriale.DataRead("00RD", 2106, 1);
                            FTLabel.Text = string.Format("{0,0}", Convert.ToInt32(ric[0]) / 10);

                            cmd.Parameters.Add("@FarinaCoccoTotale", OleDbType.VarChar, 100, "FarinaCoccoTotale").Value = FCTEdit.Text;
                            frmMain.seriale.DataWrite("00WD", FCTEdit.Text, 2107);
                            ric = frmMain.seriale.DataRead("00RD", 2107, 1);
                            FCTLabel.Text = string.Format("{0,0}", Convert.ToInt32(ric[0]) / 10);

                            cmd.Parameters.Add("@ComponenteATotale", OleDbType.VarChar, 100, "ComponenteATotale").Value = CATEdit.Text;
                            frmMain.seriale.DataWrite("00WD", CATEdit.Text, 2108);
                            ric = frmMain.seriale.DataRead("00RD", 2108, 1);
                            CATLabel.Text = string.Format("{0,0}", Convert.ToInt32(ric[0]) / 10);

                            cmd.Parameters.Add("@TempoMix", OleDbType.VarChar, 100, "TempoMix").Value = TMEdit.Text;
                            frmMain.seriale.DataWrite("00WD", TMEdit.Text, 2109);
                            ric = frmMain.seriale.DataRead("00RD", 2109, 1);
                            TMLabel.Text = Convert.ToInt32(ric[0]).ToString();
                            break;
                    }

                    ad.UpdateCommand = cmd;
                    ds.AcceptChanges();
                    ad.Update(ds);
                    tx.Commit();

                }
                catch (Exception ex)
                {
                    tx.Rollback();
                    MessageBox.Show("Salvataggio non riuscito.\n" + ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }
    }

    conn.Close();
}

此代码不起作用...它在数据集中运行良好,但更改不会复制到原始数据库中... 你能找到任何错误吗? 谢谢你的帮助......

1 个答案:

答案 0 :(得分:0)

解决!!!

添加

cmd.ExecuteNonQuery(); 

并删除DataAdapter对象。