public void WriteUser(User user) { string sqlstr; DataRowCollection result = null; if (user != null && this.isConnected()==true ) { sqlstr = "SELECT * FROM login WHERE username='******'"; try { result = db.GetDataTable(sqlstr).Rows; } catch (Exception) { Console.WriteLine("Error: can't get user from database"); this.isconnected = false; throw new Exception("can't get user from database"); } if (result.Count > 0) { sqlstr = string.Format("UPDATE login set password='******',sex={1},lastlogin='******' WHERE username='******'", user.Password, (int)user.Sex, user.lastLogin,user.Name ); try { db.ExeSql(sqlstr); } catch (Exception) { Console.WriteLine("Error: can't create new user in database"); this.isconnected = false; throw new Exception("can't create new user in database"); } } else { sqlstr = string.Format("INSERT INTO login(username,password,sex,lastlogin) VALUES ('{0}','{1}',{2},'{3}')",user.Name, user.Password,(int)user.Sex, user.lastLogin); try { db.ExeSql(sqlstr); } catch (Exception ex) { Console.WriteLine("Error: can't create new user in database:" + ex.Message ); throw new Exception("can't create new user in database"); } } } }
public void WriteUser(User user) { string sqlstr; DataRowCollection result = null; if (user != null && this.isConnected()==true ) { sqlstr = "SELECT * FROM `login` WHERE `username`='" + user.Name + "'"; try { result = SQLExecuteQuery(sqlstr); } catch (Exception) { Console.WriteLine("Error: can't get user from database"); this.isconnected = false; throw new Exception("can't get user from database"); } if (result.Count > 0) { sqlstr = string.Format("UPDATE `login` set `password`='{0}',`sex`='{1}',`lastlogin`='{2}' WHERE `username`='{3}'", user.Password, (int)user.Sex, user.lastLogin,user.Name ); try { MySqlHelper.ExecuteNonQuery(db, sqlstr, null); } catch (Exception) { Console.WriteLine("Error: can't create new user in database"); this.isconnected = false; throw new Exception("can't create new user in database"); } } else { sqlstr = string.Format("INSERT INTO `login`(`username`,`password`,`sex`,`lastlogin`) VALUES ('{0}','{1}','{2}','{3}')",user.Name, user.Password,(int)user.Sex, user.lastLogin); try { SQLExecuteNonQuery(sqlstr); } catch (Exception ex) { Console.WriteLine("Error: can't create new user in database:" + ex.Message ); throw new Exception("can't create new user in database"); } } } }
public User GetUser(string username) { User user=new User(username); string sqlstr; DataRow result; if (user != null && this.isConnected() == true) { sqlstr = "SELECT * FROM login WHERE username = '******'"; try { result = db.GetDataTable(sqlstr).Rows[0]; } catch (Exception) { return null; } try { user.AccountID = Convert.ToInt32(result["account_id"]); user.Password = (string)result["password"]; user.Sex = byte2Gender((byte)result["sex"]); user.lastLogin = (string)result["lastlogin"]; user.Banned = Convert.ToBoolean(result["Banned"]); } catch (Exception ex) { Console.WriteLine(ex.Message); } return user; } return null; }
public User GetUser(User user) { string sqlstr; DataRow result; if (user != null && this.isConnected() == true) { sqlstr = "SELECT * FROM `login` WHERE `username`='" + user.Name + "'"; try { result = SQLExecuteQuery(sqlstr)[0]; } catch (Exception) { return null; } try { user.AccountID = Convert.ToInt32(result["account_id"]); user.Password = (string)result["password"]; user.Sex = byte2Gender((byte)result["sex"]); user.lastLogin = (string)result["lastlogin"]; user.Banned = Convert.ToBoolean(result["Banned"]); } catch (Exception ex) { Logger.ShowError(ex); } return user; } return null; }