Exemple #1
0
        public static IEnumerable <User> GetUsers()
        {
            List <User> lstuser = new List <User>();

            using (SqlConnection con = POCDB.GetNewSQLConnection())
            {
                var        sqlQuery = "SELECT * FROM Users";
                SqlCommand cmd      = new SqlCommand(sqlQuery, con);

                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    User user = new User();
                    user.Id       = Convert.ToInt32(rdr["Id"]);
                    user.Name     = rdr["Name"].ToString();
                    user.Email    = rdr["Email"].ToString();
                    user.Password = rdr["Password"].ToString();

                    lstuser.Add(user);
                }
                con.Close();
            }
            return(lstuser);
        }
Exemple #2
0
        public static bool CreateUser(User newUser)
        {
            using (SqlConnection con = POCDB.GetNewSQLConnection())
            {
                try
                {
                    con.Open();
                    // insert statement
                    const string INSERT_STATEMENT = "INSERT INTO Users " +
                                                    "(Name, Email,Password) VALUES " +
                                                    "(@Name, @Email,@Password)";

                    // create insert command
                    SqlCommand cmd = new SqlCommand(INSERT_STATEMENT);
                    // add values to command
                    cmd.Parameters.AddWithValue("@Name", newUser.Name);
                    cmd.Parameters.AddWithValue("@Email", newUser.Email);
                    cmd.Parameters.AddWithValue("@Password", newUser.Password);
                    cmd.Connection = con;

                    // execute the command
                    int result = cmd.ExecuteNonQuery();
                    if (result >= 1)
                    {
                        return(true);
                    }

                    return(true);
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    con.Close();
                    con.Dispose();
                }
            }
        }
Exemple #3
0
        public static User GetUserByEmail(string email)
        {
            User newUser = new User();

            using (SqlConnection con = POCDB.GetNewSQLConnection())
            {
                var        sqlQuery = "Select * from Users where Email='" + email + "'";
                SqlCommand cmd      = new SqlCommand(sqlQuery, con);

                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    newUser.Id       = Convert.ToInt32(rdr["Id"]);
                    newUser.Name     = rdr["Name"].ToString();
                    newUser.Email    = rdr["Email"].ToString();
                    newUser.Password = rdr["Password"].ToString();
                }
                con.Close();
            }
            return(newUser);
        }
Exemple #4
0
        public static bool Insert(List <POCXml> pxmls)
        {
            // get a new connection
            SqlConnection conn = POCDB.GetNewSQLConnection();

            try
            {
                conn.Open();

                foreach (POCXml xml in pxmls)
                {
                    // create insert command
                    SqlCommand insertCmd = CreateInsertCommand(xml);
                    insertCmd.Connection = conn;

                    // execute the command
                    int result = insertCmd.ExecuteNonQuery();

                    // if the command couldn't insert new record,
                    // stop the process immediately
                    if (result != 1)
                    {
                        return(false);
                    }
                }
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
                conn.Dispose();
            }
        }