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");
}
}
}
}