2021-11-22 19:07:46 +01:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-11-24 12:28:45 +01:00
|
|
|
using System.Runtime.Serialization;
|
2021-11-23 18:38:08 +01:00
|
|
|
using MySqlConnector;
|
|
|
|
using de.hottis.genericdatabaseapiservice.Models;
|
2021-11-22 19:07:46 +01:00
|
|
|
|
|
|
|
namespace de.hottis.genericdatabaseapiservice.Services {
|
|
|
|
public interface IDbService {
|
2021-11-23 18:38:08 +01:00
|
|
|
void JustDoSomething<T>(string msg);
|
|
|
|
|
2021-11-22 19:07:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public class DbService : IDbService {
|
2021-11-23 18:38:08 +01:00
|
|
|
async public void JustDoSomething<T>(string msg) {
|
2021-11-22 19:07:46 +01:00
|
|
|
Console.WriteLine(msg);
|
2021-11-23 18:38:08 +01:00
|
|
|
|
|
|
|
var item = Activator.CreateInstance<T>();
|
|
|
|
|
|
|
|
using (var conn = new MySqlConnection("Server=172.16.10.18;User ID=apiservicetestdb;Password=geheim123;Database=apiservicetestdb")) {
|
|
|
|
await conn.OpenAsync();
|
|
|
|
|
|
|
|
using (var cmd = conn.CreateCommand()) {
|
|
|
|
// cmd.CommandText = "INSERT INTO test1 (txt, nr) VALUES (@txt, @nr)";
|
|
|
|
// cmd.Parameters.AddWithValue("txt", "txt1");
|
|
|
|
// cmd.Parameters.AddWithValue("nr", 12);
|
|
|
|
// await cmd.ExecuteNonQueryAsync();
|
|
|
|
cmd.CommandText = "SELECT id, txt, nr FROM test1";
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync()) {
|
|
|
|
while (await reader.ReadAsync()) {
|
|
|
|
foreach (var propertyInfo in typeof(T).GetProperties()) {
|
2021-11-24 12:28:45 +01:00
|
|
|
Console.WriteLine("Property name: {0}", propertyInfo);
|
|
|
|
var attributes = propertyInfo.GetCustomAttributes(typeof(DataMemberAttribute), true);
|
|
|
|
var dma = (DataMemberAttribute)attributes[0];
|
|
|
|
Console.WriteLine("DataMember name: {0} {1} ", dma.Name, dma.TypeId);
|
2021-11-23 18:38:08 +01:00
|
|
|
}
|
|
|
|
Console.WriteLine(reader.GetString("txt"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-22 19:07:46 +01:00
|
|
|
}
|
|
|
|
}
|
2021-11-23 18:38:08 +01:00
|
|
|
}
|