[C#/Ado.net]

Pagina: 1
Acties:
  • 80 views sinds 30-01-2008

  • Denhomer
  • Registratie: Augustus 2000
  • Laatst online: 12-10-2025
Ik ben bezig met een projectje waar bepaalde rijen eerst uit de database gehaald worden, waarna ze geedit kunnen worden.
De database is gewoon een access bestand

Het probleem is dat in de updatePizza een DBConcurrencyException geworpen wordt, terwijl er niemand anders met de database verbonden is (proggie draait slechts 1 maal, access is gesloten, server explorer is disconnected)

Er bestaat een Dataset met schema die zorgt voor de strong typing in de dataset
Dit is de klasse die zorgt voor de databasetoegang

C#:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Diagnostics;
using PizzaShopClassLibrary;

namespace PizzaShopDatabase
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    public class dbPizza
    {
        private OleDbConnection dbconn;
        private OleDbDataAdapter myOleDbDataAdapter;
        private DatabaseDS myDS;
        public dbPizza()
        {
            try 
            {
                dbconn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=../../../data.mdb;User Id=admin;Password=;");
                dbconn.Open();
            } 
            catch (OleDbException e) 
            {
                //Databaseconnectie is mislukt
                Debug.Write(e.ToString());
            }
        }
        public bool isConnected()
        {
            return dbconn.State == ConnectionState.Open;
        }
        public ArrayList getPizzas() 
        {
            ArrayList result = new ArrayList();
            OleDbCommand mySelectCommand = new OleDbCommand("Select * from T_PIZZA", dbconn);
            myOleDbDataAdapter = new OleDbDataAdapter(mySelectCommand);
            myDS = new DatabaseDS();
            myOleDbDataAdapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;

            OleDbParameter workParam = null;

            myOleDbDataAdapter.UpdateCommand = new OleDbCommand("Update T_PIZZA Set name = @name, ingredients = @ingredients, price = @price WHERE id = @id" , dbconn);

            workParam = myOleDbDataAdapter.UpdateCommand.Parameters.Add("@id", OleDbType.Integer);
            workParam.SourceColumn = "id";
            workParam.SourceVersion = DataRowVersion.Original;
            workParam = myOleDbDataAdapter.UpdateCommand.Parameters.Add("@name", OleDbType.VarChar);
            workParam.SourceColumn = "name";
            workParam.SourceVersion = DataRowVersion.Current;
            workParam = myOleDbDataAdapter.UpdateCommand.Parameters.Add("@ingredients", OleDbType.VarChar);
            workParam.SourceColumn = "ingredients";
            workParam.SourceVersion = DataRowVersion.Current;
            workParam = myOleDbDataAdapter.UpdateCommand.Parameters.Add("@price", OleDbType.Double);
            workParam.SourceColumn = "price";
            workParam.SourceVersion = DataRowVersion.Current;

            try
            {
                myOleDbDataAdapter.Fill(myDS, "pizza");
                foreach (DatabaseDS.pizzaRow myDataRow in myDS.pizza) 
                {
                    Pizza myPizza = new Pizza(myDataRow.id, myDataRow.name, myDataRow.price, myDataRow.ingredients);
                    result.Add(myPizza);
                }
                
            }
            catch(Exception e)
            {
                Console.Write(e.ToString());
            }
            return result;
        }
        public void updatePizza(Pizza pizza)
        {
            DatabaseDS.pizzaRow myPizzaRow = (DatabaseDS.pizzaRow)myDS.pizza.Rows.Find(pizza.id);
            myPizzaRow.BeginEdit();
            myPizzaRow.name = pizza.naam;
            myPizzaRow.ingredients = pizza.ingredienten;
            myPizzaRow.price = pizza.prijs;
            myPizzaRow.EndEdit();
            myOleDbDataAdapter.Update(myDS, "pizza");
        }
    }
}

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 15:56

.oisyn

Moderator Devschuur®

Demotivational Speaker

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Dit topic is gesloten.