public bool save(loggedInUser currentUser) { string selectCSQL = "select cid from Countries where name = @nam"; SqlCommand selectCCMD = new SqlCommand(); selectCCMD.CommandText = selectCSQL; selectCCMD.Connection = Starter.GetConnection(); selectCCMD.Parameters.Add(new SqlParameter("nam", country.countryName)); SqlDataReader Creader = selectCCMD.ExecuteReader(); Creader.Read(); //setzt den Reader auf den ersten / nächsten DS country.cID = Creader.GetInt32(0); string selectTSQL = "select tid from Transport where name = @nam"; SqlCommand selectTCMD = new SqlCommand(); selectTCMD.CommandText = selectTSQL; selectTCMD.Connection = Starter.GetConnection(); selectTCMD.Parameters.Add(new SqlParameter("nam", transport.transportName)); SqlDataReader Treader = selectTCMD.ExecuteReader(); Treader.Read(); transport.transportID = Treader.GetInt32(0); if (postID != -1) { //test, if user who created this post, is currently logged in user if (currentUser.uID.Equals(user.uID)) { string SQLSTMT = "update Posts set cid = @cid, content = @content, tid = @tid where postID = @id"; SqlCommand updateCMD = new SqlCommand(); updateCMD.CommandText = SQLSTMT; updateCMD.Connection = Starter.GetConnection(); //Die Parameter in SQL-String mit Werten versehen... updateCMD.Parameters.Add(new SqlParameter("cid", country.cID)); updateCMD.Parameters.Add(new SqlParameter("content", content)); updateCMD.Parameters.Add(new SqlParameter("tid", transport.transportID)); updateCMD.Parameters.Add(new SqlParameter("id", postID)); // ExecuteNonQuery() gibt die Anzahl der veränderten/angelegten Records zurück. return(updateCMD.ExecuteNonQuery() > 0); } } user.uID = currentUser.uID; user.userName = currentUser.userName; string SQL = "insert into Posts (uid, cid, content, tid) values (@uid, @cid, @content, @tid)"; SqlCommand cmd = new SqlCommand(); cmd.CommandText = SQL; cmd.Connection = Starter.GetConnection(); //Die Parameter in SQL-String mit Werten versehen... cmd.Parameters.Add(new SqlParameter("uid", currentUser.uID)); cmd.Parameters.Add(new SqlParameter("cid", country.cID)); cmd.Parameters.Add(new SqlParameter("content", content)); cmd.Parameters.Add(new SqlParameter("tid", transport.transportID)); // ExecuteNonQuery() gibt die Anzahl der veränderten/angelegten Records zurück. return(cmd.ExecuteNonQuery() > 0); }
public static loggedInUser login(string username, string password) { object o = searchByName(username); if (o != null) { Type typeOfo = o.GetType(); Transport control1 = new Transport(); Type transportType = control1.GetType(); Country control2 = new Country(); Type countryType = control2.GetType(); User control3 = new User(); Type userType = control3.GetType(); if (typeOfo == transportType || typeOfo == countryType) { Console.WriteLine("Name ist schon an ein Land oder Transportmittel vergeben."); return(null); } if (typeOfo == userType) { User user = (User)o; if (user.checkPassword(password)) { if (user.checkAdmin()) { AdminUser admin = new AdminUser(); admin.uID = user.uID; admin.userName = user.userName; admin.postIDs = user.postIDs; return(admin); } loggedInUser loggedInUser = new loggedInUser(); loggedInUser.uID = user.uID; loggedInUser.userName = user.userName; loggedInUser.postIDs = user.postIDs; return(loggedInUser); } else { Console.WriteLine("Username oder Passwort falsch bzw. Username schon vergeben."); return(null); } } } loggedInUser newUser = new loggedInUser(); newUser.userName = username; if (newUser.insert(password)) { return(newUser); } Console.WriteLine("Das Speichern des Datensatzes hat nicht funktioniert."); return(null); }