Ejemplo n.º 1
0
        public bool Insert(User entity)
        {
            // responsible for adding new users to the db. Takes a parameter which is type of User.

            SqlCommand    cmd    = new SqlCommand();
            DBConnection  tempDB = new DBConnection();
            SqlDataReader reader = null;

            dBConnection.OpenConnection();
            tempDB.OpenConnection();


            // clean the attribute. otherwise values left from previous operations may cause conflict
            _rowsAffected = 0;

            try
            {
                // first add the person object to the db. It holds detailed information of user.
                InsertPerson(entity.Person);
                dBConnection.OpenConnection();
                int id = 0; // will hold the last added person's id

                // the last added person will have the greatest id value so bring it from the db.
                cmd.CommandText = DBCommandCreator.SELECT(new string[] { "kisiID" }, DBTableNames.Person, "WHERE kisiID = (SELECT MAX(kisiID) FROM tblKisi)");
                reader          = dBConnection.DataReader(cmd);
                if (reader.HasRows)
                {
                    while (reader.Read())
                    {
                        id = reader.GetInt32(0);
                    }
                }


                SqlCommand cmdUser = new SqlCommand();

                // call a stored proc. to add newly created user to the db.
                cmdUser.CommandText = DBCommandCreator.EXEC(new string[] { "kullaniciAd", "sifre", "kisiID" }, "SP_kullaniciKayit");
                DBCommandCreator.AddParameter(cmdUser, "@kullaniciAd", DbType.String, ParameterDirection.Input, entity.Username);
                DBCommandCreator.AddParameter(cmdUser, "@sifre", DbType.String, ParameterDirection.Input, entity.Password);
                DBCommandCreator.AddParameter(cmdUser, "@kisiID", DbType.Int32, ParameterDirection.Input, id);

                _rowsAffected = tempDB.ExecuteQueries(cmdUser);


                // if added, affected rows will be greater than 0
                return(_rowsAffected > 0);
            }
            catch (Exception ex)
            {
                throw new Exception("An error occured while executing Insert() in SpiceApp.DataAccessLayer.UserRepository", ex);
            }
            finally
            {
                if (reader != null)
                {
                    reader.Close();
                }
                dBConnection.CloseConnection();
                tempDB.CloseConnection();
            }
        }