コード例 #1
0
        public UserClass Login(string username, string password)
        {
            string sql = "select Name, Surname, BirthDate, Username, Password, Type, Image from users " +
                         "where Username=@username and Password=@password";
            SqlCommand cmd = new SqlCommand(sql, conn);

            cmd.Parameters.AddWithValue("@username", username);
            cmd.Parameters.AddWithValue("@password", password);
            conn.Open();
            using (SqlDataReader reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    string    name          = reader["Name"].ToString();
                    string    surname       = reader["Surname"].ToString();
                    DateTime  birthdate     = DateTime.Parse(reader["BirthDate"].ToString());
                    string    _username     = reader["Username"].ToString();
                    string    _password     = reader["Password"].ToString();
                    int       id            = (int)reader["Type"];
                    string    roadToPicture = reader["Image"].ToString();
                    UserClass user          = new UserClass(name, surname, birthdate, _username, _password);
                    user.UserType = EnumConverters.getUserType(id);
                    user.setProfilePicture(roadToPicture);
                    return(user);
                }
            }
            conn.Close();


            throw new Exception("Bad username/password!");
        }
コード例 #2
0
        public void Register(UserClass user)
        {
            SqlConnection con = new SqlConnection(@"Server=.;Database=db_sc;Trusted_Connection=true;");
            SqlCommand    cmd = new SqlCommand("Select * from Users where Username= @Username", con);

            cmd.Parameters.AddWithValue("@Username", user.getUsername());
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();

            if (dr.HasRows == true)
            {
                throw new Exception("Username is used!");
            }
            else
            {
                string sql = "insert into Users(Name, Surname, BirthDate, Username, Password, Type, Image)" +
                             "values (@name, @surname, @birthdate, @username, @password, @userType, @image)";
                SqlCommand cmd2 = new SqlCommand(sql, conn);
                cmd2.Parameters.AddWithValue("@name", user.GetName());
                cmd2.Parameters.AddWithValue("@surname", user.GetSurname());
                cmd2.Parameters.AddWithValue("@birthdate", user.GetBirthdate());
                cmd2.Parameters.AddWithValue("@username", user.getUsername());
                cmd2.Parameters.AddWithValue("@password", user.getPassword());
                int id = EnumConverters.getUserTypeID(user.UserType);
                cmd2.Parameters.AddWithValue("@userType", id);
                cmd2.Parameters.AddWithValue("@image", "C:/Users/Admin/Downloads/icon.png");
                conn.Open();
                cmd2.ExecuteNonQuery();
                conn.Close();
            }
            con.Close();
        }