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