Autor Beitrag
Das S
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 101

Win XP
D7 Prof
BeitragVerfasst: Mo 22.08.05 07:10 
Hi,

ich beschäftige mich jetzt seit einigen Tagen mit delphi 2005 und ich kriege es einfach nicht geregelt, mit einer oledbconnection einen Datensatz in eine Datenbank zu schreiben. Kann mir hier jemand mal ein Beispiel posten, daß ich zumindest mal die grobe Richtung habe.

Schöne Grüße

Das S
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Mo 22.08.05 07:34 
Hallo!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
//Datenmenge öffnen
Adodataset.Commandtext:='select * from testtabelle';
AdoDataset.Active:=True;

//Daten anhängen
AdoDataset.Append;
AdoDataset.FieldByName('ID').AsInteger:=1;
AdoDataset.FieldByName('NAME').AsString:='Testdatensatz';
AdoDataset.Post;


Cu,
Udontknow
Das S Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 101

Win XP
D7 Prof
BeitragVerfasst: Mo 22.08.05 07:42 
Ähhhhhhheeeeeeeeeeeemmmmmmmmmmmm, ich meinte eigentlich mit Hilfe des oledbconnection, oledbadapter und mit einem dataset. Und mit Delphi 2005.NET :D
Udontknow
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2596

Win7
D2006 WIN32, .NET (C#)
BeitragVerfasst: Mo 22.08.05 08:01 
Oh, tatsächlich, ist ja die .NET-Sparte... :oops:
Marauder
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 72



BeitragVerfasst: Mo 22.08.05 17:46 
gucksch du :

Damit befüllst du eine listview mit dem SQL-blabla, ist praktisch exakt das gleiche wie mit dem oledb, einfach
den präfix sql in oledb abändern...

ausblenden volle Höhe Delphi-Quelltext
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:
Procedure GetDR (var dr:SQLdatareader;SQL:String;var conn : SQLconnection);
var
    cmd : SQLCommand;
begin
    conn := SQLConnection.create('Server=icecube;Initial Catalog=Northwind;Integrated Security=true');
    try
      cmd := SQLCommand.Create(SQL,conn);
      conn.open();
      dr :=cmd.ExecuteReader ();
    except
      on ex: Exception do messagebox.show (ex.message,'Fehler bei Verbindungsaufbau !');
    end;
end;

Procedure LoadListView (lView : ListView;SQL :String);
var    item1: ListViewItem;
        SubItem: ListViewItem.ListViewSubItem ;
        a : integer;
        lvwCol : ColumnHeader;
        dr : SQLDatareader;
        cn : SQLConnection;
  begin
     LView.Clear;
     LView.View := View.Details;
     LView.FullRowSelect := True;

     getDR (dr,SQL,cn);

     if dr.read = True then
     begin
          with LView.Columns do begin
             for a := 0 to dr.FieldCount -1 do
                 begin
                     lvwCol := ColumnHeader.Create;
                     lvwCol.Text := dr.GetName(a).ToString;
                     Add  (lvwCol);
                 end;
          end;
     end;
     
     getDR (dr,SQL,cn);

        while dr.read do
           begin
               item1 := ListViewItem.Create(dr.GetValue(0).toString.trim, 0);
               for a := 1 to dr.FieldCount -1 do
                   begin
                      SubItem := item1.SubItems.Add(dr.GetValue(a).toString.trim);
                   end;
                   LView.Items.Add(item1);
           end;
      for a := 0 to LView.Columns.Count -1 do
      begin
            try
              LView.Columns[a].Width := -2;
            except
            end;
      end;
     dr.close;
     cn.close;
 end;

procedure TWinForm.TWinForm_Load(sender: System.Object; e: System.EventArgs);
 Begin
    LoadListView (ListView1,'Select * from Shippers');
 end;


kannste übrigens ab oktober in meinem buch nachlesen... :wink:

den server solltest du natürlich ändern und die authentifizierungsmethode:
conn := SQLConnection.create('Server=icecube;Initial Catalog=Northwind;Integrated Security=true');

um sätze zu ändern nimmst du sowas:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
Function ExecuteSQL (SQL:String):Integer;
  var   cmd : SQLCommand;
        cn : SQLConnection;
  begin
    //messagebox.Show(sql); 
    cn := SQLConnection.create(connSTR);
    cn.open();
    Result := 0;
    try
      cmd  := SQLCommand.Create ();
      cmd.CommandText := SQL;
      cmd.Connection :=cn;
      Result := cmd.ExecuteNonQuery();
    except
      // Fehler
      on ex: Exception do messagebox.show (ex.message+chr(13)+chr(10)+SQL,'Fehler bei Execute !');
    end;
    cn.close ();
    cmd.Free ;

  end;


da kannst du nun einfach einen sqlbefehl übergeben:
ausblenden Delphi-Quelltext
1:
ExecuteSQL ('update table set x=y where id=1');					

zurückliefern tut dir die funktion die anzahl der geänderten...

hope it helps