public int InsertNewUser(Account newuser) { int flag = 0; if (UserIsExistByName(newuser.UserName)) return flag; string sql = "INSERT INTO Account(acc_name, acc_pwd, acc_createtime, acc_pwdtip)values(@username,@password,@createtime,@passwordtip)"; using (DbTransaction transaction = connection.BeginTransaction()) { try { SQLiteParameter[] parameters = new SQLiteParameter[]{ new SQLiteParameter("@username", newuser.UserName), new SQLiteParameter("@password", newuser.PassWord), new SQLiteParameter("@createtime", newuser.CreateTime), new SQLiteParameter("@passwordtip",newuser.PwdTip)}; dataop.ExecuteNonQuery(sql, parameters); transaction.Commit(); flag = 1; } catch { flag = -1; transaction.Rollback(); throw; } } return flag; }
public Account GetAccountInfoByName(string username) { if (!UserIsExistByName(username)) return null; Account newuser = new Account(); string sql = "SELECT * FROM Account WHERE acc_name=" + "'" + username + "'"; using (DbTransaction transaction = connection.BeginTransaction()) { using (SQLiteDataReader reader = dataop.ExecuteReader(sql, null)) { if (reader.HasRows) { while (reader.Read()) { newuser.UserId = reader.GetInt32(0); newuser.UserName = reader.GetString(1); newuser.PassWord = reader.GetString(2); newuser.CreateTime = reader.GetDateTime(3); newuser.PwdTip = reader.GetString(4); } } else return null; } transaction.Commit(); } return newuser; }