· 10 years ago · Sep 08, 2016, 06:58 AM
1/*
2 Benutzer: Susanne Zifle
3 Datum: 30.08.2016
4
5 */
6using System;
7using System.Data.SQLite;
8using System.Collections.Generic;
9using System.Data.Sql;
10using System.Data.SqlClient;
11using System.Data.SqlTypes;
12
13namespace dbanbindungv2
14{
15 class CDatabase
16 {
17 public void Connect(){
18 }
19
20 public void Create(){
21 }
22
23 public void Insert(string[] daten){
24 }
25
26 public void Read(string[] daten){
27 }
28
29 public void Disconnent(){
30 }
31 }
32
33 class CDatabaseSQLite : CDatabase
34 {
35 SQLiteConnection connection;
36 SQLiteCommand command;
37 SQLiteDataReader reader;
38
39 public void Connect (){
40 string dataSource = "SQLiteDemo.db";
41 connection = new SQLiteConnection();
42 connection.ConnectionString = "Data Source=" + dataSource;
43 connection.Open();
44 Console.WriteLine("Verbindung hergestellt");
45 }
46
47 public void Create(string tablename, string[] columns){
48 command = new SQLiteCommand(connection);
49 command.CommandText = "CREATE TABLE IF NOT EXISTS ('"+ tablename + "')";
50 }
51
52 public void Insert(string[] daten){
53 command = new SQLiteCommand(connection);
54 command.CommandText = "INSERT INTO beispiel (name, ort) VALUES('"+daten[0].ToString()+"', '"+daten[1].ToString()+"')";
55 command.ExecuteNonQuery();
56 }
57
58 public void Read(string[] daten){
59
60 command = new SQLiteCommand(connection);
61
62 command.CommandText = "select * from beispiel order by id desc";
63
64 reader = command.ExecuteReader();
65
66 while (reader.Read())
67 {
68 Console.WriteLine(reader[0].ToString(), reader[1].ToString(), reader[2].ToString());
69 }
70 Console.ReadKey();
71 reader.Close();
72 reader.Dispose();
73
74 command.Dispose();
75 }
76
77 public void Disconnent(){
78
79 connection.Close();
80 connection.Dispose();
81 }
82 }
83 class Program
84 {
85 public static void Main(string[] args)
86 {
87 CDatabaseSQLite database = new CDatabaseSQLite();
88 Console.WriteLine("Datenbank wählen. 'l' für SQlite"); //Auswahl des Datenbanksystems
89
90 string wahl = Console.ReadLine();
91
92 if (wahl == "l"){
93 database.Connect();
94
95 Console.WriteLine("Tabelle erstellen oder Tabelle wählen?"); //Tabellenerstellung oder Wahl
96 string tabelleEingabe = Console.ReadLine();
97
98 if (tabelleEingabe == "erstellen"){ //Tabelle erstellen
99 Console.WriteLine("Tabellenname eingeben:");
100 string tablename = Console.ReadLine();
101 Console.WriteLine("Tabellenspalten eingeben:");
102 string columnnames = Console.ReadLine();
103
104 string[] columnnamesSplit = columnnames.Split(',');
105 foreach (string columns in columnnamesSplit)
106 {
107 Console.WriteLine(columns);
108 }
109 database.Create(tablename);
110 }
111
112 if(tabelleEingabe == "wählen"){
113 Console.WriteLine("Tabellenname eingeben:");
114 string tablename = Console.ReadLine();
115 }
116
117 string[] daten = new string[5];
118 /*
119 Console.WriteLine("Name eingeben");
120 string name = Console.ReadLine();
121 daten[0] = name;
122
123 Console.WriteLine("Ort eingeben");
124 string ort = Console.ReadLine();
125 daten[1] = ort;
126
127 database.Insert(daten);
128 database.Read(daten);
129 */
130 database.Disconnent();
131
132 }
133 }
134 }
135}