Programm_Wirl/AA-Klassen/WProgramm.cs
2025-11-30 11:51:28 +01:00

71 lines
2.3 KiB
C#

using Npgsql;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
namespace DatenDB
{
public class WProgramm
{
private static string TABLE = "kundenverwaltung.wprogramm";
private static string COLUMNS = "wprogramm_id, nummer, bezeichnung";
public WProgramm()
{
}
public static List<WProgramm> GetList()
{
DatenbankConnection.GetConnection().Open();
List<WProgramm> resultList = new List<WProgramm>();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
command.CommandText = $"select {COLUMNS} from {TABLE} order by nummer";
NpgsqlDataReader reader = command.ExecuteReader();
while (reader.Read()) resultList.Add(new WProgramm(reader));
reader.Close();
DatenbankConnection.GetConnection().Close();
return resultList;
}
public WProgramm(NpgsqlDataReader reader)
{
this.WprogrammID = reader.GetInt16(0);
this.Nummer = reader.GetInt16(1);
this.Bezeichnung = reader.IsDBNull(2) ? string.Empty : reader.GetString(2);
}
public int Save()
{
DatenbankConnection.GetConnection().Open();
NpgsqlCommand command = new NpgsqlCommand();
command.Connection = DatenbankConnection.GetConnection();
if (this.WprogrammID.HasValue & this.WprogrammID != 0)
{
command.CommandText = $"update {TABLE} set nummer = :p1, bezeichnung = :p2 WHERE wprogramm_id = :p0";
}
else
{
command.CommandText = "select nextval('kundenverwaltung.wprogramm_seq')";
this.WprogrammID = (int)(long)command.ExecuteScalar();
command.CommandText = $"insert into {TABLE} ({COLUMNS}) values (:p0, :p1, :p2)";
}
command.Parameters.AddWithValue("p0", this.WprogrammID);
command.Parameters.AddWithValue("p1", (int)this.Nummer);
command.Parameters.AddWithValue("p2", string.IsNullOrEmpty(this.Bezeichnung) ? (object)DBNull.Value : this.Bezeichnung);
int result = command.ExecuteNonQuery();
DatenbankConnection.GetConnection().Close();
return result;
}
public int? WprogrammID { get; set; }
public int Nummer { get; set; }
public string Bezeichnung { get; set; }
}
}