private void RegistrationButtom_Click(object sender, EventArgs e) { var user = new User(); if (user.LoginAvailable(loginField.Text)) { MessageBox.Show(Resources.user_there_in_db, Resources.error_operation_msg); } else { user.CreateNew(loginField.Text, pswField.Text); } }
/// <summary> /// Inserts the user. /// </summary> /// <param name="user">The user.</param> /// <returns><c>true</c> on success.</returns> public bool InsertUser(User user) { Log.DebugFormat("InsertUser '{0}'", user); using (dbConnection) { using (DbCommand dbCommand = dbConnection.CreateCommand()) { dbConnection.Open(); string commandText = String.Format(CultureInfo.InvariantCulture, "INSERT INTO Users (Name, Password, Level) VALUES('{0}', '{1}', {2})", user.Name, user.Password, user.Level); Log.Debug(commandText); dbCommand.CommandText = commandText; if (dbCommand.ExecuteNonQuery() == 1) { return true; } Log.ErrorFormat("Failed to insert user [{0}]", user); return false; } } }
/// <summary> /// Gets the list of users. /// </summary> /// <returns><see cref="IList{T}"/></returns> public IList<User> GetUsers() { Log.Debug("GetUsers"); List<User> users = new List<User>(); using (dbConnection) { using (DbCommand dbCommand = dbConnection.CreateCommand()) { dbConnection.Open(); // Create a table in the database //dbCommand.CommandText = "CREATE TABLE FOO (ID INTEGER PRIMARY KEY, MYVALUE VARCHAR(50))"; //dbCommand.ExecuteNonQuery(); // Create a parameterized insert command //dbCommand.CommandText = "INSERT INTO FOO (MYVALUE) VALUES(?)"; //dbCommand.Parameters.Add(dbCommand.CreateParameter()); // Insert 10 rows into the database //for (int n = 0; n < 10; n++) //{ // dbCommand.Parameters[0].Value = "Value " + n; // dbCommand.ExecuteNonQuery(); //} // Now read them back const string commandText = "SELECT * FROM Users"; dbCommand.CommandText = commandText; Log.Debug(commandText); using (DbDataReader reader = dbCommand.ExecuteReader()) { while (reader.Read()) { User user = new User { Id = Int32.Parse(reader[0].ToString()), Name = reader[1].ToString(), Password = reader[2].ToString(), Level = Int32.Parse(reader[3].ToString()) }; Log.DebugFormat("Adding user : [{0}]", user); users.Add(user); } } } } return users; }
/// <summary> /// Gets the user. /// </summary> /// <param name="username">The username.</param> /// <returns></returns> public User GetUser(string username) { Log.DebugFormat("GetUser '{0}'", username); using (dbConnection) { using (DbCommand dbCommand = dbConnection.CreateCommand()) { dbConnection.Open(); string commandText = String.Format(CultureInfo.InvariantCulture, "SELECT * FROM Users WHERE Name='{0}'", username); dbCommand.CommandText = commandText; Log.Debug(commandText); using (DbDataReader reader = dbCommand.ExecuteReader()) { while (reader.Read()) { User user = new User { Id = Int32.Parse(reader[0].ToString()), Name = reader[1].ToString(), Password = reader[2].ToString(), Level = Int32.Parse(reader[3].ToString()) }; Log.DebugFormat("Have user : [{0}]", user); return user; } } } } Log.WarnFormat("User '{0}' not found!", username); return null; }