Created
March 13, 2009 17:31
-
-
Save fitoria/78656 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Data; | |
using System.Data.SQLite; | |
/* create table usuarios( | |
* username text primary key, | |
* password text not null, | |
* ultima_sesion text not null, | |
* fecha_creacion text not null | |
* ) | |
*/ | |
namespace ciberboro | |
{ | |
class usuario | |
{ | |
conexion con; | |
String username; | |
String password; | |
DateTime ultimaSesion; | |
DateTime fechaCreacion; | |
SQLiteCommand comando; | |
bool logueado = false; | |
public usuario(String username, String password) | |
{ | |
//procedimiento para crear un usuario | |
this.username = username; | |
this.password = password; | |
this.fechaCreacion = DateTime.Today; | |
this.ultimaSesion = DateTime.Today; | |
} | |
public usuario(string username) | |
{ | |
//para leer un usuario | |
this.username = username; | |
string query = "select username, password, ultima_sesion from usuario " + | |
"where username = @username;"; | |
con = new conexion(); | |
comando = new SQLiteCommand(query, con.ConexionSql); | |
SQLiteParameter paramUsername = new SQLiteParameter("@username", this.username); | |
comando.Parameters.Add(paramUsername); | |
SQLiteDataReader dr; | |
dr = comando.ExecuteReader(); | |
if (dr.HasRows == true) | |
{ | |
while (dr.Read() == true) | |
{ | |
this.password = dr[1].ToString(); //TODO: check this shit out! | |
//this.ultimaSesion = DateTime.Parse(dr[2].ToString()); | |
// this.fechaCreacion = DateTime.Parse(dr[3].ToString()); | |
} | |
} | |
else | |
{ | |
throw new NullReferenceException("no existe el usuario"); | |
} | |
} | |
//propiedades | |
public String Username | |
{ | |
get { return username; } | |
set { username = value; } | |
} | |
public bool guardar() | |
{ | |
//procedimiento para guardar el usuario. | |
string query; | |
/*if (this.logueado == true) | |
{ | |
query = "update usuarios set password = '@password', " + | |
"fecha_creacion = '@fecha_creacion', ultima_sesion = '@ultima_sesion' where username = '@user_actual';"; | |
} | |
else | |
{*/ | |
query = "INSERT INTO usuario(id_usuario, username, password, fecha_registro, ultima_sesion) VALUES(" + | |
"null, @username, @password, @fecha_creacion, @ultima_sesion);"; | |
//} | |
con = new conexion(); | |
comando = new SQLiteCommand(query, con.ConexionSql); | |
SQLiteParameter paramUsername = new SQLiteParameter("@username", this.username); | |
SQLiteParameter paramPassword = new SQLiteParameter("@password", this.password); | |
SQLiteParameter paramFecha = new SQLiteParameter("@fecha_creacion", this.fechaCreacion.ToString()); | |
SQLiteParameter paramUltimaSesion = new SQLiteParameter("@ultima_sesion", this.ultimaSesion.ToString()); | |
comando.Parameters.Add(paramUsername); | |
comando.Parameters.Add(paramPassword); | |
comando.Parameters.Add(paramFecha); | |
comando.Parameters.Add(paramUltimaSesion); | |
try | |
{ | |
comando.ExecuteNonQuery(); | |
return true; | |
} | |
catch (Exception e) | |
{ | |
Console.WriteLine(e.Message); | |
return false; | |
} | |
} | |
public bool login() | |
{ | |
//proceso para loguearse | |
string query = "select username, password, ultima_sesion from usuario " + | |
"where username = @username and password = @password;"; | |
con = new conexion(); | |
comando = new SQLiteCommand(query, con.ConexionSql); | |
SQLiteParameter paramUsername = new SQLiteParameter("@username", this.username); | |
SQLiteParameter paramPassword = new SQLiteParameter("@password", this.password); | |
comando.Parameters.Add(paramUsername); | |
comando.Parameters.Add(paramPassword); | |
SQLiteDataReader dr; | |
dr = comando.ExecuteReader(); | |
if (dr.HasRows == true) | |
{ | |
while (dr.Read() == true) | |
{ | |
this.username = dr[0].ToString(); | |
this.password = dr[1].ToString(); | |
//this.ultimaSesion = DateTime.Parse(dr[2].ToString()); | |
//this.fechaCreacion = DateTime.Parse(dr[3].ToString()); | |
} | |
this.logueado = true; | |
query = "update usuario set ultima_sesion = '" + DateTime.Now.ToString() + "' where username = @username"; | |
comando = new SQLiteCommand(query, con.ConexionSql); | |
comando.Parameters.Add(paramUsername); | |
comando.ExecuteNonQuery(); | |
return true; | |
} | |
else | |
{ | |
return false; | |
} | |
} | |
public bool borrar() | |
{ | |
con = new conexion(); | |
string query = "delete from usuarios where username = @username;"; | |
comando = new SQLiteCommand(query, con.ConexionSql); | |
comando.Parameters.Add(new SQLiteParameter("@username", this.username)); | |
try | |
{ | |
comando.ExecuteNonQuery(); | |
return true; | |
} | |
catch (Exception e) | |
{ | |
Console.Write(e.Message); | |
return false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment